hrubasek
hrubasek2mo ago

Quotation marks api call fail

Hey guys. Can someone help me how to setup my agent just before the Api call step I need to make sure that my API call doesnt fail when user writes quotation marks. Thanks
3 Replies
Tico | Voiceflow Assistant
Here are the two easiest ways to protect your API call from breaking when the user’s message contains quotation marks ( " or ' ). 1️⃣ Let Voiceflow do the escaping for you (JSON body) • In the API step set the Body type to JSON.
• Add your variable as a value, without wrapping it in quotes:
{
"userText": {{user_input}}
}
{
"userText": {{user_input}}
}
When the request is sent Voiceflow serialises the body with JSON.stringify(), so any quotes inside user_input are automatically escaped (e.g. He said "hi").
2️⃣ Pre-process the text yourself (works for query-strings, headers, or raw bodies) Add a Function step just before the API step and create an “escaped” version of the text:
// input variable: user_input
// output variable: safe_text
export default async function main(args) {
const { user_input } = args.inputVars;
const safe_text = user_input
.replace(/\\/g, "\\\\") // escape backslashes first
.replace(/"/g, "\\\"") // escape double quotes
.replace(/'/g, "\\'"); // (optional) escape single quotes

return { outputVars: { safe_text } }; // use {{safe_text}} in your API step
}
// input variable: user_input
// output variable: safe_text
export default async function main(args) {
const { user_input } = args.inputVars;
const safe_text = user_input
.replace(/\\/g, "\\\\") // escape backslashes first
.replace(/"/g, "\\\"") // escape double quotes
.replace(/'/g, "\\'"); // (optional) escape single quotes

return { outputVars: { safe_text } }; // use {{safe_text}} in your API step
}
If the text is going into a URL query parameter you can instead encode it:
const safe_text = encodeURIComponent(user_input);
const safe_text = encodeURIComponent(user_input);
3️⃣ Use the new value in the API step Replace every occurrence of {{user_input}} with {{safe_text}} (or whatever name you chose).
With either approach the request that reaches your backend will always be valid, even when the user types:
She answered: "I’m in!" 😄
Docs: https://docs.voiceflow.com
hrubasek
hrubasekOP2mo ago
I would appreciate getting more help not just from AI thanks
W. Williams (SFT)
you just need to JSON.stringify() the var.

Did you find this page helpful?