Objective
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.
Product
Programmable Messaging
Procedure
Usage Example
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.
- Unless your Twilio number has been enabled for WhatsApp in production, use your WhatsApp Sandbox number for sending messages.
-
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 on both session and template messages. Media templates are only supported on Content Templates. See our documentation for how to create a media template on Content API and Content Editor.
Supported File Types
The Twilio API for WhatsApp supports sending and receiving the following media formats:
Images | JPG, JPEG, PNG, WEBP* |
Audio | MP3, OGG**, AMR, 3GP, AAC, MPEG |
Documents | PDF, DOC, DOCX, PPTX, XLSX |
Video | MP4 (with H.264 video codec and AAC audio), HEIC |
Contacts | vCard (.vcf) |
*The image/webp
MIME type is only used for WhatsApp stickers, which have additional requirements. See the section "WhatsApp Stickers" below for more details.
**WhatsApp outbound OGG is only supported when the ogg file uses the opus audio codec. These files may have the .ogg
or the .opus
extension.
The following size restrictions apply to WhatsApp media messages:
- Audio, Video and Document media messages up to 16 MB.
- Stickers up to 100KB.
- Images up to 5MB.
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 the limit for that media type, please reduce the file size before sending it to Twilio.
Please note that the Twilio API for WhatsApp does not support including a text body ("caption") in the same message as a webp image, 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.
If you would like to include a caption within a WhatsApp media message, you can utilize a Media Content Template.
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.
Sending WhatsApp "Voice Notes" (audio play button appears in-app)
WhatsApp mobile users have the ability to record and send "Voice Notes" by holding a microphone button in the app. These appear as an audio message with a play button, rather than a downloadable file.
Businesses using the Twilio API for WhatsApp can send a Voice Note by using the OGG file format (with the opus audio codec – see above).
Only OGG files are natively playable in the WhatsApp app. Other audio formats supported by WhatsApp, such as MP3 files, will appear as a downloadable file in the app.
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.
WhatsApp Stickers
WhatsApp Stickers are large, colorful emojis in webp
format. To send a sticker on the WhatsApp Business API with Twilio, it must fulfil the following requirements:
- Stickers must have a transparent background.
- Stickers must be exactly 512x512 pixels.
- Stickers must be less than 100 KB.
- Stickers must be static - animated stickers are not supported.
Message bodies are not supported when sending stickers. Any message body included in a request with a sticker will be ignored.
Troubleshooting
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/was not sent with an approved media template (see Rules and Limitations above).
- Twilio encountered an error when trying to get your media.
- Your message, including media, is larger than the limit for that file type.
- 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.
Pricing
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.