Quickstart

Getting started with the Twilio API couldn’t be easier. Create a Twilio REST client to get started. For example, the following code makes a call using the Twilio REST API.

Make a Call

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token
@call = @client.calls.create(
  to: "9991231234",
  from: "9991231234",
  url: "http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient"
)
puts @call.length
puts @call.sid

Send an SMS

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@message = @client.messages.create(
  to: "+12316851234",
  from: "+15555555555",
  body: "Hello there!"
)

Send an MMS

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@message = @client.messages.create(
  to: "+15558676309",
  from: "+15555555555",
  body: "Jenny I need you!",
  media_url: "http://twilio.com/heart.jpg"
)

Generating TwiML

To control phone calls, your application needs to output TwiML. Use twilio.twiml.Response to easily create such responses.

Twilio::TwiML::Response do |r|
    r.Play "https://api.twilio.com/cowbell.mp3", loop: 5
end.text
<?xml version="1.0" encoding="utf-8"?>
<Response>
    <Play loop="5">https://api.twilio.com/cowbell.mp3</Play>
<Response>

Create a Task with TaskRouter

If you need to create a Task to TaskRouter, you can do so by using the TaskRouterClient.

Additional resources had off of the workspace object (task_queues, workers, workflows, activities, tasks, statistics, events).

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
workspace_sid = "WSzzzzzzzzzzzzzzzzzzzzzzzzzzz"

client = Twilio::REST::TaskRouterClient.new account_sid, auth_token, workspace_sid

workflow_sid = "WFzzzzzzzzzzzzz"
task = client.workspace.tasks.create(attributes: '{"foo":"bar"}', workflow_sid: workflow_sid)

Digging Deeper

The full power of the Twilio API is at your fingertips. The User Guide explains all the awesome features available to use.