How to send SMS Text Messages with Twilio from Rails

It was kind of confusing to find a straight forward example on how to send a text message through Twilio using Rails. Here is a step by step guide I wish I had.

  1. Sign up for a free account with Twilio. You will get a good amount of free credit to play around with.
  2. Install the Twilio Ruby Gem by navigating to your project folder in Terminal and typing:

    sudo gem install twiliolib
  3. In your controller (create a new controller if you want), put this chunk of code at the very top. Replace SID, ACCOUNT_TOKEN, and CALLER_ID with what Twilio provides you:

    require "twiliolib.rb" # your Twilio authentication credentials ACCOUNT_SID = "put_your_sid_in_parentheses_here" ACCOUNT_TOKEN = "put_your_token_in_parentheses_here" # version of the Twilio REST API to use - do not change API_VERSION = '2010-04-01' # Sandbox phone number # or outgoing Caller ID you have purchased through Twilio CALLER_ID = "put_your_sandbox_phone_number_here" #CALLER_PIN only needed to send using sandbox number. Looks like 9999-9999 CALLER_PIN = "put_your_sandbox_pin_number_here"
  4. Then in a method in that controller try this:

    #Text message info t = { 'From' => CALLER_ID, 'To' => "replace_with_your_cell_num_in_parentheses", 'Body' => ("#{CALLER_PIN} Twilio scales!") } begin account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN) resp = account.request("/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages", 'POST', t) resp.error! unless resp.kind_of? Net::HTTPSuccess rescue StandardError => bang #This area will be called if an error happens with the Twilio API redirect_to({ :action => :index, 'msg' => "Error #{ bang }" }) return end redirect_to({ :action => :index, 'msg' => "SMS sent." })
  5. Then you should get a text message that says: “Sent from a Twilio Trial Account -9999-9999 Twilio Scales!”
  6. Now that you have it working with a sandbox number it’s easy to to switch to a real phone number. Just go into the “Numbers” section of your account and buy an outgoing phone number. Give the phone number an SMS URL. I just gave it the url of my app.change out the sandbox phone number
  7. Then use that new phone number instead of your sandbox phone number and delete the CALLER_PIN from the top and in the body part of the text message you are sending.

If you found this post helpful please link to it from your blog or website. Leaving comments below is also cool. Thanks!

8 thoughts on “How to send SMS Text Messages with Twilio from Rails

  1. Just used this to get set up today. It worked like a charm! Thanks.

  2. Protip: dont put SID or token in parenthenses. just in quotes. also before you can send to a number you have to register the caller ID over at your twilio dashboard

  3. Pingback: links for 2011-07-05 « Caiwangqin’s delicious bog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>