Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Twilio SMS Ruby Quickstart

With just a few lines of code, your Ruby application can send and receive text messages with Twilio Programmable SMS.

This Ruby SMS Quickstart will teach you how to do this using our Communications REST API, the Twilio Ruby helper library, and Ruby’s Sinatra framework to ease development. If you prefer using Rails, check out this blog post.

In this Quickstart, you will learn how to:

1. Sign up for Twilio and get your first SMS-enabled Twilio phone number
2. Set up your development environment to send and receive messages
3. Send your first SMS
4. Receive inbound text messages
5. Reply to incoming messages with an SMS

Prefer to get started by watching a video? Check out our Ruby SMS Quickstart video on Youtube.

Show me how it's done!

If you already have a Twilio account and an SMS-enabled Twilio phone number, you’re all set here! Feel free to jump to the next step.

Before you can send an SMS with Ruby, you'll need to sign up for a Twilio account or sign into your existing account and purchase an SMS-capable phone number.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "SMS" box and click "Search."

Buy a SMS-Capable Twilio Phone Number

You’ll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.

Buy SMS Capable Number

Now that you have a Twilio account and a programmable phone number, you can start writing some code! To make things even easier, we'll next install Twilio's official helper for Ruby applications.

No problem! Take me through the setup.

If you’ve gone through one of our other Ruby Quickstarts already and have Ruby and the Twilio Ruby helper library installed, you can skip this step and get straight to sending your first text message.

To send your first SMS, you’ll need to have Ruby and the Twilio Ruby helper library installed.

Install Ruby

If you’re using a Mac or Linux machine, you probably already have Ruby installed. You can check this by opening up a terminal and running the following command:

$ ruby --version

You should see something like:

ruby 2.4

Windows users can use RubyInstaller to install Ruby.

Twilio’s Ruby SDK is tested against and supports Ruby versions from 2.0.0 through 2.4.0. (Got an older version of Ruby? You can use RVM or Homebrew to upgrade to the minimum supported version.)

Install the Twilio Ruby Helper Library

The easiest way to install twilio-ruby is from RubyGems.

gem install twilio-ruby

Manual Installation

Or, you can clone the source code for twilio-ruby, and install the library from there.

"Permission Denied"

If the command line gives you a long error message that says Permission Denied in the middle of it, try running the above commands with sudo: sudo gem install twilio-ruby.

All set! Let's send a text message.

Send an outbound SMS with Ruby

Now that we have Ruby and twilio-ruby installed, we can send an outbound text message from the Twilio phone number we just purchased with a single API request. Create and open a new file called send_sms.rb and type or paste in this code sample.

        This code creates a new instance of the Message resource and sends an HTTP POST to the Messages resource URI.

        Send SMS using Twilio

        This code creates a new instance of the Message resource and sends an HTTP POST to the Messages resource URI.

        You’ll need to edit this file a little more before your message will send:

        Replace the placeholder credential values
        Swap the placeholder values for account_sid and auth_token with your personal Twilio credentials.

        Go to and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on 'view':


        Open send_sms.rb and replace the values for account_sid and auth_token with your unique values.

        Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.

        Replace the "from" phone number
        Remember that SMS-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing from number with that one, making sure to use E.164 formatting:

        [+][country code][phone number including area code]

        Replace the "to" phone number
        Replace the to phone number with your mobile phone number. This can be any phone number that can receive text messages, but it’s a good idea to test with your own phone to see the magic happen! As above, you should use E.164 formatting for this value.
        Save your changes and run this script from your terminal:

        ruby send_sms.rb

        That's it! In a few moments, you should receive an SMS from your Twilio number on your phone.

        Are your customers in the U.S. or Canada? You can also send them MMS messages by adding just one line of code. Check out this guide to sending MMS to see how it's done.

        If you are on a Twilio trial account, your outgoing SMS messages are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs.

        I got the message! What's next?

        Install Sinatra and set up your development environment

        In order to receive and reply to incoming SMS messages, we'll need to create a very lightweight web application that can accept incoming requests. We'll use Sinatra for this Quickstart, but if you prefer to use Rails, you can find instructions in this blog post.

        First, you need a Gemfile with the following content on it.

        # Gemfile
        source ''

        gem 'sinatra'
        gem 'twilio-ruby'

        Ruby projects use Bundler to manage dependencies, so the command to pull Sinatra and the Twilio SDK into our development environment is bundle install.

        bundle install
        Use `bundle show [gemname]` to see where a bundled gem is installed.

        Create a simple Sinatra application

        We can test that our development environment is configured correctly by creating a simple Sinatra application. Copy the following code and drop it in a new file called quickstart.rb:

        # Download the twilio-ruby library from
        require 'sinatra'
        get '/' do
          "Hello World!"

        We can then try running our new Sinatra application with the command ruby quickstart.rb. You can then open http://localhost:4567 in your browser and you should see a "Hello World!" message.

        I'm set up! Show me how to let Twilio reach my application.

        Allow Twilio to talk to your Sinatra application

        We’re about to build a small Sinatra application to receive incoming messages. Before we do that, we need to make sure that Twilio can reach our application.

        Most Twilio services use webhooks to communicate with your application. When Twilio receives an SMS, for example, it reaches out to a URL in your application for instructions on how to handle the message.

        When you’re working on your Sinatra application in your development environment, your app is only reachable by other programs on your computer, so Twilio won’t be able to talk to it. We need to solve this problem by making your application accessible over the internet.

        While there are a lot of ways to do this, like deploying your application to Heroku or AWS, you'll probably want a less laborious way to test your Twilio application. For a lightweight way to make your app available on the internet, we recommend a tool called ngrok. Once started, ngrok provides a unique URL on the domain which forwards incoming requests to your local development environment.

        It works something like this:

        How ngrok helps Twilio reach your local server

        If you don’t already use ngrok, head over to their download page and grab the appropriate binary for your operating system. Once downloaded, unzip the package.

        If you're working on a Mac or Linux, you're all set. If you're on Windows, follow our guide on how to install and configure ngrok on Windows. For more info on ngrok, including some great tips and tricks, check out this in-depth blog post.

        Once you’ve got ngrok set up, start that Sinatra application we made previously:

        ruby quickstart.rb

        Your application must be running locally for ngrok to do its magic.

        Then open a new terminal tab or window and start ngrok with this command:

        ./ngrok http 4567

        4567 is the default port for Sinatra applications. If your local server is running on a different port, replace 4567 with the correct port number.

        You should see output similar to this:

        Ngrok server terminal output

        Copy your public URL from this output and paste it into your browser. If everything’s working correctly, you should see your Sinatra application’s "Hello World!" message.

        My server is publicly accessible. Teach me how to receive messages!

        Receive and reply to inbound SMS messages with Sinatra

        When someone sends an SMS to your Twilio phone number, Twilio makes an HTTP request to your server asking for instructions on what to do next. Once you receive the request, you can tell Twilio to reply with an SMS, kick off a phone call, store details about the SMS in your database, or trigger something else entirely - it’s all up to you!

        For this quickstart, we’ll have our Sinatra app reply to incoming SMS messages with a thank you to the sender. Open up quickstart.rb again and update the code to look like this code sample:

              When your phone number receives an incoming message, Twilio will send an HTTP request to your server. This code shows how your server should respond to reply with a text message.

              Reply SMS using Twilio

              When your phone number receives an incoming message, Twilio will send an HTTP request to your server. This code shows how your server should respond to reply with a text message.

              Save the file and restart your app with

              ruby quickstart.rb
              Double-check that ngrok is still running on your localhost port. Now Twilio will be able to find your application - but first, we need to tell Twilio where to look.

              Configure your webhook URL

              For Twilio to know where to look, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in.

              1. Log into and go to the Numbers page in the Console.
              2. Click on your SMS-enabled phone number.
              3. Find the Messaging section. The default “CONFIGURE WITH” is what you’ll need: "Webhooks/TwiML."
              4. In the “A MESSAGE COMES IN” section, select "Webhook" and paste in the URL you want to use (don’t forget the /sms-quickstart endpoint!). Set the HTTP verb to GET. Save your number settings.

              Configure webhook on phone number

              Test your application

              As long as your localhost and ngrok server are up and running, we’re ready for the fun part - testing our new Sinatra application!

              Send a text message from your mobile phone to your Twilio phone number. You should see an HTTP request in your ngrok console. Your Sinatra app will process the text message, and you’ll get your response back as an SMS.

              It worked! All done - what's next?

              Where to next?

              Now that you know the basics of sending and receiving SMS text messages with Ruby and Sinatra, you might want to check out these resources.

              We can't wait to see what you build!

              Rate this page:

              Need some help?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.