TwiML™ Voice: Your Response

When a phone call comes in to one of your Twilio numbers, Twilio makes an HTTP request to the URL configured for that number. In your response to that request you can tell Twilio what to do on the call.

Twilio Is a Well Behaved HTTP Client

Twilio behaves just like a web browser when making HTTP requests to URLs:

  • Cookies: Twilio accepts HTTP cookies and will include them in each request, just like a normal web browser.
  • Redirects: Twilio follows HTTP Redirects (HTTP status codes 301, 307, etc.), just like a normal web browser.
  • Caching: Twilio will cache files when HTTP headers allow it (via ETag and Last-Modified headers) and when the HTTP method is GET, just like a normal web browser.

Twilio Understands MIME Types

Twilio does the right thing when your application responds with different MIME types:

MIME Type Behavior
text/xml, application/xml, text/html Twilio interprets the returned document as an XML Instruction Set (which we like to call TwiML). See the TwiML Interpreter section for details. This is the most commonly used response.
various audio types Twilio plays the audio file to the caller, and then hangs up. See the <Play> documentation for supported MIME types.
text/plain Twilio reads the content of the text out loud to the caller, and then hangs up.

Twilio's TwiML Interpreter

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 a few specially named XML elements. In TwiML parlance these are divided into three groups: the root <Response> element, "verbs" and "nouns". Each group is discussed below.

The interpreter starts at the top of your TwiML document and executes instructions ("verbs") in order from top to bottom. As an example, the following TwiML 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" ?>  
    <Say>Hello World</Say>

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." And you can use XML comments freely; the interpreter ignores them.

The <Response> Element

The root element of Twilio's XML Markup is the <Response> element. In any TwiML response to a Twilio request, all verb elements must be nested within this element. Any other structure is considered invalid.

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

TwiML Verbs

Most XML elements in a TwiML document are TwiML verbs. Verb names are case sensitive, as are their attribute names. There are only six core TwiML Voice verbs and four secondary verbs, with detailed documentation on each. The six core verbs are:

  • <Say>: Read some text to the caller.
  • <Play>: Play an audio file to the caller.
  • <Record>: Record a call or part of a call.
  • <Gather>: Get the digits a caller presses.
  • <Dial>: Call another phone number or conference and connect the current caller.
  • <Sms>: Send an SMS message during a call.

Note that 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 <Sms> and then another <Say>, the 2nd <Say> is unreachable if the <Sms> verb's 'action' URL is set. In this case, call flow continues with the TwiML received in your response to the 'action' URL request.

The following verbs may impact control flow: <Gather>, <Record>, <Dial>, <Sms>, <Redirect>, <Hangup> and <Reject>.

TwiML Nouns

A Noun in TwiML is anything nested inside a verb that is not itself a verb. It's whatever the verb is acting on. This is usually just text. But sometimes, as in the case of <Dial> with its <Number> and <Conference> nouns, there are nested XML elements that are nouns.

Status Callbacks

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.

Need some help?

We all do sometimes; Code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.