TwiMLTM: the Twilio Markup Language

Overview

Twilio handles instructions for calls and SMS messages in real time from web applications.

When an SMS or incoming call is received, Twilio looks up the URL of the web application associated with the phone number called and makes a request to that URL. The web server that responds to the request decides how the call should proceed by returning a Twilio Markup XML (TwiML) document telling Twilio to say text to the caller, send an SMS message, play audio files, get input from the keypad, record audio, connect the call to another phone and more.

TwiML is similar to HTML. Just as HTML is rendered in a browser to display a webpage, TwiML is 'rendered' by Twilio to the caller. Only one TwiML document is rendered to the caller at once but many documents can be linked together to build complex interactive voice applications.

Outgoing calls are controlled in the same manner as incoming calls using TwiML. The initial URL for the call is provided as a parameter to the Twilio REST API request you make to initiate the call. See the making calls section of the REST documentation for more information on initiating outgoing calls. See the sending SMS messages section of the REST documentation for more information on sending SMS messages.

How Twilio Interacts with Your Application

You can configure Twilio to make its initial TwiML requests to your application via HTTP GET or POST. Subsequent call flow changes such as gathering input from the keypad using <Gather> or recording audio using <Record> require further requests to your application which are also configurable as GET or POST via each verb's 'method' parameter.

POST

When Twilio performs an HTTP POST to a web server, metadata associated with the call (phone number, etc.) is passed as the body of the POST just as if the parameters were submitted as part of a form on a webpage. One important caveat with POSTs is that Twilio cannot cache POSTs. If you want Twilio to cache static TwiML pages then have Twilio make requests to your application using GET.

GET

When Twilio performs an HTTP GET to a web server, metadata associated with the call (phone number, etc.) is passed as URL query string parameters as if the parameters were submitted as part of a form on a webpage. Twilio will honor standard HTTP caching directives for HTTP GET requests and locally cache static TwiML documents and media files. Caching is especially important for large media files like MP3s.