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

Send Templates Created with the Content Template Builder


To send messages from templates created using the Content Template Builder, you will need two new fields in your API request. You will also need to first configure a Messaging Service and create your content template.


New Content Fields Used to Send Messages

new-content-fields-used-to-send-messages page anchor

You will utilize the existing messaging endpoint with an additional field, ContentSid. You may also include the ContentVariables field to substitute any placeholder values in your templates.


_10
POST https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages

New FieldRequiredDescription
ContentSidYesString field used to identify the preconfigured content. Required to send templates created using the Content Template Builder.
ContentVariablesNoJSON string used to define the values of any placeholder variables found in the preconfigured content. Key-value pairs of placeholder variable names and substitution values.
(information)

Info

If you are sending a WhatsApp message outside the 24-hour free-form session, you need to use a template approved by WhatsApp. Learn more.


Use Messaging Services to Send Content Templates

use-messaging-services-to-send-content-templates page anchor

To send content templates, you'll need a Messaging Service. Messaging Services are also a great tool to organize your account and reduce complexity as your messaging application grows. Learn more about Messaging Services features here.

To create a messaging service, head to the Messaging > Services section of the Twilio Console. Check out the Quick Start for a detailed guide.

MSGSER1.

Now grab your Messaging Service's Sid by going to the Twilio Console > Services and finding the "Sid".

find-messaging-service-sid.

Next, we will review the two ways to send a template created with the Content Template Builder:

  • Include a MessagingServiceSid field, or
  • Use the From field to specify the Messaging Service Sid

Send Messages with a Messaging Service in the "From" Field

send-messages-with-a-messaging-service-in-the-from-field page anchor
(warning)

Warning

Body and MediaUrl should be excluded. They are both not required. They are both superseded by the ContentSid.

The body in the response will be empty. Since this is a templated message, the body is in the template. If you would like to see the body that was delivered they can view it in the Twilio logs in console.

Send Messages with a Messaging Service in the "From" field

send-messages-with-a-messaging-service-in-the-from-field-1 page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_21
// Download the helper library from https://www.twilio.com/docs/node/install
_21
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_21
_21
// Find your Account SID and Auth Token at twilio.com/console
_21
// and set the environment variables. See http://twil.io/secure
_21
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_21
const authToken = process.env.TWILIO_AUTH_TOKEN;
_21
const client = twilio(accountSid, authToken);
_21
_21
async function createMessage() {
_21
const message = await client.messages.create({
_21
contentSid: "HXXXXXXXXX",
_21
contentVariables: JSON.stringify({ 1: "Name" }),
_21
from: "MGXXXXXXXX",
_21
to: "whatsapp:+18551234567",
_21
});
_21
_21
console.log(message.body);
_21
}
_21
_21
createMessage();

Output

_28
{
_28
"account_sid": "ACXXXXXXXX",
_28
"api_version": "2010-04-01",
_28
"body": "Hello! 👍",
_28
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"direction": "outbound-api",
_28
"error_code": null,
_28
"error_message": null,
_28
"from": "MGXXXXXXXX",
_28
"num_media": "0",
_28
"num_segments": "1",
_28
"price": null,
_28
"price_unit": null,
_28
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"status": "queued",
_28
"subresource_uris": {
_28
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
_28
},
_28
"tags": {
_28
"campaign_name": "Spring Sale 2022",
_28
"message_type": "cart_abandoned"
_28
},
_28
"to": "whatsapp:+18551234567",
_28
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
_28
}

This method requires adding a Sender to a Messaging Service. This approach is recommended if you plan to scale your traffic with multiple senders.


How to Add Senders to a Your Sender Pool

how-to-add-senders-to-a-your-sender-pool page anchor

Each Messaging Service has a "Sender Pool" that contains all the senders that can access that service's configurations. To add a sender to a Messaging Service, you need to add it to its Sender Pool. Go to the Messaging Service, find the "Sender Pool" Section on the left and click "Add Sender".

