The twilio-ruby helper library lets you write Ruby code to make HTTP requests to the Twilio API.
The easiest way to install twilio-ruby is from RubyGems. In the command
gem install twilio-ruby:
# Be sure to type "twilio-ruby", not just "twilio" gem install twilio-ruby
Or, you can clone the source code for
twilio-ruby, and install
the library from there.
If the command line gives you a big long error message that says Permission
Denied in the middle of it, try running the above commands with
sudo gem install twilio.
Try sending yourself an SMS message, like this:
require 'rubygems' require 'twilio-ruby' # Get your Account Sid and Auth Token from twilio.com/user/account account_sid = 'AC32a3c49700934481addd5ce1659f04d2' auth_token = '' @client = Twilio::REST::Client.new account_sid, auth_token message = @client.account.sms.messages.create(:body => "Jenny please?! I love you <3", :to => "+14159352345", # Replace with your phone number :from => "+14158141829") # Replace with your Twilio number puts message.sid
The Rest API Documentation has examples written in Ruby for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the Ruby library. If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the Ruby quickstarts.
If the Twilio API returns a 400 or a 500 level HTTP response, the
library will throw a
Twilio::REST::RequestError. 400-level errors are normal
during API operation ("Invalid number", "Cannot deliver SMS to that
number", for example) and should be handled appropriately.
require 'twilio-ruby' begin client = Twilio::REST::Client.new account_sid, auth_token client.account.sms.messages.create( from => from_number, to => to_number, body => "Hello World" ) rescue Twilio::REST::RequestError => e puts e.message end