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!
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.