Using Twilio with Node.js by Dusty Candland of Red27 Consulting

Dusty Candland is the founder of Red27 Consulting in Denver, CO. Recently Dusty was working on an app called SMSBet.info that uses Node.js with Twilio. Dusty wrote this post to help you get started writing your own Twilio apps running on Node.

Recently while working on a Functional Mockup for SMSBet.info, we decided to use Node.js and Twilio. Combining these two technologies makes building SMS and telephony apps really easy. Node.js is a super fast, event-based, async server-side JavaScript engine. While the project is relatively young it has a very committed and growing community behind it.

Node.js on its own provides simple evented IO. The first step to building a Twilio app was to find a web framework to help handle the basics of HTTP and View rendering. Express is a popular framework that will work well for our needs. Using Express is enough to be able to receive an SMS message, get it’s content, and respond. That’s what I’m going to demonstrate here.

Setup

You’ll also need a Twilio account for these examples. If you’ve just signed up, you can use the sandbox number on your account or if you’ve upgraded a local number will work as well. First we’ll build the app and deploy it, then later on we’ll configure our SMS URL that gets invoked when a message is sent to our sandbox number.

Lastly we need a place to host this application, I’m going to use Nodester.

The Project

Now lets create our Express project and get the ball rolling.

1

2

$Express twiliosample

$cd twiliosample

twiliosample is the name of the Express project we’re creating. The Express command will create and initialize the app in a directory with the same name. If you prefer, you can use any name you want in place of twiliosample.

Inside of our project folder, locate and open app.js in your editor of choice and add the following to the top of the file. This creates a sys object that will allow us to log the incoming SMS data.

1

varsys=require('sys');

Next we’ll create a function to handle our incoming SMS requests. Add the following method under the default index route.

When a POST request comes in to /incoming, this function will be called to handle the request. We first extract the From and Body values from the POST data and log it. Next we return back the TwiML that will send a text message back to the sender with a thank you message.

To test this out you will need a public URL for Twilio to access. To test locally you can simulate the a request using the follow cURL syntax. Start your Node.js server using ‘node app.js’ from the project root. Then from another command prompt issue this curl command.

If everything is working you will see your TwiML returned and your message logged in the Node.js command prompt window.

Going Live

If you have a Nodester account, we can publish our application, set up Twilio to call it, and test it out! Nodester uses Git as for deployment, so we need to set up our project as a Git reposititory.

1

2

3

$git init.

$git add.

$git commit–m"initial commit"

Nodester provides a CLI toolkit to make it easier to work with your Nodester-hosted apps. You can install the tools using the Node Package Manager:

1

$npm install nodester-cli

Once you have the tools installed, follow these instructions to set up your user account. Next we’ll set up an application. Because app names need to be globally unique, you’ll need to use a name other than twiliosample. We’ll use the appnpm command to remotely install the packages we’re using for this project.

1

2

3

4

$nodester app create twiliosample app.js

$nodester appnpm install twiliosample express

$nodester appnpm install twiliosample jade

$nodester app info twiliosample

The last command will output a Git repository URL. Use that URL in the following command to connect your local Git repository with the remote Nodester repository.

1

$gitremoteaddnode{{git repo from previous output}}

Update the port your app is listening on, default is 3000, in app.js to the port listed by the info command. Commit your changes to git and push to Nodester and start your application.

1

2

$git push node master

$nodester app start twiliosample

Browse to your app and see if it’s all working http://[nodesterappname].nodester.com.

The last step is to connect your public Nodester app URL with your phone number. Sign in to Twilio and update the SMS URL for your phone number (or the sandbox number) to http://[nodesterapp].nodester.com/incoming. Send a text to you number and wait for the response. If you’re using the sandbox number remember to prefix your PIN to your text.

You can check the logs on the server to make sure your message was logged with the following command: