Rate this page:

Record Phone Calls in Ruby

Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. Learn more about the new Console.

In this guide we'll show you how to use Programmable Voice to record phone calls with your Ruby web application. We'll learn to record both inbound calls and outbound calls.

You can tell Twilio to record part of a phone call or the entire thing. In both examples we'll use Twilio's Ruby SDK.

Set up your web application

Incoming Voice

Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to your web application asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that instructs Twilio on what to do with the call. Those instructions can direct Twilio to read out a message, play an MP3 file, make a recording and much more.

To start answering phone calls, you must:

  • Buy and configure a Twilio-powered phone number capable of making and receiving phone calls, and point it at your web application
  • Write a web application to tell Twilio how to handle the incoming call using TwiML
  • Make your web application accessible on the Internet so Twilio can make an HTTP request when you receive a call

If you are sending SMS messages to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained by purchasing them. These restrictions do not apply to Voice or other uses outside of SMS messaging. Please click here for details.

Buy and configure a phone number

In the Twilio Console, you can search for and buy phone numbers in countries around the world. Numbers that have the Voice capability can make and receive voice phone calls from just about anywhere on the planet.

Search for voice capable numbers

Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook. This can be done in the number's configuration page.

configure an incoming phone number URL

What is a Webhook?

Webhooks are user-defined HTTP callbacks. They are usually triggered by some event, such as receiving an SMS message or an incoming phone call. When that event occurs, Twilio makes an HTTP request (usually a POST or a GET) to the URL configured for the webhook.

To handle a webhook, you only need to build a small web application that can accept the HTTP requests. Almost all server-side programming languages offer some framework for you to do this. Examples across languages include ASP.NET MVC for C#, Servlets and Spark for Java, Express for Node.js, Django and Flask for Python, and Rails and Sinatra for Ruby. PHP has its own web app framework built in, although frameworks like Laravel, Symfony and Yii are also popular.

Whichever framework and language you choose, webhooks function the same for every Twilio application. They will make an HTTP request to a URI that you provide to Twilio. Your application performs whatever logic you feel necessary - read/write from a database, integrate with another API or perform some computation - then replies to Twilio with a TwiML response with the instructions you want Twilio to perform.

What is TwiML?

TwiML is the Twilio Markup Language, which is just to say that it's an XML document with special tags defined by Twilio to help you build your SMS and voice applications. TwiML is easier shown than explained. Here's some TwiML you might use to respond to an incoming phone call:

<?xml version="1.0" encoding="UTF-8"?>
    <Say>Thanks for calling!</Say>

And here's some TwiML you might use to respond to an incoming SMS message:

<?xml version="1.0" encoding="UTF-8"?>
    <Message>We got your message, thank you!</Message>

Every TwiML document will have the root <Response> element and within that can contain one or more verbs. Verbs are actions you'd like Twilio to take, such as <Say> a greeting to a caller, or send an SMS <Message> in reply to an incoming message. For a full reference on everything you can do with TwiML, refer to our TwiML API Reference.

Record an inbound call.

Now comes the fun part - writing code that will handle an incoming HTTP request from Twilio!

In this example we'll use the Sinatra web framework to respond to Twilio's request and we'll use TwiML to tell Twilio how to handle the call.

Loading Code Sample...
        Use the <Record> TwiML verb to record a message from the caller

        Record part of an incoming call

        Use the <Record> TwiML verb to record a message from the caller

        Record an outbound call

        Next, let's record an outbound call. First, we'll need to retrieve the credentials for our Twilio account.

        Retrieve your Twilio account credentials

        First, you'll need to get your Twilio account credentials. They consist of your AccountSid and your Auth Token. They can be found on the home page of the console.

        Retrieve Your Twilio Credentials

        Make and record an outbound call

        Just pass an extra "record" argument to "client.account.calls.create()" and Twilio will record the entire phone call.

        Loading Code Sample...

              Record an outbound call

              Once the call is complete, you can listen to your recordings in your Twilio Console or access them directly through Twilio's REST API.

              You can also gain access to the recording as soon as the call is complete by including a "StatusCallbackUrl" with your "client.account.calls.create()" command. At the end of the call Twilio will send a request to the URL you specified, and that request will include a link to the recording's audio file.

              You can learn more about the "StatusCallbackUrl" parameter in the Making Calls reference docs.

              What's next?

              Hector Ortega Rob Spectre Ricky Robinett David Prothero Kevin Whinnery Andrew Baker Daniel Erazo Brianna DelValle Mathew Roberts
              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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

              Loading Code Sample...

                    Thank you for your feedback!

                    Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!