Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this pageProducts used

Content API Quickstart



Before Starting

before-starting page anchor

Before sending templates created using the Content API, you'll need:

  1. At least one valid sender. This can be a phone number, WhatsApp sender, or Facebook Messenger sender (available by requesting through a support ticket).
  2. A Messaging Service. The Messaging Service provides a higher-level "bundling" of messaging functionality around a common set of senders.

Configuring Your Sender

configuring-your-sender page anchor

In this tutorial, we will be using WhatsApp.

Configuring Your Messaging Service

configuring-your-messaging-service page anchor
  1. Create a Messaging Service by navigating to Messaging > Services and clicking " Create Messaging Service(link takes you to an external page) ".
create-messaging-services-twilio.
  1. 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.
messaging-services-add-senders-to-sender-pool.
  1. 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 manage multiple senders' with this single webhook.
  • Optional: If you want to use Conversations API, choose "Autocreate a conversation."
messaging-services-configuration-integration.
(information)

Info

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.

  1. If you are using SMS and MMS, you may need to complete Step 4 "Add compliance info" depending on your region's compliance requirements(link takes you to an external page) . Otherwise, you can click "Click to Complete Messaging Service Setup" and click "View my new Messaging Service".
  2. Find your Messaging Service Sid and save it. You will need this later.
messaging-service-sid.

You are ready to start creating and sending message content!


Create and Send Your First Content API Template

create-and-send-your-first-content-api-template page anchor

Next, we'll explore the following using the Content API:

  1. Create a template
  2. Review individual templates
  3. Send a Message
  4. Submit templates for approval to WhatsApp
  5. Fetch Approval Status
  6. 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.


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.

(information)

Info

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 your first Content API Template

create-your-first-content-api-template page anchor

Create a quick reply template using Content API

curl

_30
curl -X POST 'https://content.twilio.com/v1/Content' \
_30
-H 'Content-Type: application/json' \
_30
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN \
_30
-d '{
_30
"friendly_name": "flight_replies",
_30
"language": "en",
_30
"variables": {"1":"Owl Air Customer"},
_30
"types": {
_30
"twilio/quick-reply": {
_30
"body": "Hi, {{1}} 👋 \nThanks for contacting Owl Air Support. How can I help?",
_30
"actions": [
_30
{
_30
"title": "Check flight status",
_30
"id": "flightid1"
_30
},
_30
{
_30
"title": "Check gate number",
_30
"id": "gateid1"
_30
},
_30
{
_30
"title": "Speak with an agent",
_30
"id": "agentid1"
_30
}
_30
]
_30
},
_30
"twilio/text": {
_30
"body": "Hi, {{1}}. \n Thanks for contacting Owl Air Support. How can I help?."
_30
}
_30
}
_30
}'

Output

_38
{
_38
"language": "en",
_38
"date_updated": "2022-08-29T10:43:20Z",
_38
"variables": {
_38
"1": "Owl Air Customer"
_38
},
_38
"friendly_name": "flight_replies",
_38
"account_sid": "ACXXXXXXXXXXXXXXXXXXX",
_38
"url": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_38
"sid": "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_38
"date_created": "2022-08-29T10:43:20Z",
_38
"types": {
_38
"twilio/text": {
_38
"body": "Hi, {{ 1 }}. \n Thanks for contacting Owl Air Support. How can I help?."
_38
},
_38
"twilio/quick-reply": {
_38
"body": "Hi, {{ 1 }}. \n Thanks for contacting Owl Air Support. How can I help?",
_38
"actions": [
_38
{
_38
"id": "flightid1",
_38
"title": "Check flight status"
_38
},
_38
{
_38
"id": "gateid1",
_38
"title": "Check gate number"
_38
},
_38
{
_38
"id": "agentid1",
_38
"title": "Speak with an agent"
_38
}
_38
]
_38
}
_38
},
_38
"links": {
_38
"approval_fetch": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests",
_38
"approval_create": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests/whatsapp"
_38
}
_38
}

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.

(information)

Info

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

review-individual-templates page anchor

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.

