The Twilio Node Helper Library

The twilio-node helper library lets you write Node.js code to make HTTP requests to the Twilio API.

This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-node project hosted on Github.

More Documentation

The Rest API Documentation has examples written in Node.js for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the Node.js library.

You may also want to refer to the twilio-node specific documentation for more details about the functions contained in the module.

Installation

The easiest way to install twilio-node is from NPM. You can run the command below from your project directory to install the library:

npm install twilio

Or, you can clone the source code for twilio-node, and install the library from there.

git clone https://github.com/twilio/twilio-node
cd twilio-node
npm install  # install twilio-node's dependencies

Then in your code:

var twilio = require("path/to/twilio-node/lib");

Testing your installation

Try sending yourself an SMS message, like this:

var accountSid = '{{ account_sid }}'; // Your Account SID from www.twilio.com/console
var authToken = '{{ auth_token }}';   // Your Auth Token from www.twilio.com/console

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

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

Using the Module

Refer to the twilio-node specific documentation for details about the functions contained in the module.

Handling Exceptions

If the Twilio API returns a 400 or a 500 level HTTP response, the twilio-node library will include information in the error parameter of the callback. 400-level errors are normal during API operation ("Invalid number", "Cannot deliver SMS to that number", for example) and should be handled appropriately.

var accountSid = '{{ account_sid }}'; // Your Account SID from www.twilio.com/console
var authToken = '{{ auth_token }}';   // Your Auth Token from www.twilio.com/console

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

client.messages.create({
    body: 'Hello from Node',
    to: '+12345678901',  // Text this number
    from: '+12345678901' // From a valid Twilio number
}, function(err, message) {
    if(err) {
        console.error(err.message);
    }
});

Getting help

Still running into problems?

Installing the Next-Gen Version

The "next-gen" version of the Twilio library will markedly improve the developer experience. As we put the finishing touches on this new way of building helper libraries, we need to hear your feedback. Please let us know what you think.

To install the next-gen version of twilio-node use npm as follows, specifying the 3.0.0-rc.5 version:

npm install twilio@3.0.0-rc.5

Working with the helper library is a bit different than the current version. Here's an example of how to send an SMS:

var accountSid = '{{ account_sid }}'; // Your Account SID from www.twilio.com/console
var authToken = '{{ auth_token }}';   // Your Auth Token from www.twilio.com/console

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

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

Visit the next-gen branch on GitHub for more information and to provide feedback.