TwiML™ Message: Your Response

When a message comes in to one of your Twilio numbers, Twilio makes an HTTP request to the message URL configured for that number. In your response to that request you can tell Twilio what to do in response to the message. You can configure your number URLs here.

Twilio Is a Well Behaved HTTP Client

Twilio behaves just like a web browser, so there's nothing new to learn.

  • 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.

Cookies

Twilio will keep cookie state across multiple SMS messages between the same two phone numbers. This allows you to treat the separate messages as a conversation, and store data about the conversation, such as a session identifier, in the cookies for future reference. Twilio will expire the cookies for that conversation after four hours of inactivity.

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 a TwiML XML Instruction Set. See the XML Verbs section for details. This is the most commonly used response.
text/plain Twilio returns the content of the text file to the sender in the form of a message.

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 Message snippet sends "Hello World" as a message reply to the sender before redirecting control to the TwiML at https://demo.twilio.com/sms/welcome.

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Message>Hello World!</Message>
    <Redirect>https://demo.twilio.com/sms/welcome</Redirect>
</Response>

You can provide multiple <Message> verbs in one TwiML document to send multiple messages. For example:

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Message>This is message 1 of 2.</Message>
    <Message>This is message 2 of 2.</Message>
</Response>

TwiML elements ("verbs" and "nouns") have case-sensitive names. For example, using <message> instead of <Message> 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.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>I'm hungry!</Message>
</Response>

TwiML Verbs

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

  • <Message>: Send a message in reply to the incoming message.

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 an <Message> is followed by a <Redirect>, the <Redirect> is unreachable if the <Message> verb's 'action' URL is set. In this case, SMS session flow continues with the TwiML received in your response to the 'action' URL request.

The following verbs may impact control flow: <Message> and <Redirect>

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 <Message> with its <Media> & <Body> nouns, there are nested XML elements that are nouns.

Status Callbacks

Status callbacks do not control application flow, so TwiML does not need to be returned; however, it's recommended that you respond to status callbacks with either a 204 No Content or a 200 OK with Content-Type: text/xml and an empty <Response/> in the body. Failure to respond properly will result in warnings in Debugger.

Generating TwiML with Twilio Helper Libraries

Twilio provides helper libraries to let you easily generate those TwiML in your favorite language.

Example 1: Send Hello World and redirect TwiML control

Loading Code Samples...
Language
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
const message = response.message();
message.body('Hello World!');
response.redirect('https://demo.twilio.com/sms/welcome');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        var message = new Message();
        message.Body("Hello World!");
        response.Message(message);
        response.Redirect(url: "https://demo.twilio.com/sms/welcome");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$message = $response->message();
$message->body('Hello World!');
$response->redirect('https://demo.twilio.com/sms/welcome');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message do |message|
    message.body('Hello World!')
end
response.redirect('https://demo.twilio.com/sms/welcome')

puts response
from twilio.twiml.messaging_response import Body, Message, Redirect, MessagingResponse

response = MessagingResponse()
message = Message()
message.body('Hello World!')
response.append(message)
response.redirect('https://demo.twilio.com/sms/welcome')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.Redirect;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("Hello World!");
        Message message = new Message.Builder().body(body).build();
        Redirect redirect = new Redirect.Builder()
            .url("https://demo.twilio.com/sms/welcome").build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).redirect(redirect).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message><Body>Hello World!</Body></Message>
    <Redirect>https://demo.twilio.com/sms/welcome</Redirect>
</Response>
Send Hello World and redirect TwiML control
Loading Code Samples...
Language
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
response.message('This is message 1 of 2.');
response.message('This is message 2 of 2.');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        response.Message("This is message 1 of 2.");
        response.Message("This is message 2 of 2.");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$response->message('This is message 1 of 2.');
$response->message('This is message 2 of 2.');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message(body: 'This is message 1 of 2.')
response.message(body: 'This is message 2 of 2.')

puts response
from twilio.twiml.messaging_response import Message, MessagingResponse

