Content API Quickstart
Before Starting
Before sending templates created using the Content API, you'll need:
- At least one valid sender. This can be a phone number, WhatsApp sender, or Facebook Messenger sender (available by requesting through a support ticket).
- A Messaging Service. The Messaging Service provides a higher-level “bundling” of messaging functionality around a common set of senders.
Configuring Your Sender
- To send SMS or MMS, use a phone number. You can port a number or purchase one from the Twilio Console by navigating to Phone Numbers.
- To send WhatsApp messages, use your Twilio phone number or your own number to register a WhatsApp sender with the self-signup process. (If you are an ISV, use guided signup). In this tutorial, we will be using WhatsApp.
- To send Facebook Messenger messages, navigate to the Channels area in the Twilio Console and connect your Facebook page to create a FB Messenger sender.
Configuring Your Messaging Service
1. Create a Messaging Service by navigating to Messaging > Services and clicking "Create Messaging Service".
2. Give your Messaging Service a friendly name and select your use case. Once you get to step 2, click to "Add Senders" to your service and add the sender(s) that you'll be using to send messages.
3. Proceed to Step 3 to "Set up integration" and choose how you want to receive messages via webhooks.
- If you are already using a sender that has an existing webhook configuration, choose "Defer to sender's webhook".
- You may choose "Send a webhook" if you want to use this Messaging Service's webhook. This option lets you to manage multiple senders' with this single webhook.
- Optional: If you want to use Conversations API, choose "Autocreate a conversation."
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.
4. If you are using SMS and MMS, you may need to complete Step 4 "Add compliance info" depending on your region's compliance requirements. Otherwise, you are can click "Click to Complete Messaging Service Setup" and click "View my new Messaging Service".
5. Find your Messaging Service Sid and save it. You will need this later.
You are ready to start creating and sending message content!
Create and Send Your First Content API Template
Next, we'll explore the following using the Content API:
- Create a template
- Review individual templates
- Send a Message
- Submit templates for approval to WhatsApp
- Fetch Approval Status
- Delete individual templates
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.
Create a Template
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.
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).
Review Individual Templates
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.
Send a Message on WhatsApp
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 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 theFrom
field, but you must add the additional parameterMessagingServiceSid
to invoke the Messaging Service.ContentSid
: specify yourHXXXXXXXXXXXXXXXX
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.
Submit a Template for WhatsApp Approval
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.
Fetch an Approval Status
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 therejection_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
.
Next: Learn about Content Types
Congratulations on successfully using the Content API! Next, we recommend checking out the supported Content Types. You can also review additional Content API Resources.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.