node.jstwiliotwilio-programmable-chattwilio-node

Twilio Content Builder API Error 21656: Invalid ContentVariables when values contain apostrophes


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:

Any help would be appreciated!


Solution

  • Not exactly a direct answer, but a possible work around. I found that sending through a right single quotation mark. So this ’ in place of the straight apostrophe '

    Allowed my messages to go through.

    So sending content variables like

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

    Seem crazy, yes, it does to me too. But without reasonable documentation on how to escape things, it's the best I could do.