Personalization

Personalization

Content templates

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.

$curl -X POST 'https://comms.twilio.com/preview/Messages' \
>--header 'Content-Type: application/json' \
>--data '{
> "from": {
> "address": "<Your Purchased Twilio Phone Number>",
> "channel": "SMS"
> },
> "to": [
> {
> "address": "+18015679900",
> "channel": "phone",
> "variables": {
> "1": "Fred"
> }
> },
> {
> "address": "+19143188062",
> "channel": "phone",
> "variables": {
> "1": "Sonny"
> }
> }
> ],
> "content": {
> "contentId": "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
> }
>}' \
>-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Personalize messages inline

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.

$curl -X POST 'https://comms.twilio.com/preview/Messages' \
>--header 'Content-Type: application/json' \
>--data '{
> "from": {
> "address": "<Your Purchased Twilio Phone Number>",
> "channel": "sms"
> },
> "to": [
> {
> "address": "+18015679900",
> "channel": "PHONE",
> "variables": {
> "name": "Fred"
> }
> },
> {
> "address": "+19143188062",
> "channel": "PHONE",
> "variables": {
> "name": "Sonny"
> }
> }
> ],
> "content": {
> "text": "Hello {{name | default: '\''there'\''}}!"
> }
>}' \
>-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Advanced templating

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.

$curl -X POST 'https://comms.twilio.com/preview/Messages' \
>--header 'Content-Type: application/json' \
>--data '{
> "from": {
> "address": "<Your Purchased Twilio Phone Number>",
> "channel": "SMS"
> },
> "to": [
> {
> "address": "+18015679900",
> "channel": "PHONE",
> "variables": {
> "name": "Fred",
> "favoriteColor": "grey"
> }
> },
> {
> "address": "+19143188062",
> "channel": "PHONE",
> "variables": {
> "name": "Sonny",
> "favoriteColor": "red"
> }
> }
> ],
> "content": {
> "text": "Hello {{name | default: '\''there'\''}}! {% if favoriteColor == '\''red'\''%} My favorite color is {{favoriteColor}} too! {% endif %}"
> }
>}' \
>-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Next steps

To deliver engaging media-rich messages, see Rich Content.