If you already have a Twilio account and a voice-enabled Twilio phone number you’re all set here! Log in then feel free to jump to the next step.

Before you can make a phone call from Python, you'll need a Twilio account. Sign up here to get your free trial account or log in to an account you already have.

The next thing you'll need is a voice-capable Twilio phone number. If you don't currently own a Twilio phone number with voice call functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "Voice" box and click "Search."

You’ll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.

Now that you have a Twilio account and a programmable phone number, you have the basic tools you need to make a phone call.

You could use Twilio's HTTP API to make your phone calls, but we'll make things even more simple by using Twilio's official Python helper library. Let's install that next.

Twilio’s Python SDK supports both Python 2 and Python 3. You can use either version for your projects, but we recommend using Python 3 for this quickstart and future projects with Twilio unless there are specific libraries your project needs which are only compatible with Python 2.

Now that we have Python and twilio-python installed, we can make an outgoing phone call with a single API request from the Twilio phone number we just purchased. Create a new file called make_call.py and type or paste in this code sample.

Swap the placeholder values for account_sid and auth_token with your personal Twilio credentials.

Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the eyeball icon:

Open make_call.py and replace the values for account_sid and auth_token with your unique values.

Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.

Remember that voice-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing from_ number with that one, making sure to use E.164 formatting:

[+][country code][phone number including area code]

Next, replace the to phone number with your mobile phone number. This can be any phone number that can receive calls, but it’s a good idea to test with your phone so that you can see the magic happen! As above, you should use E.164 formatting for this value.

Save your changes and run the script from your terminal:

python make_call.py

That’s it! Your phone should ring with a call from your Twilio number, and you'll hear our short message for you. 😉

To handle incoming phone calls we'll need a lightweight web application to accept incoming HTTP requests from Twilio. We’ll use Flask for this quickstart, but you can use your choice of web framework to make and receive phone calls from your applications.

For instructions on setting up Flask on Windows, check out this guide.

To install Flask and set up our development environment, we’ll need two tools: pip to install Flask and virtualenv to create a unique sandbox for this project. If you already have these tools installed, you can skip to the next section.

Pip comes pre-packaged with Python 3.4+, so if you’re on a recent version of Python, you don’t need to install anything new. If you’re on an earlier version, never fear: pip is included in virtualenv. So let’s install virtualenv!

If you’re using Python 2.4, run the following command in your terminal:

easy_install virtualenv

If you’re using Python 2.5-2.7, run the following command in your terminal, specifying your version number:

We're about to enhance our small Flask application to accept incoming phone calls. But before we do that, we need to make sure Twilio can talk to our local development environment.

Most Twilio services use webhooks to communicate with your application. When Twilio receives a phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.

When you’re working on your Flask application in your development environment, your app is only reachable by other programs on your computer and Twilio won’t be able to talk to it. We need to solve this problem by making your application accessible over the internet.

While there are a lot of ways to do this, like deploying your application to Heroku or AWS, you'll probably want a less laborious way to test your Twilio application. For a lightweight way to make your app available on the internet, we recommend a tool called ngrok. Once started, ngrok provides a unique URL on the ngrok.io domain which forwards incoming requests to your local development environment.

If you don’t already use ngrok, head over to their download page and grab the appropriate binary for your operating system. Once downloaded, unzip the package.

When your Twilio number receives an incoming phone call, it sends an HTTP request to your server asking for instructions on what to do next. Once you receive the request, you can tell Twilio how to respond to the call.

For this quickstart, we’ll have our Flask app reply to answer the phone call and say a short message to the caller. Open up answer_phone.py again and update the code to look like this code sample:

Respond to an incoming request from Twilio with instructions on how to handle the call

Save the file and restart your app with

python answer_phone.py

You should now be able to open a web browser to http://localhost:5000/answer. If you view the page source code, you should see the following text:

<?xml version="1.0" encoding="UTF-8"?><Response><Sayvoice="alice">Thank you for calling! Have a great day.</Say></Response>

This source code is TwiML XML generated by your code with the help of the Twilio helper library.

Double-check that ngrok is still running on localhost with the same port as before. Now Twilio will be able to find your application. There's just one last thing we need before we're ready to call your app: we need to tell Twilio where to send its request.

As long as your localhost and the ngrok servers are up and running, we’re ready for the fun part - testing our new Flask application!

Make a phone call from your mobile phone to your Twilio phone number. You should see an HTTP request in your ngrok console. Your Flask app will process the incoming request and respond with your TwiML. Then you'll hear your message once the call connects.