Get Started

SMS and MMS Quickstart Tutorial

Sending Text Messages via the REST API

After responding to so many messages, I think it's time we threw a monkey party. We are going to invite our friends via SMS messages. If you haven't yet, read over the REST Quickstart for voice calls, as this example utilizes concepts introduced in that guide.

To send an outgoing SMS message perform an HTTP POST to the Messages resource URI. We will also use a Twilio Helper Library for making REST requests.

If you are using a Twilio Free Trial account for this example, you will only be able to send messages to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Account's Phone Numbers Page.
sms-rest.cshtml
@using Twilio;
@{
    // set our AccountSid and AuthToken
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
 
    // instantiate a new Twilio Rest Client
    var client = new TwilioRestClient(AccountSid, AuthToken);
 
    // make an associative array of people we know, indexed by phone number
    var people = new Dictionary<string,string>() { 
        {"+14158675309","Curious George"},
        {"+14158675310","Boots"},
        {"+14158675311","Virgil"}
    };
 
    // iterate over all our friends
    foreach (var person in people) {
 
        // Send a new outgoing SMS by POSTing to the Messages resource */
        client.SendMessage(
            "YYY-YYY-YYYY", // From number, must be an SMS-enabled Twilio number 
            person.Key,     // To number, if using Sandbox see note above
            // message content
            string.Format("Hey {0}, Monkey Party at 6PM. Bring Bananas!", person.Value)
        );

        Response.Write(string.Format("Sent message to {0}", person.Value));
    }    
}

Lets look at the details:

  • First, head over to the Twilio website and log into your Twilio Account page. On the Dashboard near the top you will find your AccountSid and AuthToken. Copy those values and paste them into AccountSid and AuthToken variables.
  • Next, we instantiate a new TwilioRestClient and call the SendMessage method with the To, From and Body of the message. The From parameter should be the Sandbox phone number for trial accounts or an SMS-enabled Twilio phone number you purchased for upgraded accounts.

If your REST request was successful, the SMS has been successfully queued for transmission. The SMS will be sent as soon as possible at a maximum rate of 1 message per second per 'From' phone number.

By default, your application does not get any notification when an SMS is actually sent or when there is a delivery failure. To get a callback when an SMS is sent or when there is a failure, specify a URL in the StatusCallback parameter of the SendMessage method.

Sending MMS Messages via the REST API

It's also easy to send an outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify a parameter for the URL of media, such as an image.

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.

Modify the foreach loop as follows:

    // iterate over all our friends
    foreach (var person in people) {
 
        // Send a new outgoing MMS by POSTing to the Messages resource */
        client.SendMessage(
            "YYY-YYY-YYYY", // From number, must be an SMS-enabled Twilio number 
            person.Key,     // To number, if using Sandbox see note above
            // message content
            string.Format("Hey {0}, Monkey Party at 6PM. Bring Bananas!", person.Value),
            // media url of the image
            new string[] {"https://demo.twilio.com/owl.png" }
        );

        Response.Write(string.Format("Sent message to {0}", person.Value));
    }    

For more information about sending SMS and MMS messages, see the REST documentation.


Next: Tracking Conversations with Cookies »