Set up your Ruby and Sinatra development environment
This tutorial covers how to build a Twilio Voice web application in the Ruby language and Sinatra framework. It includes the configuration settings needed for Twilio webhook testing.
Time to complete: Approximately 20-45 minutes depending on your operating system and internet speed.
Before you begin this tutorial, configure, acquire, and install the following:
- Administrative access to your computer to install software
- A stable internet connection for downloading packages and dependencies
- Basic familiarity with using the Command Prompt or terminal.
- A Twilio account (free tier available)
- A Twilio phone number with voice capabilities (you can get one for free when you sign up for a Twilio account). For more information on setting up your Twilio phone number, check out the Twilio Voice Quickstart.
- Ruby 2.6 or later
- ngrok
- Visual Studio Code to write Ruby code. Though any text editor or IDE works, this tutorial provides examples and command for VS Code.
Using VS Code, create a directory and configure your Sinatra settings for your app:
-
Open VS Code.
-
To open the terminal in VS Code, go to View > Terminal.
-
Create a directory for your project.
1mkdir my-twilio-project2cd my-twilio-project3code . -
Install the required gems:
gem install sinatra rackup puma twilio-ruby
To create a Twilio app using Sinatra, create Ruby file, run it with Sinatra, and verify its response in a browser:
-
Add a file called
index.rb
to your workspace. -
with the following code:
1require 'rack'2require 'rack/handler/puma'3require 'twilio-ruby'45puts "Sinatra server starting..." # This confirms that the server is running.67class BasicApp8def call(env)9if env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == '/'10twiml = Twilio::TwiML::VoiceResponse.new do |response|11response.say(message: 'Hello World')12end13[200, {'Content-Type' => 'text/xml'}, [twiml.to_s]]14else15[404, {'Content-Type' => 'text/plain'}, ['Not Found']]16end17end18end1920options = {21Host: '0.0.0.0',22Port: 456723}24Rack::Handler::Puma.run(BasicApp.new, **options)25# This line of code bypasses Sinatra's protection middleware which allows external requests. -
Save this file in your workspace.
-
Run your Twilio Voice web app in the terminal.
ruby index.rb & -
Open your browser to
http://localhost:4567
. The browser should display:<?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>This verifies the proper configuration of your Ruby and Sinatra development environment and that you can access your app on your local system.
Web applications launched from your computer can't be accessed outside your computer. To communicate with your app, most Twilio services use webhooks.
For example: When Twilio receives an incoming phone call, it requests a URL in your app for instructions on how to handle the call.
To allow webhooks from Twilio to access your application, use ngrok.
-
Start your app as described previously.
-
Start ngrok with the following options:
ngrok http 4567Terminal output resembles the following:
1ngrok by @inconshreveable23Tunnel Status online4Version 3.x.x5Region us6Web Interface http://127.0.0.1:40407Forwarding http://<random_subdomain>.ngrok.io -> http://localhost:45678Forwarding https://<random_subdomain>.ngrok.io -> http://localhost:45679Connections ttl opn rt1 rt5 p50 p90100 0 0.00 0.00 0.00 0.00 -
A successful ngrok configuration results in your Twilio Voice app returning the following XML at your ngrok URL:
<?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>
Learn more about Twilio and Sinatra with the following resources: