Skip to contentSkip to navigationSkip to topbar
On this page

Using Variables with Content Templates



Creating Templates Using Variables in Content API

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

Variables can be used in all templates created with the Content Template Builder. Variables are supported across all channels.

  • Variables can be numeric or non-numeric. Variable names can't have spaces.

    • Not allowed: {{id name}}
    • Allowed: {{IdName}}

Variable formatting

variable-formatting page anchor

In the code sample shown, the block of code below 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": "images/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": "images/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://twilio-cms-prod.s3.amazonaws.com/{{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://twilio-cms-prod.s3.amazonaws.com/{{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": "images/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, you can 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.

ContentEditorNewvar.

Variables added on templates where samples may be required will ask for samples and default variables. Samples may be required depending on the template type.

variable_modal.

Sending Templates Using Variables with Content Template Builder

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

Use the notation below at time of sending.

1
"variables": {
2
"1": "coupon_code",
3
"2": "docs",
4
"3": "images/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: "images/library-logo-resource2x.width-1000.png",
17
}),
18
from: "MGXXXXXXXXXX",
19
to: "+18581234567",
20
});
21
22
console.log(message.body);
23
}
24
25
createMessage();

Output

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
"tags": {
23
"campaign_name": "Spring Sale 2022",
24
"message_type": "cart_abandoned"
25
},
26
"to": "+18581234567",
27
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
28
}

There are a few differing 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.

For templates sent out of session through WhatsApp the following rules apply:

  • 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 can't be right next to each other without a sample. Meta considers variables separated only by a space as next to each other. Additionally, there need to be characters separated by a space between the variables.

    • Example that would need a sample: "body": "Hi {{1}} {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
    • Example that wouldn't need a sample: "body": "Hi {{1}} and {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
  • Content variables can'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.

    • Example that would need a sample: "body": "Hi {{1}}, flight will depart from gate {{2}}."
    • Example that wouldn't need a sample: "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."

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.