Twilio Content Builder API Error 21656: Invalid ContentVariables when values contain apostrophes
21:54 01 Apr 2025

I'm encountering Twilio error 21656 ("The ContentVariables Parameter is invalid") after migrating to the Content Builder API for template messages. The error occurs specifically when one of my content variable values contains an apostrophe, like people's. My Code:

async sendWhatsappViaContentId({ from, to, contentId, jsonData, additionalSettings = {} }) {
  return this.client.messages.create({
    from: `whatsapp:${from}`,
    to: `whatsapp:${to}`,
    contentSid: contentId,
    contentVariables: JSON.stringify(jsonData),
    statusCallback: additionalSettings?.ignoreCallback ? this.ignoreStatusCallbackUrl : this.statusCallbackUrl,
    ...(additionalSettings.customCallback && { statusCallback: additionalSettings.customCallback }),
  });
}

Example JSON Data Causing the Error:

{
  "1": "aaasdasd",
  "2": "people's"
}

Questions:

  • How can I properly handle apostrophes in content variable values?

  • What other special characters might cause this error?

  • Is there a recommended way to escape or encode these characters when using the Content Builder API?

Any help would be appreciated!

node.js twilio twilio-programmable-chat twilio-node