Using Variables with Content Templates
Creating Templates Using Variables in Content API
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."
- Not Allowed:
- Content variables cannot be right next to each other without a sample. Meta considers variables separated by only a spaces 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."
- Example that would need a sample:
- 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."
- Example that would need a sample:
- 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."
- Not Allowed:
- 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.
Variable Formatting
In the code sample shown, the block of code below corresponds to variable samples and also the 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.
"variables": {
"1": "coupon_code",
"2": "docs",
"3": "images/library-logo-resource2x.width-1000.png"
},
Creating Templates Using Variables in Content Template Builder in Console
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.
Sending Templates using Variables with Content Template Builder
You can also use the notation below at time of send.
"variables": {
"1": "coupon_code",
"2": "docs",
"3": "images/library-logo-resource2x.width-1000.png"
},
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.