1

I am calling a Twilio Studio Flow Rest API from a Twilio function.

Following is the function code:

exports.handler = function(context, event, callback) {
   const request = require('request');  
   to = event.to;
   to = to.replace(/[()-.]/g, '');
   to = to.replace(/ /g, '');
   var postoptions = {  
   headers: {'AC' : 'b1xx'},       
   url:    'https://studio.twilio.com/v1/Flows/FWxxx8/Executions',  
        method: 'POST',  
        data: { 'from':  '+1814xxx',  
                'to': to 
            }  
        };

         request(postoptions, function(error, response, body){  
             callback(null, response);
         });
};

The function keeps saying the Account SID adn auth token are incorrect. However sid and token are correct.

What I am missing?

arun kumar
  • 703
  • 2
  • 13
  • 33

1 Answers1

0

You'll need to "base64" encode the Account SID and auth token when you pass them in headers.

Try something like this:


exports.handler = function (context, event, callback) {
    const request = require('request');
    to = event.to;
    to = to.replace(/[()-.]/g, '');
    to = to.replace(/ /g, '');

    var username = "AC";
    var password = "b1xx";
    var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

    var postoptions = {
        headers: { 'Authorization': auth },
        url: 'https://studio.twilio.com/v1/Flows/FWxxx8/Executions',
        method: 'POST',
        data: {
            'from': '+1814xxx',
            'to': to
        }
    };

    request(postoptions, function (error, response, body) {
        callback(null, response);
    });
};

Alex Baban
  • 11,312
  • 4
  • 30
  • 44