Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Using Variables with Content Templates


Creating templates using variables in Content API

creating-templates-using-variables-in-content-api page anchor

You can add variables to any template that you create with the Content Template Builder. Variables are supported across all channels.

  • Variables can be numeric or alphanumeric, but they can't contain spaces.
    • Not allowed: {{id name}}
    • Allowed: {{IdName}}
  • A single template can contain up to of 100 variables.
  • A variable key has a maximum length of 16 characters.
  • A variable value can be up to 1,600 characters, but we recommend using less than 250 characters. Some fields have lower maximum characters and the variable maximum for those fields allign with the field maximum.

Variable formatting

variable-formatting page anchor

In the following example, the block of code corresponds to variable samples and also the default variable definition. This is what your variables will fall back to when sending if variables aren't defined at time of send. URL path samples must resolve to a publicly hosted media/url sample.

1
"variables": {
2
"1": "coupon_code",
3
"2": "docs",
4
"3": "library-logo-resource2x.width-1000.png"
5
},
Using Content VariablesLink to code sample: Using Content Variables
1
curl -X POST 'https://content.twilio.com/v1/Content' \
2
-H 'Content-Type: application/json' \
3
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN \
4
-d '{
5
"friendly_name": "Owl Air elite status card template",
6
"language": "en",
7
"variables": {
8
"1": "coupon_code",
9
"2": "docs",
10
"3": "library-logo-resource2x.width-1000.png"
11
},
12
"types": {
13
"twilio/card": {
14
"title": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off.",
15
"subtitle": "To unsubscribe, reply Stop",
16
"actions": [
17
{
18
"url": "https://twilio.com/{{2}}",
19
"title": "Order Online",
20
"type": "URL"
21
}
22
],
23
"media": ["https://raw.githubusercontent.com/twilio-samples/api-snippets/refs/heads/main/_images/{{3}}"]
24
},
25
"twilio/text": {
26
"body": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
27
}
28
}
29
}'

Output

1
{
2
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
3
"date_created": "2022-11-17T08:52:12Z",
4
"date_updated": "2022-11-17T08:52:12Z",
5
"friendly_name": "Owl Air elite status card template",
6
"language": "en",
7
"links": {
8
"approval_create": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests/whatsapp",
9
"approval_fetch": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ApprovalRequests"
10
},
11
"sid": "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
12
"types": {
13
"twilio/card": {
14
"actions": [
15
{
16
"title": "Order Online",
17
"type": "URL",
18
"url": "https://owlair.example.com/{{2}}"
19
}
20
],
21
"body": null,
22
"media": [
23
"https://raw.githubusercontent.com/twilio-samples/api-snippets/refs/heads/main/_images/{{3}}"
24
],
25
"subtitle": "To unsubscribe, reply Stop",
26
"title": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
27
},
28
"twilio/text": {
29
"body": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
30
}
31
},
32
"url": "https://content.twilio.com/v1/Content/HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
33
"variables": {
34
"1": "coupon_code",
35
"2": "docs",
36
"3": "library-logo-resource2x.width-1000.png"
37
}
38
}

Creating templates using variables in Content Template Builder in Console

creating-templates-using-variables-in-content-template-builder-in-console page anchor

To add a variable, either use the + Add Variable button to add a variable at the end of the field's string, or you can manually type in a variable.

Variables added to templates which may require samples, request samples and default variables. Samples might be required depending on the template type.


Sending templates using variables with Content Template Builder

sending-templates-using-variables-with-content-template-builder page anchor

Use the following notation at time of sending.

1
"variables": {
2
"1": "coupon_code",
3
"2": "docs",
4
"3": "library-logo-resource2x.width-1000.png"
5
},
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createMessage() {
11
const message = await client.messages.create({
12
contentSid: "HXXXXXXXXXXX",
13
contentVariables: JSON.stringify({
14
1: "coupon_code",
15
2: "docs",
16
3: "library-logo-resource2x.width-1000.png",
17
}),
18
from: "MGXXXXXXXXXX",
19
to: "+18581234567",
20
});
21
22
console.log(message.body);
23
}
24
25
createMessage();

Response

Note about this response
1
{
2
"account_sid": "ACXXXXXXXXX",
3
"api_version": "2010-04-01",
4
"body": "Hello! 👍",
5
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
6
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
7
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
8
"direction": "outbound-api",
9
"error_code": null,
10
"error_message": null,
11
"from": "MGXXXXXXXXXX",
12
"num_media": "0",
13
"num_segments": "1",
14
"price": null,
15
"price_unit": null,
16
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"status": "queued",
19
"subresource_uris": {
20
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
21
},
22
"to": "+18581234567",
23
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
24
}

There are rules for variables sent in a WhatsApp user and business initiated session. For templates sent in session, a few extra features are available.

  • Quick reply templates can set variable titles.
  • Since Lists are in-session only on WhatsApp, they can be fully configurable with Variables.

To send templates out of session through WhatsApp, follow these rules:

  • Content variables in approved WhatsApp templates can't contain newlines.
  • Content variables need to be in sequential order. They can be is non-sequential order within a template, but variable definitions should not skip over integers. For example, Meta does not allow {{1}} words {{3}} but they allow {{1}} words {{3}} words {{2}}.
    • Not allowed: "body": "Hi {{1}}, Your flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
    • Allowed: "body": "Hi {{1}}, Your flight will depart from gate {{2}}. Please reply Stop to unsubscribe."
  • Content variables shouldn't be right next to each other. Meta considers variables separated only by a space as next to each other. Additionally, there needs to be characters separated by a space between the variables.
    • Bad practice: "body": "Hi {{1}} {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
    • Best practice: "body": "Hi {{1}} and {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
  • Content variables shouldn't start or end a body string without a sample. Meta considers variables strings ending in a variable followed by punctuation as a variable at the end of a string. There will need to be extra text after the variable.
    • Bad practice: "body": "Hi {{1}}, flight will depart from gate {{2}}."
    • Best practice: "body": "Hi {{1}}, flight will depart from gate {{2}}. Please reply Stop to unsubscribe."
  • Templates can't have too many variables relative to the message length. As a general rule, for every 'x' variables, there must be 2x+1 non-variable words. Here, words are defined as characters separated by spaces. There is also a limit of 100 variables per template.
    • Not allowed: "body": "Hi {{1}}, gate {{2}}. Thank you."
    • Allowed: "body": "Hi {{1}}, Your flight will depart from gate {{2}}. Thank you."
    • URL variable must be preceded with a slash. WhatsApp fails URLs that do not contain a slash after the core domain as an invalid URL.