response = MessagingResponse()
response.message('This is message 1 of 2.')
response.message('This is message 2 of 2.')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("This is message 1 of 2.");
        Message message = new Message.Builder().body(body).build();
        Body body2 = new Body("This is message 2 of 2.");
        Message message2 = new Message.Builder().body(body2).build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).message(message2).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>This is message 1 of 2.</Message>
    <Message>This is message 2 of 2.</Message>
</Response>
Send two messages
Loading Code Samples...
Language
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
response.message('I\'m hungry!');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        response.Message("I'm hungry!");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$response->message('I\'m hungry!');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message(body: "I'm hungry!")

puts response
from twilio.twiml.messaging_response import Message, MessagingResponse

response = MessagingResponse()
response.message('I\'m hungry!')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("I'm hungry!");
        Message message = new Message.Builder().body(body).build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>I'm hungry!</Message>
</Response>
Send a critical status update

See Also

Want to send a message without waiting for an inbound message? See our outbound message documentation.

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.

1 / 1
Loading Code Samples...
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
const message = response.message();
message.body('Hello World!');
response.redirect('https://demo.twilio.com/sms/welcome');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        var message = new Message();
        message.Body("Hello World!");
        response.Message(message);
        response.Redirect(url: "https://demo.twilio.com/sms/welcome");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$message = $response->message();
$message->body('Hello World!');
$response->redirect('https://demo.twilio.com/sms/welcome');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message do |message|
    message.body('Hello World!')
end
response.redirect('https://demo.twilio.com/sms/welcome')

puts response
from twilio.twiml.messaging_response import Body, Message, Redirect, MessagingResponse

response = MessagingResponse()
message = Message()
message.body('Hello World!')
response.append(message)
response.redirect('https://demo.twilio.com/sms/welcome')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.Redirect;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("Hello World!");
        Message message = new Message.Builder().body(body).build();
        Redirect redirect = new Redirect.Builder()
            .url("https://demo.twilio.com/sms/welcome").build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).redirect(redirect).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message><Body>Hello World!</Body></Message>
    <Redirect>https://demo.twilio.com/sms/welcome</Redirect>
</Response>
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
response.message('This is message 1 of 2.');
response.message('This is message 2 of 2.');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        response.Message("This is message 1 of 2.");
        response.Message("This is message 2 of 2.");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$response->message('This is message 1 of 2.');
$response->message('This is message 2 of 2.');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message(body: 'This is message 1 of 2.')
response.message(body: 'This is message 2 of 2.')

puts response
from twilio.twiml.messaging_response import Message, MessagingResponse

response = MessagingResponse()
response.message('This is message 1 of 2.')
response.message('This is message 2 of 2.')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("This is message 1 of 2.");
        Message message = new Message.Builder().body(body).build();
        Body body2 = new Body("This is message 2 of 2.");
        Message message2 = new Message.Builder().body(body2).build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).message(message2).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>This is message 1 of 2.</Message>
    <Message>This is message 2 of 2.</Message>
</Response>
SDK Version:
  • 5.x
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • TwiML
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
response.message('I\'m hungry!');

console.log(response.toString());
using Twilio.TwiML;


class Example
{
    static void Main()
    {
        var response = new MessagingResponse();
        response.Message("I'm hungry!");

        System.Console.WriteLine(response.ToString());
    }
}
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$response = new Twiml();
$response->message('I\'m hungry!');

echo $response;
require 'twilio-ruby'

response = Twilio::TwiML::MessagingResponse.new
response.message(body: "I'm hungry!")

puts response
from twilio.twiml.messaging_response import Message, MessagingResponse

response = MessagingResponse()
response.message('I\'m hungry!')

print(response)
import com.twilio.twiml.Body;
import com.twilio.twiml.Message;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Body body = new Body("I'm hungry!");
        Message message = new Message.Builder().body(body).build();
        MessagingResponse response = new MessagingResponse.Builder()
            .message(message).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>I'm hungry!</Message>
</Response>