Before sending templates created using the Content API, you'll need:
In this tutorial, we will be using WhatsApp.
If you already configured your sender's webhook and plan to switch to the Messaging Services' webhook, be sure to plan for up to a minute of downtime during the switch.
You are ready to start creating and sending message content!
Next, we'll explore the following using the Content API:
For more information on possible requests see Content API Resources.
For all examples, replace the $TWILIO_ACCOUNT_SID
with your Twilio Account SID and $TWILIO_AUTH_TOKEN
with your Twilio Auth Token.
In the following code sample, we will be creating a quick-reply content type template, twilio/quick-reply
. Quick replies let recipients tap, rather than type, to respond to the message on WhatsApp and Facebook Messenger.
Note that there is a second text content type, twilio/text
. This means that the message content you are creating can be used to send quick replies, but if the end user's channel does not support quick replies, Twilio will automatically default to a simple text message. This lets you send the same content to all messaging channels.
In addition, we are using a placeholder value, {{1}}. This value can later be replaced dynamically when the message is sent. If you do not provide a value at message send time, the default value specified via variables
will be used.
For additional content types see Content Types and Definitions.
Tip: Provide self-evident default variable values if you plan to submit your template for approval to WhatsApp. These will be used as sample values during the template approval process.
Create a quick reply template using Content API
After creating your first template, note down the ContentSid, HXXXXXXXXXXXXXXXXXXX
, found in the response to your content creation request. You will be using that SID throughout the tutorial.
The Content API supports an unlimited number of templates, however WhatsApp limits each business to 6000 approved templates per WhatsApp Business Account (WABA).
Using the "content sid", HXXXXXXXXXXXXXXXXXXX
, from the response in the previous step, you can utilize a GET
request to review individual content templates. To review all templates omit {ContentSid} from your GET
request URL.
Fetch a Content API Resource
To send WhatsApp messages to start a conversation with an end user ("business-initiated" conversation), you will need to get the message template approved by WhatsApp. However, you can send messages without WhatsApp approval by responding to an inbound message from the end-user. The free-form session lasts 24-hours. We will take this approach in this tutorial.
Start by sending any message, such as "hi", from the number you wish to reach to the sender in your WhatsApp sender (the one in your messaging services sender pool).
Now, send the quick reply content template to the number you wish to reach. Pay special attention to these fields:
From
:
the sender
must be replaced with the Messaging Service Sid
. Alternatively, you may use the sender in the
From
field, but you must add the additional parameter
MessagingServiceSid
to invoke the Messaging Service.
ContentSid
:
specify your
HXXXXXXXXXXXXXXXX
sid.
ContentVariables
:
specify the values you want to substitute into your placeholder variables.
At this point, you should receive a response back from your WhatsApp sender, with 3 quick reply buttons! Note that if you change the recipient in the To
field to an SMS-capable phone number, you will receive the twilio/text
content instead.
To use your content template to start WhatsApp business-initiated conversations, you can submit your template for approval. Approval by WhatsApp ranges from 5 minutes to 24 hours.
Note that WhatsApp only allows some template types to be submitted for approval and be used for notifications. For more info, see Content Types Overview.
WhatsApp has strict template approval criteria that are evolving over time. For best practices, please review the following documentation.
Fetching an approval status returns the current state of the WhatsApp template approval submission. The following statuses are possible:
Received
: The request has successfully been submitted to Twilio. Generally Twilio submits these immediately to WhatsApp.
Pending
: WhatsApp has received the submission and is currently processing the request.
Approved
: WhatsApp has approved the template and it is now available for use.
Rejected
: The template was rejected by WhatsApp. WhatsApp's reason for the rejection is found in the
rejection_reason
field. For tips to avoid template rejections, please review the following
documentation
If you are seeing successfully created templates remain in Received
status, this could mean WhatsApp does not accept them for approval, such as twilio/list-picker
.
Congratulations on successfully using the Content API! Next, we recommend checking out the supported Content Types. You can also review additional Content API Resources.