Search code examples
twiliotwilio-functions

Using TwilioClient to execute Studio Flow from a Runtime Function


I have created a Twilio Studio Flow to make an outbound call that is triggered via REST API. I'd like to trigger this from a Twilio Runtime Function, but am having encountering errors when using the TwilioClient library.

Triggering the Studio Flow via the command line works successfully as follows:

curl -X POST "https://studio.twilio.com/v1/Flows/FWXXXXXXXXXXXXXXXXXXXXXXXXXX/Executions" -d "To=+1XXXXXXXXX" -d "From=+1XXXXXXXXXX" -u ACCOUNT_SID:AUTH_TOKEN

But trying to do the equivalent via a Runtime Function fails:

exports.handler = function(context, event, callback) {
  const twilioClient = context.getTwilioClient();
  console.log(twilioClient.studio);
  twilioClient.studio.flows('FWXXXXXXXXXXXXXXXXXXXXXXXXXX').executions.create({ 
    to: '+1XXXXXXXXX', 
    from: '+1XXXXXXXXXX', 
    parameters: JSON.stringify({
      name: "Client"
    })
  })
  .then(function(execution) { 
    console.log(execution.sid); 
    callback(null, execution.sid);        
  })
  .catch(error => {
    console.error(`problem with request: ${error.message}`);
    callback(error.message);
  });
};

The error encountered is:

{ message: 'Cannot read property \'flows\' of undefined',
  name: 'TypeError',
  stack: 'TypeError: Cannot read property \'flows\' of undefined\n    at Object.exports.handler (/var/task/handlers/ZF3ef70f4f38cfdf1c656da43214c01e18.js:6:19)\n    at Object.exports.handler (/var/task/node_modules/enigma-lambda/index.js:306:10)\n    at exports.handler (/var/task/enigma.js:17:9)' }

I have been playing around with it for a number of hours and don't seem to be any closer to a solution. Any help in pointing me in the right direction would be super appreciated!


Solution

  • Please make sure you are using a current version of the Twilio helper library. You can view your current versions under Twilio Functions, Configure: https://www.twilio.com/console/runtime/functions/configure (under: twilio), 3.6.3 is old.

    The latest version can be found here: https://github.com/twilio/twilio-node/releases (currently 3.31.0).