Why does Twilio REST API v2 keep giving me a 400 Bad Request error?

668 Views Asked by At

I keep receiving 400 Bad Request errors from Twilio REST API. My only requirement is that I have to use the fetch because I will be running this code in a Zapier code block.

I've rewritten the following code a bajillion different ways and cannot seem to make the request work. I feel like I might just be missing something very obvious. Please help, Stack Overflow! You're my only hope.

const func = async () => {
  var url = "https://studio.twilio.com/v2/Flows/{Flow ID}/Executions";
  var phone = "+1" + "{phone num}";
  phone = phone.replace(/-/g,'');
  console.log('running');

  output = await fetch(url, {
    method: 'POST',
    body: JSON.stringify({
      To: phone,
      From: '+1{phone num}',
      // Parameters: JSON.stringify({
      //   item_num: "A11113232",
      //   agent: "Joel"
      //   // driver_num: '1',
      // }),
    }),
    headers: {
      'Authorization': 'Basic '+Buffer.from('{SID:Secret}').toString('base64'),
      'Content-Type': 'multipart/form-data',
      'Cache-Control': 'no-cache'
      },
    }).catch((err)=>console.log(err));

  console.log(output.status);
//} 
}

func();
1

There are 1 best solutions below

8
Jeremy Fiel On

it's not very clear why they have two versions of the api both using v2 but it appears you may be using the wrong body parameters. the 400 response means you sent some bad data in the request body.

Try using to: and from: (lowercase)

source: https://www.twilio.com/docs/studio/rest-api/v2/execution#create-a-new-execution