Personalization
Each Content Template is identified by a unique HXXXXXX SID. Use this API to customize bulk messages for each recipient.
Reference template variables by their positional index (starting at 1) within the Content Template.
1curl -X POST 'https://comms.twilio.com/preview/Messages' \2--header 'Content-Type: application/json' \3--data '{4"from": {5"address": "<Your Purchased Twilio Phone Number>",6"channel": "SMS"7},8"to": [9{10"address": "+18015679900",11"channel": "phone",12"variables": {13"1": "Fred"14}15},16{17"address": "+19143188062",18"channel": "phone",19"variables": {20"1": "Sonny"21}22}23],24"content": {25"contentId": "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"26}27}' \28-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
Alternatively, you can define and personalize message content inline. Within the content, enclose each variable name in double curly braces ({{ }}). Provide a default value for every variable to ensure that the message renders correctly when a recipient's variables don't contain the corresponding data.
1curl -X POST 'https://comms.twilio.com/preview/Messages' \2--header 'Content-Type: application/json' \3--data '{4"from": {5"address": "<Your Purchased Twilio Phone Number>",6"channel": "sms"7},8"to": [9{10"address": "+18015679900",11"channel": "PHONE",12"variables": {13"name": "Fred"14}15},16{17"address": "+19143188062",18"channel": "PHONE",19"variables": {20"name": "Sonny"21}22}23],24"content": {25"text": "Hello {{name | default: 'there'}}!"26}27}' \28-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
The Twilio Communications API supports Liquid for advanced message templating.
In addition to simple variable replacement, you can use control flow structures like if and unless to conditionally render parts of a message for specific recipients.
1curl -X POST 'https://comms.twilio.com/preview/Messages' \2--header 'Content-Type: application/json' \3--data '{4"from": {5"address": "<Your Purchased Twilio Phone Number>",6"channel": "SMS"7},8"to": [9{10"address": "+18015679900",11"channel": "PHONE",12"variables": {13"name": "Fred",14"favoriteColor": "grey"15}16},17{18"address": "+19143188062",19"channel": "PHONE",20"variables": {21"name": "Sonny",22"favoriteColor": "red"23}24}25],26"content": {27"text": "Hello {{name | default: 'there'}}! {% if favColor == '\''red'\''%} My favorite color is {{favoriteColor}} too! {% endif %}"28}29}' \30-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
To deliver engaging media-rich messages, see Rich Content.