Question

Code that works locally breaks without error code in Zapier code block (Twilio API call)

  • 21 September 2023
  • 2 replies
  • 28 views

The code I currently have in my Zap Code action block works perfectly fine locally. When I test it within Zapier, I am getting console logs that indicate it is running and the fetch Promise is resolving. However, it is not actually triggering the API. Why would this same code trigger the API locally but not from within Zapier?

 

const func = async () => {
var url = "{url}";
var phone = "+1" + "{number}";
phone = phone.replace(/-/g,'');
console.log('running');

output = await fetch(url, {
method: 'POST',
body: new URLSearchParams({
To: phone,
From: '{MessagingSID}',
Parameters: JSON.stringify({
item_num: "test",
agent: "test",
// driver_num: 'test,
city: 'test'
}),
}),
headers: {
'Authorization': 'Basic '+Buffer.from('{Token:Secret}').toString('base64'),
'Cache-Control': 'no-cache'
},
}).catch((err)=>console.log(err.body));

console.log(output);
//}
}

func();

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @jh123 

Good question.

Help articles about using the Code app: https://zapier.com/apps/code/help

Perhaps try using the Webhooks app: https://zapier.com/apps/webhook/help

 

Userlevel 6
Badge +8

Try this instead:

var url = "{url}";
var phone = "+1" + "{number}";
phone = phone.replace(/-/g, '');
console.log('running');

const twilioResp = await fetch(
url, {
method: 'POST',
body: new URLSearchParams({
'To': phone,
'From': '{MessagingSID}',
'Parameters': JSON.stringify({
'item_num': "test",
'agent': "test",
// 'driver_num': 'test',
'city': 'test'
}),
}),
headers: {
'Authorization': 'Basic ' + Buffer.from('{Token:Secret}').toString('base64'),
'Cache-Control': 'no-cache'
},
}
).catch((err) => console.log(err.body));

var data = await twilioResp.text();

output = [{ data }];