How to Send an SMS in 30 Seconds with Ruby
Time to read: 4 minutes
Twilio is all about powering communication, regardless of the language—or language framework—that you're using for a given project.
In this tutorial, you'll learn how to send an SMS directly from a Ruby application. What's more, you're going to use Twilio's Ruby Helper Library to make sending an SMS even faster than it would otherwise be. Ready to go? Let's dive in!
To complete the tutorial, you will need the following things:
- A free Twilio account (sign up with this link and get $10 in free credit when you upgrade your account)
- Ruby, version 2.7 or later. If you don't already have Ruby installed, you can install it using your operating system's package manager (if you're using Linux or macOS) or download it.
- A smartphone with active service, to test the project.
- The Twilio CLI (optional)
As we're sending an SMS, you're also going to need a Twilio phone number, one that is SMS capable.
If you already have a Twilio phone number, you can retrieve it through the Twilio Console. To do so, go to "Phone Numbers > Active Numbers" and copy a phone number that is SMS capable from the "Number" column.
NOTE: If you are using a free account you will be using your trial credit for this purchase.
On the “Buy a Number” page, select your country and check “SMS” in the capabilities field. If you’d like to request a number from your region, enter your area code in the “Number” field.
Click the “Search” button to see what numbers are available. Then click “Buy” for the number that you want from the results. After you confirm your purchase, click the “Close” button.
To be able to send an SMS via Twilio, the Ruby application needs four things:
- Your Twilio “Account SID”.
- Your Twilio “Auth Token”.
- A phone number for the sender.
- A phone number for the recipient.
NOTE: Your Twilio Account SID and Auth Token are required to authenticate with Twilio.
The sender's phone number is your existing (or newly purchased) Twilio phone number. The recipient's phone number will likely be your mobile phone number (or a friend's, perhaps).
The most convenient and secure way to store this information is to set environment variables for each of them.
You can find your Twilio “Account SID” and “Auth Token” on the dashboard of the Twilio Console:
With the Twilio credentials and two phone numbers available, in your terminal, set them as environment variables by running the following commands—after replacing the placeholders with the relevant information.
NOTE: If you are following this tutorial on a Windows computer, use
set instead of
export to define your environment variables in the command prompt. For the phone numbers, use theE.164 format, which includes a plus sign and the country code. If you want to learn more about environment variables, check out our how to set environment variables tutorial.
To keep the project files nice and tidy, create a new project directory, named twilio-sms-ruby, then cd into the new project directory using the commands below.
Then, run the command below to install the Gem.
With the environment variables and Twilio Gem ready, it's time to write the Ruby code. There's not all that much of it, to be honest, which you can see below. Create a new file, named send-sms.rb, and copy the code below into it.
client = Twilio::REST::Client.new(
message = client.messages.create(
body: "Hello from Ruby",
The code starts off by importing the twilio-ruby Gem. It then initializes a new variable,
client, using the Twilio Account SID and Auth Token retrieved from the respective environment variables. This object handles authenticating and interacting with Twilio's API.
After that, it calls the
client.messages.create method, passing in three things:
- The message to send. Feel free to change this to whatever you like, so long as it is not longer than 1600 characters.
- The recipient's phone number.
- The sender's phone number.
Both phone numbers are retrieved from the respective environment variables, which we exported earlier.
create method sends the SMS and stores the JSON response in
message. If the SMS was sent successfully, the response will include a lot of helpful information, including the SMS' sid, which is printed to the terminal. If it was not sent successfully, then the response will include relevant error details.
Now that the code's ready, it's time to test it. To do that, run the command below.
Assuming that the SMS was sent successfully, in a short while you (or your friend) will receive the SMS on your mobile phone!
NOTE: that if you are using a free Twilio account, Twilio requires that the number that you use as an SMS recipient is verified in your account. This requirement does not apply if you are using a paid account.
You've now learned the essentials. I'm sure you can think of so many different applications for sending SMS' in your applications. Be sure to check out the documentation, as well as the Message Resource Reference to learn about the complete functionality available.
I can’t wait to see what you build with Twilio and Ruby!
Matthew Setter is a PHP Editor in the Twilio Voices team and a polyglot developer. He’s also the author of Mezzio Essentials and Docker Essentials. When he’s not writing PHP code, he’s editing great PHP articles here at Twilio. You can find him at firstname.lastname@example.org; he's also settermjd on Twitter and GitHub.
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.