Twilio Client for Android Quickstart Tutorial

Configure a Placeholder TwiML Application

Visit TwiML Apps in the Twilio Account Portal and click 'Create TwiML App'. Enter 'Mobile Quickstart' as the app's Friendly Name and click 'Save'. Don't worry about the Voice and Messaging URLs yet--we'll fill them in shortly.

Once the application is saved, make note of its ApplicationSid (a long string like "APa4983020a0b29fb5db1b1dc68e322fbe"). We'll need this in the upcoming steps.

Setup the back-end Application Server

Next, we'll deploy a server to perform two functions:

  1. Provide capability tokens to our Android client application.
  2. Serve up our TwiML application and/or make REST API calls to orchestrate Twilio's telephony services.

Quick Tour of the Application

The code for the Quickstart Tutorial server-side application can be found in the Server/ folder inside the SDK tarball. Feel free to open up and take a look. The app uses Python's Flask framework and exposes three endpoints:

  • / generates TwiML to tell Twilio to play "Welcome to Twilio" whenever you place a call.
  • /call generates TwiML to route calls to/from the PSTN and your Android application.
  • /token returns a capability token to your Android application.

Deploying the Server

Now, we need to make this application available via the public Internet. You can do this with either of the following methods, or another approach to deploy a web server you're comfortable with.

Option 1: Use Heroku

The easiest way to deploy the application is Heroku. To do this, do the following:

  1. Visit the Twilio Mobile QuickStart repository on Github and click the "Deploy to Heroku" button. If you don't have a Heroku account, you'll be led through the process to set one up.
  2. Once you've signed into Heroku, you'll be taken to a form to deploy the Quickstart application.
  3. Enter your Twilio Account Sid and your Twilio Auth Token as the ACCOUNT_SID and AUTH_TOKEN values, respectively.
  4. Enter the TwiML App Sid you created above as the value for APP_SID.
  5. Enter any caller ID that you've verified with your Twilio account in the CALLER_ID field.
  6. Click Create your application.
  7. Once Heroku finishes, open up a second tab and return to the configuration screen for your TwiML app. Configure the Voice URL to point to your Heroku application's server /call URL and save the change.

Option 2: Run Locally Using ngrok

With this method, you'll run Flask on your local box and create a publicly-addressable tunnel to it using ngrok.

  1. Open and modify ACCOUNT_SID and AUTH_TOKEN to match the values from your Twilio account.
  2. Modify APP_SID to match the App Sid you created above.
  3. Modify CALLER_ID to any caller ID that you've verified with your Twilio account.
  4. Install all the required python dependencies by running pip install -r requirements.txt from the Server/ folder at the command line.
  5. Download and install ngrok.
  6. Run python to start the server.
  7. Run ngrok 5000 to open up a tunnel from the public internet to your server.
  8. Return to the configuration screen for your TwiML app. Configure the Voice URL to point to your ngrok applications's server /call URL and save the change.

Testing your Server

To make sure your server deployed successfully, open a web browser and enter http://{your heroku app or ngrok address}/ and view source on the resulting page. If everything is working, you'll see some XML that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <Say>Welcome to Twilio</Say>

This is a simple TwiML application that will play an announcement whenever you make a call from your Android application.

Now that we have the server-side dependencies setup, let's get started on our Android app. You can test the examples below in the Android Emulator or on your device.

Next: Make Outgoing Calls »