Build the future of communications.
Start building for free

3 Things You May Not Know About Twilio Programmable SMS


Twilio Quickstarts make it easy to get started with Programmable SMS. But where do you go to learn more after you send and receive your first text messages?

Twilio Guides are the documentation you need after “Getting Started.” Guides answer common questions and come replete with code snippets in all the most popular languages so that you can build your Twilio app faster.

Here are three lessons you’ll find in the Programmable SMS guide. We’ll show off the PHP examples here, but the guides offer code snippets in C#, Java, Python, Ruby, and Javascript as well.

Use cookies to track SMS conversations

SMS interactions typically span more than one text. Twilio offers an easy way to track SMS conversations without spinning up a datastore.

When someone texts your Twilio phone number, Twilio makes an HTTP request to your app and expects an HTTP response in return. Much like the HTTP request-response cycle that happens with browsers, you can use cookies to temporarily store data about a session — only in this case, a “session” represents a conversation.

Here’s what conversation tracking looks like in PHP:


    // start the session

    // get the session variable if it exists
    $counter = $_SESSION['counter'];

    // if it doesn't, set the default
    if(!strlen($counter)) {
        $counter = 0;

    // increment it

    // save it
    $_SESSION['counter'] = $counter;

    // make an associative array of senders we know, indexed by phone number
    $people = array(
        "+14158675309"=>"Curious George",

    // if the sender is known, then greet them by name
    // otherwise, consider them just another monkey
    if(!$name = $people[$_REQUEST['From']]) {
        $name = "Monkey";

    // output the counter response
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    <Message><?php echo $name ?> has messaged <?php echo $_REQUEST['To']." ".$counter ?> times</Message>

Any time there are more than two messages between your app and the user, you’ll probably want to track state. A couple real world examples:

  • An SMS based trivia game uses cookies to store a player’s score and the correct answer to their previous question.
  • mRelief, a Y Combinator backed non-profit, uses cookies to track what step its users are on when taking an SMS survey.

Check out the conversation tracking guide for more details.

Use Delivery Status to Know If Your Message Made It

There are a number of reasons why a text message may not arrive at its destination. For example:

  • The phone was turned off
  • The phone was out of service range
  • The recipient asked not to receive messages from your number
  • The number was a landline

These edge cases aren’t always apparent when developing your app and sending texts to the phone on your desk. But as your app scales, it’s important to confirm that messages hit their target so that you can resend the SMS or move the conversation to a different medium (e.g., email) as appropriate.

To track delivery status, simply add a statusCallback parameter to the code used to fire off the SMS:

// Get the PHP helper library from
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from
$token = "your_auth_token";
$client = new Client($sid, $token);

        'from' => '+15017250604',
        'body' => "McAvoy or Stewart? These timelines can get so confusing.",
        'statusCallback' => ""

Twilio will send updates to the statusCallback webhook with statues of queued, failed, sent, delivered, and undelivered as the message passes through the delivery cycle.

You can also use delivery receipts when replying to an SMS with the <Message> tag. And if you’d like to help us in our quest to continually improve our service, you can send us feedback via the Twilio Message Delivery Feedback API.

Retrieve and Modify Message History

You’ll often want to revisit the details of messages you’ve sent and received. The Programmable SMS guide tells you how to retrieve and modify message history.

For example, here’s how you list your SMS logs using the PHP helper library:

// Get the PHP helper library from
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from
$token = "your_auth_token";
$client = new Client($sid, $token);

// Loop over the list of messages and echo a property for each one
foreach ($client->messages->read() as $message) {
    echo $message->body;

For privacy’s sake, you may want to erase the contents of a message stored on Twilio servers:

// Get the PHP helper library from
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from
$token = "your_auth_token";
$client = new Client($sid, $token);

$message = $client
        array("body" => "")

Alternatively, you can remove all traces of the SMS from Twilio’s logs simply by changing that last line:

    ->delete(); // Deletes entire message record

What else can you do?

We’ve covered only half of the topics in the Programmable SMS guides. Whether you’re a long-time Twilio user or just getting started, the guides will level-up your Twilio game so that you can build features faster.

If you have any questions, or if there are any other topics you’d like to covered in guides, please let us know in the comments or drop me a line at

Sign up and start building
Not ready yet? Talk to an expert.