Review Content API Templates

review-content-api-templates page anchor

Fetch a Content API Resource

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.content.v1.contents('HX...XXX')
_10
.fetch()
_10
.then(content => console.log(content.friendlyName));

Output

_26
{
_26
"sid": "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"friendly_name": "Some content",
_26
"language": "en",
_26
"variables": {
_26
"name": "foo"
_26
},
_26
"types": {
_26
"twilio/text": {
_26
"body": "Foo Bar Co is located at 39.7392, 104.9903"
_26
},
_26
"twilio/location": {
_26
"longitude": 104.9903,
_26
"latitude": 39.7392,
_26
"label": "Foo Bar Co"
_26
}
_26
},
_26
"url": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"date_created": "2015-07-30T19:00:00Z",
_26
"date_updated": "2015-07-30T19:00:00Z",
_26
"links": {
_26
"approval_create": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests/whatsapp",
_26
"approval_fetch": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests"
_26
}
_26
}


Send a Message on WhatsApp

send-a-message-on-whatsapp page anchor

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.

Send a Template Created using Content API

send-a-template-created-using-content-api page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_17
// Download the helper library from https://www.twilio.com/docs/node/install
_17
// Find your Account SID and Auth Token at twilio.com/console
_17
// and set the environment variables. See http://twil.io/secure
_17
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_17
const authToken = process.env.TWILIO_AUTH_TOKEN;
_17
const client = require('twilio')(accountSid, authToken);
_17
_17
client.messages
_17
.create({
_17
contentSid: 'HX...XXX',
_17
from: 'MG...XXX',
_17
contentVariables: JSON.stringify({
_17
1: 'Name'
_17
}),
_17
to: 'whatsapp:+18551234567'
_17
})
_17
.then(message => console.log(message.sid));

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "Hello! 👍",
_24
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "MG...XXX",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "whatsapp:+18551234567",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

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.

content-api-quick-reply.

Submit a Template for WhatsApp Approval

submit-a-template-for-whatsapp-approval page anchor

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.

Submit a Content API Template for WhatsApp Approval

submit-a-content-api-template-for-whatsapp-approval page anchor
curl

_10
curl -X POST 'https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXX/ApprovalRequests/whatsapp' \
_10
-H 'Content-Type: application/json' \
_10
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN \
_10
-d '{
_10
"name": "flight_replies",
_10
"category": "UTILITY"
_10
}'

Output

_10
{
_10
"category": "TRANSPORTATION_UPDATE",
_10
"status": "received",
_10
"rejection_reason": "",
_10
"name": "flight_replies",
_10
"content_type": "twilio/quick-reply"
_10
}


Fetch an Approval Status

fetch-an-approval-status page anchor

Fetching an approval status returns the current state of the WhatsApp template approval submission. The following statuses are possible:

  1. Received : The request has successfully been submitted to Twilio. Generally Twilio submits these immediately to WhatsApp.
  2. Pending : WhatsApp has received the submission and is currently processing the request.
  3. Approved : WhatsApp has approved the template and it is now available for use.
  4. 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
(warning)

Warning

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.

Get Template Approval Status from Whatsapp

get-template-approval-status-from-whatsapp page anchor
curl

_10
curl -X GET 'https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXX/ApprovalRequests' \
_10
-H 'Content-Type: application/json' \
_10
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Output

_13
{
_13
"url": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXX/ApprovalRequests",
_13
"whatsapp": {
_13
"category": "TRANSPORTATION_UPDATE",
_13
"status": "pending",
_13
"name": "flight_replies",
_13
"type": "whatsapp",
_13
"content_type": "twilio/quick-reply",
_13
"rejection_reason": ""
_13
},
_13
"account_sid": "ACXXXXXXXXXXXXXXXXXXX",
_13
"sid": "HXXXXXXXXXXXXXXXXXXX"
_13
}


Next: Learn about Content Types

next-learn-about-content-types page anchor

Congratulations on successfully using the Content API! Next, we recommend checking out the supported Content Types. You can also review additional Content API Resources.


Rate this page: