The Twilio for WhatsApp API supports sending and receiving media messages with WhatsApp users. The API works like sending MMS messages with Twilio, however there are some rules and restrictions that are unique to WhatsApp.
Please note that, unlike MMS, a WhatsApp media message can only contain one media attachment. Any additional attachments will be ignored.
- Usage Example
- Rules and Limitations
- Supported File Types
- File name behavior on WhatsApp media messages
- Related Topics
To send a media message with WhatsApp, include the
MediaUrl parameter with your HTTP POST request. This is exactly like sending an MMS message with Twilio Programmable SMS.
Here’s an example cURL script:
curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json \ --data-urlencode "To=whatsapp:+13105555555" \ --data-urlencode "From=whatsapp:+12125551234" \ --data-urlencode "Body=Thanks for contacting me on WhatsApp! Here is a picture of an owl." \ --data-urlencode "MediaUrl=https://demo.twilio.com/owl.png" \ -u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'
This example will contact the WhatsApp destination (310) 555-5555 from the sender (212) 555-1234 via the WhatsApp app, and includes the image owl.png, and the following message:
Thanks for contacting me on WhatsApp! Here is a picture of an owl.
To make this script work for you, make the following updates, and then paste it into a terminal window:
- Line 1 update with your Account SID.
- Line 2 update with a valid destination.
- Line 3 update with a valid WhatsApp sender number.
- Line 4 update with your desired message.
- Note: A free-form message like this is only possible during a 24 hour user-initiated session. Unprompted messages have template requirements. For more information, see the Rules and Limitations section below.
- Line 5 update with a valid media file url.
- Line 6 update with your Account SID and Auth Token.
For full details, including sample code from our Helper Library SDKs, please see Twilio API for WhatsApp (Twilio Docs).
Rules and Limitations
You can send media messages to any WhatsApp user who has sent you a message within the past 24 hours.
If a user has not sent a message to your WhatsApp number within the past 24 hours, you cannot send them a media message through WhatsApp. This means media files are not allowed in pre-approved templates.
This is because WhatsApp only allows sending media on session messages. WhatsApp divides outgoing messages into two categories: template messages and session messages. Template messages are the initial messages you send to users, and must follow a pre-approved template cleared by WhatsApp. Once a user starts a “session” by sending you a message, you can exchange free-form messages with them for 24 hours. For more detail, please see Rules and Best Practices for WhatsApp Messaging on Twilio.
Supported File Types
The Twilio API for WhatsApp supports sending and receiving images, audio, and PDF files. The following formats are currently supported:
|Images||JPG, JPEG, PNG|
|Audio||MP3, OGG, AMR|
|Video||MP4 (with H.264 video codec and AAC audio)|
You can send media messages up to 16 MB in size. A WhatsApp media message can only contain one media object. Additional MediaUrl parameters will be ignored. At this time, Twilio will not transcode media for outgoing WhatsApp messages, so if you need to send a media object that is larger than 16 MB, please reduce the file size before sending it to Twilio.
Please note that WhatsApp does not support including a text body in the same message as a video, audio file, document, contact (vCard), or location. If you pass the Body parameter on a message with one of these media types, the body will be ignored and not delivered to the device.
Notice: MP4 video files which do not include an audio track (silent videos) may fail with error 63005, due to a bug in WhatsApp. This issue persists as of June 2020. To work around this issue, please add an audio track (with AAC codec) to any MP4 video sent via WhatsApp. The audio track can contain silence.
File name behavior on WhatsApp media messages
Outbound WhatsApp messages with media: Outbound media messages will generally have the media filename preserved. However, if you send a video file, WhatsApp may rename the file to "WhatsApp Video [timestamp]", which is the WhatsApp application's default behavior.
Inbound WhatsApp messages with media: The media filename as sent by the user is shown in the message body. The actual filename associated with the media object will be a random string.
If a WhatsApp media message was not delivered, there are a few likely explanations:
- The media message was not sent during an active session with the user (see Rules and Limitations above).
- Twilio encountered an error when trying to get your media.
- Your message, including media, is larger than 16 MB.
- The content-type headers of your media attachment do not match the file extension (for example, content-type is image/jpg but file extension is .png).
If you included a message body with your WhatsApp media message, and only the media was delivered to the user, please note that this is expected when sending an audio, contact, or location message. To deliver a text message as well as one of these media types, please send two separate messages.
Adding a media attachment to a message does not affect the message price. Media messages have the same pricing as a standard Session message to the same destination country. For more information, see Twilio API for WhatsApp pricing.