Get Started

Twilio SMS and MMS SMS Quickstart

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.

To send an outgoing SMS message, perform an HTTP POST to the Messages resource URI. Using the twilio-ruby library's client object, you can create a new instance of the message resource and specify the to, from, and body parameters for your message.

If you want to send a message to several recipients, you can create an array of recipients and iterate through each number in that array.

send-sms.rb
require 'rubygems'
require 'twilio-ruby'

account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxx"
auth_token = "yyyyyyyyyyyyyyyyyyyyyyyyy"
client = Twilio::REST::Client.new account_sid, auth_token

from = "+14159998888" # Your Twilio number

friends = {
"+14153334444" => "Curious George",
"+14155557775" => "Boots",
"+14155551234" => "Virgil"
}
friends.each do |key, value|
  client.account.messages.create(
    :from => from,
    :to => key,
    :body => "Hey #{value}, Monkey party at 6PM. Bring Bananas!"
  ) 
  puts "Sent message to #{value}"
end

Once your app is saved, run the file at the command line:

$ ruby send-sms.rb 
Sent message to Curious George
Sent message to Boots
Sent message to Virgil

As soon as you hit enter, the app should send SMS messages to the specified recipients and you should see a confirmation message in your terminal.

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 send-sms.rb as follows:

send-sms.rb
    friends.each do |key, value|
      client.account.messages.create(
        :from => from,
        :to => key,
        :body => "Hey #{value}, Monkey party at 6PM. Bring Bananas!"
        :media_url => "https://demo.twilio.com/owl.png"
      ) 
      puts "Sent message to #{value}"
    end

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


Next: Reply to Incoming SMS »