Ahoy there! All messaging transmitted using Twilio’s messaging channels is treated as Application-to-Person (A2P) messaging and subject to Twilio’s Messaging Policy. For detailed information on policy rules to ensure you remain compliant while using Twilio’s services, please see our Acceptable Use Policy.
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.
Already have a Twilio account? Go ahead and skip this section.
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 a series of questions to customize your experience.
- Once you finish the onboarding 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.
We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that next.
Once you have installed Homebrew, run the following command to install
brew tap twilio/brew && brew install twilio
The suggested way to install
twilio-cli is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.
Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.
- Add the
scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
- Install the app:
scoop install twilio
twilio-cli can be installed using the Advanced Package Tool (
apt) on most distributions such as Debian, Ubuntu, and Mint.
To do so, run the following commands in your terminal:
wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \ | sudo apt-key add - sudo touch /etc/apt/sources.list.d/twilio.list echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \ | sudo tee /etc/apt/sources.list.d/twilio.list sudo apt update sudo apt install -y twilio
For other installation methods, see the Twilio CLI Quickstart.
twilio login to get the Twilio CLI connected to your account. Visit https://www.twilio.com/console, and you’ll find your unique Account SID and Auth Token to provide to the CLI.
You can reveal your auth token by clicking on the eyeball icon:
If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. With the CLI, run:
twilio phone-numbers:buy:local --country-code US --sms-enabled
Replace US with your ISO-3166-1 country code if you would like a phone number in another country. If you aren't finding any SMS enabled numbers, try looking for a mobile number instead of a local number:
twilio phone-numbers:buy:mobile --country-code DE --sms-enabled
Select a phone number to add it to your account.
Next, we need to install Ruby and the Twilio Ruby Helper Library.
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.
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:
Windows users can use RubyInstaller to install Ruby.
The easiest way to install twilio-ruby is from RubyGems.
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 long error message that says Permission Denied in the middle of it, try running the above commands with sudo: sudo gem install twilio-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.
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 https://www.twilio.com/console 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':
send_sms.rb and replace the values for
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:
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.
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.
Ruby projects use Bundler to manage dependencies, so the command to pull Sinatra and the Twilio SDK into our development environment is
Use `bundle show [gemname]` to see where a bundled gem is installed.
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
require 'sinatra' get '/' do "Hello World!" end
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.
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:
Save the file and restart your app with
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.
Now, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. Just run this CLI command, replacing the phone number with your Twilio phone number:
twilio phone-numbers:update "+15017122661" --sms-url="http://localhost:4567/sms-quickstart"
The CLI will start an ngrok tunnel (so Twilio can reach your development machine) and wait patiently for incoming text messages!
Now that everything is glued together, it's time to test.
Send a text message from your mobile phone to your Twilio phone number. You'll see a couple of things happen very quickly -
- Your Sinatra dev server will note a new connection
- Twilio will forward your response back as an SMS!
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.
- Dive into the API Reference documentation for Twilio SMS
- Learn how to create an SMS conversation in Ruby
- Track the delivery status of your messages with Ruby
- Send an SMS during a phone call
- Learn how to build cool things with TwilioQuest, our interactive, self-paced game that teaches you how to Twilio.
We can't wait to see what you build!