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}}.
"body": "Hi {{1}}, Your flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
"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.
"body": "Hi {{1}} {{2}}, flight will depart from gate {{3}}. Please reply Stop to unsubscribe."
"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.
"body": "Hi {{1}}, flight will depart from gate {{2}}."
"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.
"body": "Hi {{1}}, gate {{2}}. Thank you."
"body": "Hi {{1}}, Your flight will depart from gate {{2}}. Thank you."
Variables do allow for non-numeric variables. Variable names cannot have spaces
{{id name}}
{{IdName}}
For templates sent in session a few extra features are available.
1curl -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}'
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}
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.
1"variables": {2"1": "coupon_code",3"2": "docs",4"3": "images/library-logo-resource2x.width-1000.png"5},
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.
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.
You can also use the notation below at time of send.
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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createMessage() {11const message = await client.messages.create({12contentSid: "HXXXXXXXXXXX",13contentVariables: JSON.stringify({141: "coupon_code",152: "docs",163: "images/library-logo-resource2x.width-1000.png",17}),18from: "MGXXXXXXXXXX",19to: "whatsapp:+18581234567",20});2122console.log(message.body);23}2425createMessage();
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": "whatsapp:+18581234567",27"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"28}