Scott
Scott

Reputation: 1247

Twilio: How to initiate a flow with an API call

I'm trying to initiate a flow on Twilio via an API call in Node, and I can't seem to get it to work. The flow is designed to make an outbound call when the API request is sent. Have tried several code examples I've seen online to no avail. I'm getting an error like Cannot read property X of undefined (see below), but the thing is I'm able to initiate a phone call (not a flow, just a call) to my cell phone via an API call, so I know the Twilio client is connected.

Works:

app.post('/call', (req, res) => {
  client.calls
  .create({
     url: 'https://handler.twilio.com/twiml/PNxxxxxxxxxxxxxxxxxxxx',
     to: '+1708xxxxxxx',
     from: '+1312xxxxxxx'
   })
  .then((call, err) => {
      if (err) { console.log(err) }
      res.json({ success: "success" });
    });
  });

Not working: Triggers Cannot read property 'v1' of undefined

app.post('/flow', (req, res) => {

    client.studio.v1.flows('FWxxxxxxxxxxxxxxxxxxxx')
    .fetch()
    .then(flow => console.log("flow : ", flow));

Not working: Triggers Cannot read property 'flows' of undefined

app.post('/flow', (req, res) => {
  client.studio.flows('FWxxxxxxxxxxxxxxxxxxxx')
  .executions
  .create({
    to: '+1847xxxxxxx',
    from: '+1312xxxxxxx'
  })
  .then(function(execution) { console.log("sid : ", execution.sid); });
});

Not working: No error, just nothing happens

app.post('/flow', (req, res) => {

  client.calls
  .create({
    url: 'https://studio.twilio.com/v1/Flows/FWxxxxxxxxxxxxxxxxxxxx/Executions',
    to: '+1847xxxxxxx',
    from: '+1312xxxxxxx'
  })
  .then((call, err) => {
    if (err) { console.log("err : ", err) }
    if (call) { console.log("call : ", call)}
    res.json({ success: "success" });
  });
});

Upvotes: 1

Views: 890

Answers (2)

Alex Baban
Alex Baban

Reputation: 11702

It is most likely that you're using an old version of Twilio's Node.js library without support for Studio flows. The current version is 3.39.1

You can find the version you're using if you read "dependencies" in the package.json file.

Also, if you open a terminal in your project root folder and run npm outdated you'll probably see twilio colored red in that table.


How to fix:

There might be other ways to do this but to get the latest version just for Twilio's package I would open a terminal in the project's root folder and

  • run npm uninstall twilio --save
  • then run npm install twilio --save

After that check again with npm list --depth=0 and hopefully you'll get -- [email protected] which has support for Studio flows.

Upvotes: 1

Robert Moore
Robert Moore

Reputation: 21

https://www.twilio.com/docs/libraries/node

I would install the twilio into your project.

From console -This adds the packages into your package.json and downloads the modules npm install --save twilio


var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Your Account SID from www.twilio.com/console
var authToken = 'your_auth_token';   // Your Auth Token from www.twilio.com/console

var twilio = require('twilio');
var client = new twilio(accountSid, authToken);

client.messages.create({
    body: 'Hello from Node',
    to: '+12345678901',  // Text this number
    from: '+12345678901' // From a valid Twilio number
})
.then((message) => console.log(message.sid));```


when using the twilio node package they also have info on making calls with it
https://www.twilio.com/docs/voice/quickstart/node

// Download the helper library from https://www.twilio.com/docs/node/install
// Your Account Sid and Auth Token from twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.calls
      .create({
         url: 'http://demo.twilio.com/docs/voice.xml',
         to: '+123456789',
         from: '+987654321'
       })
      .then(call => console.log(call.sid));

Upvotes: 0

Related Questions