Building a voice app with Node, Twilio, Compose and Heroku - Part 1

I’m challenging myself today. My hangover Saturday. It’s just no good sitting around in my dressing gown watching the same Simpsons episodes I did when I was hungover last week. It just doesn’t warm the soul.

My challenge was to use some of these incredible APIs I read about every day, these APIs that I look at and thing - wow, I could make something really good. But do I? No. Thinking about doing something and doing something are two very separate things, unfortunately.

But this stops now.

Picture the scene, you’ve agreed to go and meet some friends somewhere, but its the end of the day and you forgot your phone charger. Your phone slowly splutters out of charge, looking up at you with its big flat face, wondering what went wrong in your relationship together that lead to you not taking even basic precautions to ensure your phone remains charged and loyal to your needs. Luckily, you’re with a friend and they’ve got a phone! Only problem is they don’t have your other friends number. How are we going to meet up now? If only there was some sort of number I could call to get their number. IF ONLY.

Well, person in this rare, edge case scenario - you’re in luck. I’m going to build you one today. Lets get started!

 About the tools


I’ve got a lot of love for MongoDB. It’s fast, JSON based, and seems to be pretty resilient. Well, it is when you let someone else host it. I’ve got a little saying that I apply to this scenario:

Can't someone else do it?

What Compose do, and they do very well is host MongoDB collections in a reliable, fault tolerant environment on a Cloud Provider of my choice. As I’ve mentioned before I love a bit of DigitalOcean, and awesomely, Compose allow me to host my Compose instance in the DigitalOcean London datacenter, allowing for low latency between my application server and my database server which I think is excellent.

They also provide a really smooth administration interface along as well as some handy analytics and general statistics for the connections that are currently pumping data in or out of the service.


A company that I’ve admired from afar for some time, but have never plucked up the courage to pick them some flowers and come to their doorstep - Twilio provide programmer-centric voice and SMS services backed by a global point-of-presence network, ensuring that your important comms get to their recipients on time, every time!

The bit about their API offering that intrigued me is the possibility of receiving calls made to a number and being able to script the interaction the service has with the person calling, based on some sort of endpoint or database call - that’s sweet.


The curiously named web host that allows for an easy push-to-deploy based workflow. I chose them today because I’ve not used them before and I think I should broaden my horizons.

To be totally honest, I usually run my own custom flavour of Dokku on a DigitalOcean box - and could have used that. But I use that every day. Lets live a little.

 Setting up the project


So, as I mentioned - I’m going to write this solution using Node and the fabulous Twilio Node client. I’m going to set up an Express server to serve the API as well as the management screens. Lets set up our dependencies in package.json, by executing these commands:

$ npm install express-generator -g
$ express twilio-numbers

This installs a handy little tool called the Express Generator, which basically provides you the scaffolding for an Express project, with some layout already provided and a couple of useful error handlers and whatnot in the app.js file.

The second command is essentially going to create a folder in the current working directory called twilio-numbers, and fill with the goodness I just described above.


Now, I want to use Mongoose to connect to my Compose instance. Mongoose is a wicked little library, makes it very easy to interact with Mongo in a a fluent, readable way. So I’ll install that too!

$ npm install --save mongoose

And the Twilio client:

$ npm install --save twilio

(Just a note, if you were wondering what the --save does - it adds the package you just installed to the package.json file, which allows you to manage your dependencies a little more effectively!)

Click here for part two


Now read this

Debugging with Node.js and debug

One of the things I find most useful when creating complex Node.js applications is easily seeing what component of the code is generating console output. Now, it’d be pretty easy for me to simply bash in a command such as this:... Continue →