add-sender-to-sender-pool.

Messaging Services support Phone Numbers, Short Codes, Alpha Senders, WhatsApp Senders and Facebook Messenger Senders. If you do not see an option for FB Messenger, file a support ticket asking to enable the "FBM with messaging services feature". Read our Facebook Messenger with Twilio documentation for more information.

Here is a summary of all the fields used in the API request:

FieldRequiredDescription
FromYesThe Messaging Service sid, MGXXXXXXXX.
ToYesThe identifier of the recipient you are sending the message to. Use e.164 format for phone numbers, "whatsapp:e.164" for WhatsApp, and "messenger:{messenger_id}" for FB Messenger.
ContentSid new-String field used to identify the preconfigured content. Required to send templates created using Content Template Builder.
ContentVariables newNoJSON string used to define the values of any placeholder variables found in the preconfigured content. Key-value pairs of placeholder variable names and substitution values.

Send Messages with a MessagingServiceSid Field

send-messages-with-a-messagingservicesid-field page anchor
(warning)

Warning

Body and MediaUrl should be excluded. They are both not required. They are both superseded by the ContentSid

Send Messages with a MessagingServiceSid Field

send-messages-with-a-messagingservicesid-field-1 page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_22
// Download the helper library from https://www.twilio.com/docs/node/install
_22
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_22
_22
// Find your Account SID and Auth Token at twilio.com/console
_22
// and set the environment variables. See http://twil.io/secure
_22
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_22
const authToken = process.env.TWILIO_AUTH_TOKEN;
_22
const client = twilio(accountSid, authToken);
_22
_22
async function createMessage() {
_22
const message = await client.messages.create({
_22
contentSid: "HXXXXXXXXX",
_22
contentVariables: JSON.stringify({ 1: "Name" }),
_22
from: "whatsapp:+15551234567",
_22
messagingServiceSid: "MGXXXXXXXX",
_22
to: "whatsapp:+18551234567",
_22
});
_22
_22
console.log(message.body);
_22
}
_22
_22
createMessage();

Output

_28
{
_28
"account_sid": "ACXXXXXXXXX",
_28
"api_version": "2010-04-01",
_28
"body": "Hello! 👍",
_28
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"direction": "outbound-api",
_28
"error_code": null,
_28
"error_message": null,
_28
"from": "whatsapp:+15551234567",
_28
"num_media": "0",
_28
"num_segments": "1",
_28
"price": null,
_28
"price_unit": null,
_28
"messaging_service_sid": "MGXXXXXXXX",
_28
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"status": "queued",
_28
"subresource_uris": {
_28
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
_28
},
_28
"tags": {
_28
"campaign_name": "Spring Sale 2022",
_28
"message_type": "cart_abandoned"
_28
},
_28
"to": "whatsapp:+18551234567",
_28
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
_28
}

This method lets you keep using the From field to specify the Sender ID. You need to reference a Messaging Service by including a new MessagingServiceSid field. Note that while a Messaging Service is required, you do not have to add the sender to its Sender Pool (more on this later). This may be an easier way to get started if you are new to Messaging Services. Here are all the fields used in the API request:

FieldRequiredDescription
FromYesThe sender that is initiating the message. Use e.164 format for phone numbers, "whatsapp:e.164" for WhatsApp, and "messenger:{messenger_id}" for FB Messenger.
ToYesThe identifier of the recipient you are sending the message to. Use e.164 format for phone numbers, whatsapp:e.164 for WhatsApp, and messenger:{messenger_id} for FB Messenger.
MessagingServiceSid newYesThe Messaging Service Sid, MGXXXXXXXX.
ContentSid newYesString field used to identify the preconfigured content. Required to send templates created using the Content Template Builder.
ContentVariables newNoJSON string used to define the values of any placeholder variables found in the preconfigured content. Key-value pairs of placeholder variable names and substitution values.

Rate this page: