Accessing REST Resources

To access Twilio REST resources, you’ll first need to instantiate a Twilio::REST::Client.

Authentication

The Twilio::REST::Client needs your Twilio credentials. To get your credentials, visit your Twilio account portal. If you don’t have a Twilio account yet, go here to get started.

Once you have your credentials, you can create create a new Client and get started.

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

You can also configure the client using the top level configure method, like so:

require 'twilio-ruby'

Twilio.configure do |config|
  config.account_sid = "ACXXXXXXXXXXXXXXXXX"
  config.auth_token = "YYYYYYYYYYYYYYYYYY"
end

@client = Twilio::REST::Client.new

Listing Resources

The Client gives you access to various list resources. ListResource.list(), by default, returns the most recent 50 instance resources.

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
    @calls = @client.calls.list()

ListResource.list() accepts paging arguments. The following will return page 3 with page size of 25.

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
    @calls = @client.calls.list(page: 3, page_size: 25)

Get an Individual Resource

To get an individual instance resource, use ListResource.get(). Provide the sid of the resource you’d like to get.

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.get("CA123")
puts @call.to

Deleting a Resource

Resources can only be deleted via their instance object. This means you must instantiate an instance object using ListResource.get() and then call delete() on it.

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

@recording = @client.recordings.get("RC123")
@recording.delete()