Skip to contentSkip to navigationSkip to topbar
Rate this page:
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 on all templates built with Content Template Builder. There are a few differing rules for variables sent in a user and business initiated session.

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 cannot be right next to each other without a sample. Meta considers variables separated only by a space as next to each other. There additionally needs to be characters separated by a space between variables.

    • Example that would need a sample: "body": "Hi {{1}} {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
    • Example that would not need a sample: "body": "Hi {{1}} and {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
  • Content variables cannot be the start or end of a body string without a sample. Meta considers variables strings ending in a variable + 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 would not need a sample: "body": "Hi {{1}}, flight will depart from gate {{2}}. Please reply Stop to unsubscribe."
  • Templates cannot have too many variables relative to the message length. The general rule observed for this is for every 'x' variables there needs to be 2x+1 non variable words. Words being defined as characters separated by a space. 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."
  • Variables do allow for non-numeric variables. Variable names cannot have spaces

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

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.

Using Content Variables

using-content-variables page anchor
curl

_29
curl -X POST 'https://content.twilio.com/v1/Content' \
_29
-H 'Content-Type: application/json' \
_29
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN \
_29
-d '{
_29
"friendly_name": "docs_card",
_29
"language": "en",
_29
"variables": {
_29
"1": "coupon_code",
_29
"2": "docs",
_29
"3": "images/library-logo-resource2x.width-1000.png"
_29
},
_29
"types": {
_29
"twilio/card": {
_29
"title": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off.",
_29
"subtitle": "To unsubscribe, reply Stop",
_29
"actions": [
_29
{
_29
"url": "https://twilio.com/{{2}}",
_29
"title": "Order Online",
_29
"type": "URL"
_29
}
_29
],
_29
"media": ["https://twilio-cms-prod.s3.amazonaws.com/{{3}}"]
_29
},
_29
"twilio/text": {
_29
"body": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
_29
}
_29
}
_29
}'

Output

_38
{
_38
"language": "en",
_38
"date_updated": "2022-11-17T08:52:12Z",
_38
"variables": {
_38
"1": "coupon_code",
_38
"3": "images/library-logo-resource2x.width-1000.png",
_38
"2": "docs"
_38
},
_38
"friendly_name": "docs_card",
_38
"account_sid": "ACXXX..XX",
_38
"url": "https://content.twilio.com/v1/Content/HX...XXX",
_38
"sid": "HX...XXX",
_38
"date_created": "2022-11-17T08:52:12Z",
_38
"types": {
_38
"twilio/text": {
_38
"body": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
_38
},
_38
"twilio/card": {
_38
"body": null,
_38
"media": [
_38
"https://twilio-cms-prod.s3.amazonaws.com/{{3}}"
_38
],
_38
"subtitle": "To unsubscribe, reply Stop",
_38
"actions": [
_38
{
_38
"url": "https://twilio.com/{{2}}",
_38
"type": "URL",
_38
"title": "Order Online"
_38
}
_38
],
_38
"title": "Congratulations, you've reached Elite status! Add code {{1}} for 10% off."
_38
}
_38
},
_38
"links": {
_38
"approval_fetch": "https://content.twilio.com/v1/Content/HX...XXX/ApprovalRequests",
_38
"approval_create": "https://content.twilio.com/v1/Content/HX...XXX/ApprovalRequests/whatsapp"
_38
}
_38
}


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. When submitting a template for approval by WhatsaApp, it is also what will be used as a sample per WhatsApp's requirements. URL path samples must resolve to a publicly hosted media/url sample.


_10
"variables": {
_10
"1": "coupon_code",
_10
"2": "docs",
_10
"3": "images/library-logo-resource2x.width-1000.png"
_10
},


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 that will help your template get approved for sending. 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

You can also use the notation below at time of send.


_10
"variables": {
_10
"1": "coupon_code",
_10
"2": "docs",
_10
"3": "images/library-logo-resource2x.width-1000.png"
_10
},

Sending with Content Variables

sending-with-content-variables page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_19
// Download the helper library from https://www.twilio.com/docs/node/install
_19
// Find your Account SID and Auth Token at twilio.com/console
_19
// and set the environment variables. See http://twil.io/secure
_19
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_19
const authToken = process.env.TWILIO_AUTH_TOKEN;
_19
const client = require('twilio')(accountSid, authToken);
_19
_19
client.messages
_19
.create({
_19
contentSid: 'HXXXXXXXXXXX',
_19
from: 'MGXXXXXXXXXX',
_19
contentVariables: JSON.stringify({
_19
1: 'coupon_code',
_19
2: 'docs',
_19
3: 'images/library-logo-resource2x.width-1000.png'
_19
}),
_19
to: 'whatsapp:+18581234567'
_19
})
_19
.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": "MGXXXXXXXXXX",
_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:+18581234567",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}


Rate this page: