Android Quickstart: Setting up your Server

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.

Set up the back-end Application Server

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

  1. Provide a Capability Token to our Android application.
  2. Serve up TwiML; this instructs Twilio what to do when our client attempts to establish an outbound call.

Quick Tour of the Application

The code for the Quickstart Tutorial server application can be found in the Mobile Quickstart repository that we downloaded in the previous step. You may want to open server.py and take a look. The app uses the popular Flask framework and exposes three endpoints:

  • '/' generates TwiML which tells Twilio to simply echo "Welcome to Twilio" - we will use this to verify that everything is wired up correctly.
  • '/call' generates TwiML which can route a call between the phone network and your Android application.
  • '/token' returns a Capability Token to your Android application.

Deploying the Server

Now, we need to make this application accessible via the public Internet. Why? Because Twilio's servers (which are connected to the phone network) need to access your server in order to fetch instructions for how your Twilio calls should be handled. This is a standard requirement for any Twilio-powered application. You can do this with either of the following methods, or use another approach that you prefer.

Option 1: Use Heroku

One of the easiest ways to deploy the application is using Heroku. To do this:

  1. Visit the Twilio Mobile QuickStart repository on Github and click the purple "Deploy to Heroku" button about half-way down the README page. 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 and provide Config Variables.
  3. Enter your Twilio Account Sid and your Twilio Auth Token as the ACCOUNT_SID and AUTH_TOKEN values, respectively. These can be found on the landing page of your Twilio Account Portal (click 'Show API Credentials').
  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 Twilio in the CALLER_ID field. You can verify a new Caller ID here. Alternatively, can also use any Twilio Phone Number that you've purchased.
  6. Click "Deploy for Free" to finish launching your application.
  7. Once Heroku finishes, launch a second tab and return to the configuration screen for your TwiML app. Enter your Heroku application's base URL as the Voice Request URL for your application and save the change:

    Request URL: https://bosco-baracus-1201.herokuapp.com/

Option 2: Run Locally Using ngrok

With this method, you'll run Flask on your local machine using python, then create a publicly-addressable 'secure tunnel' to it using ngrok.

  1. Open server.py 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. You can verify a new Caller ID here. Alternatively, can also use any Twilio Phone Number that you've purchased.
  4. Install all the required python dependencies by running pip install -r requirements.txt from the Server/ directory at the command line.
  5. Download and install ngrok.
  6. Run python server.py to start the server.
  7. Run ngrok http 5000 to open up a tunnel from the public internet to your server.
  8. Return to the configuration screen for your TwiML app. Enter your ngrok base URL as the Voice Request URL for your application and save the change:

    Request URL: https://d75ca7cc.ngrok.io/

Testing your Server

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

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

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 »