Twilio Connect is an easy way for developers to obtain authorization to make calls, send text messages, purchase phone numbers, read access logs and perform other API functions on behalf of another Twilio account holder.
As an example, imagine you want to access the Twilio account of a user of your web application to provide in-depth analytics of their Twilio account activity. In this quickstart we'll solve this problem by creating your first Twilio Connect App, placing the "Connect" button on your website so users can authorize your app to access their Twilio account data and make API requests against their account.
Let's jump right in and create our first Connect app. Log in to your Twilio account dashboard, select "Apps" and click the "Create Connect App" button. Fill in the top section with the name of your application and your company information.
Next, assign an Authorize URL to your Connect application. The Authorize URL is the URL that Twilio will redirect the user's browser to after they have authorized your application to access their Twilio account. Later on in the quickstart we'll demonstrate how the Authorize URL is used.
Lastly, select the access rights your Connect app requires on the user's account. For this example we will access call logs for analytics, so we'll choose "Read all account data".
Here's what our sample Connect application looks like:
Click 'Save Changes' and you're done!
The Connect button is where your customers will start the process of authorizing your Connect App to access their Twilio account. You can generate the code needed to place this button on your web site with the Twilio Connect button HTML generator.
After saving your application you will see a popup with HTML code. Copy the generated code and paste it into the HTML of your web site where you would like the button to appear. If you ever need to generate this HTML again, click on the "Generate Connect Button HTML" link at the bottom of your Connect App Details page.
With the Twilio Connect button now on your web site, browse to the page where you placed the HTML and click the Connect button. Verify that the information displayed on the authorization screen is correct.
After completing the app authorization process, you are redirected to the Authorize URL you specified when creating your Connect App. Appended to that URL is an Account Sid URL parameter with a value that looks like this:
http://www.example.com/twilio/authorize?AccountSid=AC12345 Your Connect App's Authorize URL Customer's SID
Your application should extract the AccountSid value from the URL and associate it with the user's account within your application. After extracting the AccountSid, we recommend that you redirect the user to another page within your app so the AccountSid isn't hanging around. Let's show an example using Ruby.
This tutorial assumes you have a Ruby development environment with Sinatra and the twilio-ruby helper library. If you don't have these yet, please see our post on setting up your Ruby environment.
require 'rubygems' require 'sinatra' require 'twilio-ruby' get '/connect' do sid = params['AccountSid'] # Store this account sid in your database, so you can retrieve it # later. You will need to write this section of the code. # Finally, redirect the user to your app after you've gathered their Sid redirect 'http://example.com/myapp' end
With the user's Account Sid in hand you can now request data from their account via the Twilio REST API. A request to retrieve data from a user's account is nearly identical to a request made against your own account, with one key difference. Instead of authenticating with your own AccountSid, you authenticate with the Account Sid retrieved during the authorization process and your account's Auth Token.
Here is a simple request to retrieve call logs from an account using the Ruby helper library. Pay special attention to line 4 where the customer's Account Sid is specified instead of your own:
require 'rubygems' # not necessary with ruby 1.9 but included for completeness require 'twilio-ruby' # Use your customer's account sid. account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Use your own auth token. auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' # set up a client to talk to the Twilio REST API client = Twilio::REST::Client.new(account_sid, auth_token) calls = client.calls.list begin calls.each do |call| puts call.sid + "\t" + call.from + "\t" + call.to end calls = calls.next_page end while not calls.empty?
Retrieving call logs on behalf of your customers is just the start of what you can accomplish with Twilio Connect. Visit the complete Connect documentation and best practices to learn more about how to integrate Connect's additional capabilities into your applications.