Introducing Templates for TwiML Bins

October 24, 2016
Written by

twimlbin

This past May we launched Native TwiML Bins in the Twilio Console. TwiML Bins made it easy for developers to prototype their ideas and even ship simple production use cases without the need to stand up a web server. Since then thousands of developers have been using TwiML Bins to explore the capabilities of TwiML and build simple call forwarding, auto response, and voicemail applications.  However, one consistent piece of feedback that we got from developers was their desire to be able to do more. For instance, SMS forwarding isn’t possible with a static TwiML document. You need to be able to customize the <Body>  tag to include both the phone number of the person sending the SMS and the body of the SMS itself.  Developers wanted to be able customize their TwiML at runtime based on the HTTP parameters based into the Bin. We’re excited to share that this is exactly what you can do today with TwiML Bins templates.

Before we dive into templates, let’s review how Twilio processes an incoming phone call.

How Twilio Processes An Incoming Phone Call

When a phone call is placed to a customer’s Twilio number, Twilio looks up the Voice Request URL associated with that phone number. Twilio then makes an HTTP request to that URL passing along the several parameters that provide the app with information about the incoming call. The app then constructs a TwiML document and returns this document in the HTTP response which Twilio then executes for the person on the phone.

TwiML Bins made it possible for a developers to write simple TwiML documents that Twilio hosts and then executes in response to incoming phone calls and SMS messages. Now developers can define a template that includes placeholders for any of the HTTP parameters passed-in by Twilio’s webhook request. This is done by enclosing the name of the HTTP parameter with opening and closing double curly braces (i.e. {{From}} ). With this ability it is now simple to build a TwiML Bin that will forward incoming SMS messages:

 <Response>
  <Message to="+12065551212">{{From}}: {{Body}}</Message>
</Response>

In addition to the HTTP parameters that Twilio passes, a developer can also pass their own custom HTTP parameters by simply appending them to the query string in the URL. Here’s an example in Python of placing an outbound call using a TwiML Bin:

client.calls.create(
    url="https://handler.twilio.com/twiml/EHxxx?Dest=%2B12065551212",
    to="+14155551212",
    from_="+15017250604")

The TwiML Bin used above would then reference the value of the Dest parameter like so:

<Response> 
  <Dial>{{Dest}}</Dial>
</Response>

It has been great to see developers using TwiML Bins to explore the power of TwiML and to make shipping simple things easy so that they can focus more on building experiences and less on managing web infrastructure. We hope that templates make TwiML Bins even more useful and look forward to seeing what you build.

If you have any any questions about TwiML Bins templates don’t hesitate to reach out to me at @crtr0 on Twitter or carter@twilio.com.