Sean's Obsessions

  • I'm a happy Linode customer. This is a Linode 540 VPS. Linode periodically increases RAM and disk while keeping costs steady, which makes them the natural choice.
  • Archives

05 Nov

Using an IP phone with Twilio

Twilio has supported SIP termination for a while but recently announced SIP origination. This means that previously you could receive calls with SIP but now you can also make calls from a hard phone using SIP instead of using the browser client or going through the PSTN.

It was this second announcement that got my interest. I have an IP phone that I use in my office, currently it’s through but I like the pricing and interface of Twilio and would rather use them.

For some reason everything I read about SIP and Twilio uses a separate SIP proxy even if they have a compliant SIP device. Even their own blog takes a working SIP ATA and puts it behind Asterisk. I knew I could do better.

What you’ll need

  • An IP phone. I used a Cisco 7960 converted to SIP
  • A publicly available web server running PHP (feel free to use another language, we have to do some basic processing of the request so static won’t work)
  • A Twilio account

When thinking about VoIP, always think in two directions. Inbound and outbound. Sending and receiving. Talking and listening.

Receiving calls

Get a number and point the Voice Request URL to your web server. Please don’t use mine.

Phone Number | Dashboard | Twilio 2013-10-21 08-04-09

Your outbound.php script will send some TwiML to dial your phone:

<?xml version="1.0" encoding="UTF-8"?>

Note: this part was a lot of trouble. After some packet tracing and some brilliant detective work by Brian from Twilio support, it turns out that the address of the phone in the SIP invite had to be an IP address, not a hostname. With a hostname the phone received the INVITE but always returned 404.

Your phone will need to be on the Internet, either with a public address or with UDP port 5060 port forwarded to it. The “line_number” has to match the name of the line on your phone. In my case, I named my line after my phone number:

proxy2_address: ""
line2_name: "204xxxxxxx"
line2_shortname: "204xxxxxxx"
line2_displayname: "204xxxxxxx"
line2_authname: "204xxxxxxx"

One thing to note is that you don’t register your phone to Twilio. I left the proxy address there so that the requests will keep the NAT translation alive. After detailed packet tracing it looks like the Twilio SIP messages originate from different IP addresses so this might not be helping as much as I thought.

At this point you should be able to dial your Twilio number from a regular phone. Twilio will request inbound.php and then do a SIP INVITE to the phone. The phone will accept it and then you have voice!

Making calls

The first step is to set up a SIP domain in Twilio:

Twilio User - Account Sip Domains 2013-10-21 08-15-14

Call it whatever you want, but you’ll need to set the Voice URL.

Twilio User - Account Sip Domains 2013-10-21 08-15-57

The script you point it at has to parse the data coming in from Twilio to find the phone number and then issue a Dial instruction to get Twilio to dial the phone and connect the two ends.


  $called = preg_replace('/sip:1?(.*?)@.*/', '{$1}', $_POST['Called']);

  header("content-type: text/xml");
  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  $contents = ob_get_contents();
  error_log($contents, 3, "/tmp/twilio.txt");

  <Dial timeout="10" record="false" callerId="YOURTWILIONUMBER"><?= $called ?></Dial>

All we’re doing here is extracting the phone number from the Called header that Twilio sends us, stripping any leading 1’s, and then sending a TwiML response to dial that number. The ob_start through to the error_log is just logging the parameters if you’re interested.

Don’t forget to change the caller ID to your phone number, otherwise you get errors in the Twilio console.

So now when you place a call on your phone, Twilio will send the digits to the application which will return a Dial verb and the proper 10 digit number. Twilio links the two calls.


It took a bit of playing around to get this going but now I’ve shown that you don’t need an Asterisk server to integrate a SIP phone with Twilio. If you are setting up a phone bank or something with hard phones you can just configure them to hit Twilio, and for Twilio to hit them.

Of course, if you are expecting significant inbound traffic the benefit of a SIP proxy is that it can direct calls to multiple numbers without needing Twilio to be able to reach the phone directly. I’m hoping that Twilio can improve on that in the future!

4 Responses to “Using an IP phone with Twilio”

  1. 1
    Larry Truong Says:

    Hi Sean: Where is the Cisco IP Phone 7960 connect to? Do you attach it to the Cisco CM router or PC? I am trying to deploy the Cisco UC500 router to the twilio sip but I have no idea.

    Regards and thanks,
    Larry Truong

  2. 2
    sean Says:

    Hi Larry,

    The 7960 is on the network by itself and receiving the SIP messages. If you have a UC500 then your phones will register to the UC500 and you’ll point Twilio to the IP of the router. Twilio will invite the extension to the call, the router will invite the phone and bridge the call.

  3. 3
    Aaron Arnold Says:

    This is great, helps me out a lot.

    We have a ton of Polycom phones from a cloud-based provider we’re leaving behind, and I’m trying to figure out what exactly Twilio is and what it can do. If I understand this correctly, I can effectively treat Twilio as my server (routing my calls, handling queues, etc), then just use the SIP tag to register my phones to Twilio, completely negating the need for a new server?

  4. 4
    sean Says:

    Something like that. You don’t actually register the phones to Twilio, you’re using some scripts to point the two parties together.

    If I had more than a few phones I’d probably use an Asterisk server in the middle, as the way I did it above doesn’t scale well. The advantage of a SIP server is that the phones can do a proper registration and then Asterisk can take care of picking the right phone to ring.

Leave a Reply

© 2014 Sean's Obsessions | Entries (RSS) and Comments (RSS)

Powered by Wordpress, design by Web4Sudoku, based on Pinkline byGPS Gazette