TwiML (the Twilio Markup Language) is a set of instructions you can use to tell Twilio what to do when you receive an incoming call, SMS, or fax.
When someone makes a call to one of your Twilio numbers, Twilio looks up the URL associated with that phone number and sends it a request. Twilio then reads the TwiML instructions hosted at that URL to determine what to do, whether it's recording the call, playing a message for the caller, or prompting the caller to press digits on their keypad.
At its core, TwiML is an XML document with special tags defined by Twilio to help you build your Programmable Voice application.
The following will say "Hello, world!" when someone dials a Twilio number configured with this TwiML:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Hello, world!</Say> </Response>
You can always return raw TwiML from your language of choice, or leverage the Twilio helper libraries to automatically create valid TwiML for you: you can check out our short tutorial on responding to incoming phone calls, available in our six supported helper library languages.
You can also leverage Twilio's TwiML bins, our serverless solution that lets you write TwiML that Twilio will host for you so you can quickly prototype a solution without spinning up a web server.
Outbound calls (calls from a Twilio number to an outside number) are controlled using TwiML in the same manner. When you initiate an outbound call with the Twilio API, Twilio then requests your TwiML to learn how to handle the call.
Twilio executes just one TwiML document to the caller at a time, but many TwiML documents can be linked together to build complex interactive voice applications.
TwiML elements (verbs and nouns) have case-sensitive names. For example, using
<say> instead of
<Say> will result in an error. Attribute names are also case sensitive and camelCased.
You can use XML comments freely in your TwiML; the interpreter ignores them.
In any TwiML response to a Twilio request, you must nest all verb elements within
<Response>, the root element of Twilio's XML markup:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say> This message must be nested in a Response element in order for Twilio to say it to your caller. </Say> </Response>
Any other structure is considered invalid.
TwiML verbs tell Twilio what actions to take on a given call. Because of this, most elements in a TwiML document are TwiML verbs. Verb names are case sensitive, as are their attribute names.
You can use different combinations of TwiML verbs to create all kinds of interactive voice applications. The core TwiML verbs for Programmable Voice are:
The following verbs may be used to control the flow of your call:
There are certain situations when the TwiML interpreter may not reach verbs in a TwiML document because control flow has passed to a different document. This usually happens when a verb's action attribute is set.
For example, if a
<Say> verb is followed by a
<Redirect> and then another
<Say>, the 2nd
<Say> is unreachable because
<Redirect> transfers full control of a call to the TwiML at a different URL.
A TwiML noun describes the phone numbers and API resources you want to take action on. Effectively, a TwiML noun is anything nested inside a verb that is not itself a verb: it's whatever the verb is acting on.
When someone makes an inbound call to one of your Twilio phone numbers, Twilio needs to request TwiML from your application to get instructions for handling the call.
You can configure your Twilio phone number to point to your application's URL by visiting the phone numbers section of the console. Select your phone number, then scroll to the 'Voice & Fax' section to set a webhook, TwiML bin, or Twilio Function for Twilio to send that HTTP request when a call comes in:
Twilio makes its request over HTTP, either as a
POST, just like requesting a web page in your browser or submitting a form.
Twilio cannot cache POSTs. If you want Twilio to cache static TwiML pages, then have Twilio make requests to your application using GET.
By including parameters and values in its request, Twilio sends data to your application that you can act upon before responding.
Twilio always sends the following parameters with its request.
These will send as either
POST parameters or URL query parameters, depending on which HTTP method you've configured.
|CallSid||A unique identifier for this call, generated by Twilio.|
|AccountSid||Your Twilio account ID. It is 34 characters long, and always starts with the letters
The phone number or client identifier of the party that initiated the call.
Phone numbers are formatted with a '+' and country code, e.g.
The phone number or client identifier of the called party.
Phone numbers are formatted with a '+' and country code, e.g.
A descriptive status for the call.
The value is one of the following:
The version of the Twilio API used to handle this call.
For incoming calls, this is determined by the API version set on the called number. For outgoing calls, this is the version used by the REST API request from the outgoing call.
A string describing the direction of the call:
This parameter is set only when Twilio receives a forwarded call, but its value depends on the caller's carrier including information when forwarding.
Not all carriers support passing this information.
|CallerName||This parameter is set when the IncomingPhoneNumber that received the call has had its
A unique identifier for the call that created this leg.
This parameter is not passed if this is the first leg of a call.
Twilio also attempts to look up geographic data based on the To and From phone numbers. If available, Twilio will send the following parameters with its request:
|FromCity||The city of the caller|
|FromState||The state or province of the caller|
|FromZip||The postal code of the caller|
|FromCountry||The country of the caller|
|ToCity||The city of the called party|
|ToState||The state or province of the called party|
|ToZip||The postal code of the called party|
|ToCountry||The country of the called party|
Depending on what is happening on a call, other variables may also be sent.
For example, when Twilio receives SIP, it will send additional parameters to your web application: you'll find the list of parameters sent with SIP in "SIP and TwiML interaction".
The following are the possible values for the CallStatus parameter. These values also apply to the DialCallStatus parameter, which is sent with HTTP requests to a
<Dial> action URL.
|queued||The call is ready and waiting in line before going out.|
|ringing||The call is currently ringing.|
|in-progress||The call was answered and is actively in progress.|
|completed||The call was answered and has ended normally.|
|busy||The caller received a busy signal.|
|failed||The call could not be completed as dialed, most likely because the phone number was non-existent.|
|no-answer||The call ended without being answered.|
|canceled||The call was canceled via the REST API while queued or ringing.|
After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the Twilio number that was called.
You need to explicitly provide this URL to your application in the StatusCallback parameter of each message for which you want the status callbacks. The raw TwiML for this looks like:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Dial> <Number statusCallbackEvent="initiated ringing answered completed" statusCallback="https://myapp.com/calls/events" statusCallbackMethod="POST"> +12316851234 </Number> </Dial> </Response>
By providing a StatusCallback URL for your Twilio number and capturing this request, you can determine when a call ends and receive information about the call. Please note that non-relative URLs must contain a valid hostname, and underscores are not permitted.
When Twilio sends parameters to your application in an asynchronous request to the StatusCallback URL, they include the same parameters passed in a synchronous TwiML request.
The Status Callback request also passes these additional parameters:
|CallDuration||The duration in seconds of the just-completed call.|
|RecordingUrl||The URL of the phone call's recorded audio. This parameter is included only if Record=true is set on the REST API request, and does not include recordings from
|RecordingSid||The unique id of the Recording from this call.|
|RecordingDuration||The duration of the recorded audio (in seconds).|
All phone numbers in requests from Twilio are in E.164 format if possible. For example, (231) 685-1234 would come through as '+12316851234'. However, there are occasionally cases where Twilio cannot normalize an incoming caller ID to E.164. In these situations, Twilio will report the raw caller ID string.
All dates and times in requests from Twilio are GMT in RFC 2822 format. For example, 6:13 PM PDT on August 19th, 2010 would be 'Fri, 20 Aug 2010 01:13:42 +0000'
Twilio behaves just like a web browser when making HTTP requests to URLs:
Twilio does the right thing when your application responds with different MIME types:
|text/xml, application/xml, text/html||Twilio interprets the returned document as an XML Instruction Set (which we like to call TwiML). This is the most commonly used response.|
|various audio types||Twilio plays the audio file to the caller, and then hangs up. See the
|text/plain||Twilio reads the content of the text out loud to the caller, and then hangs up.|
When your application responds to a Twilio request with XML, Twilio runs your document through the TwiML interpreter. To keep things simple, the TwiML interpreter only understands the few specially-named XML elements that make up TwiML:
<Response>, verbs, and nouns.
The interpreter starts at the top of your TwiML document and executes instructions (verbs) in order from top to bottom.
The following code snippet reads "Hello World" to the caller before playing Cowbell.mp3 for the caller and then hanging up.
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Hello, World!</Say> <Play>https://api.twilio.com/Cowbell.mp3</Play> </Response>
Status callbacks do not control call flow, so TwiML does not need to be returned. If you do respond, use status code 204 No Content or 200 OK with Content-Type: text/xml and an empty
<Response/> in the body. Not responding properly will result in warnings in Debugger.
Go in-depth to learn more about the various TwiML verbs you'll use with Twilio's Programmable Voice, such as <Dial> to connect a call or <Gather> for speech recognition and collecting user key presses. You'll find all in-depth reference documents linked above.
You may also wish to explore how to generate TwiML with Twilio's helper libraries, provided to let you generate TwiML in your favorite language.