With just a few lines of code, your Python application can make and receive phone calls with Twilio Programmable Voice.
This Python quickstart will teach you how to do this using our REST API, the Twilio Python helper library, and Python's Flask microframework to ease development.
In this quickstart, you will learn how to:
Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with Python on Youtube.
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."
_10python --version
You should see something like:
_10$ python --version_10Python 3.9 # Python 3.7+ is okay, too
Windows users can follow this excellent tutorial for installing Python on Windows.
Twilio's Python SDK only supports Python 3.7+.
The easiest way to install the library is using pip. Just run this in the terminal:
_10pip install twilio
If you get the error pip: command not found
, you can use easy_install
to install the Twilio helper library by running this in your terminal:
_10easy_install twilio
For a manual installation, you can download the source code (ZIP) for twilio-python and then install the library by running:
_10python setup.py install
in the folder containing the twilio-python library.
And with that, it's time to write some code.
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.
This code starts a phone call between the two phone numbers that we pass as arguments. The 'from' number is our Twilio number, and the 'to' number is who we want to call.
The URL argument points to some TwiML, which tells Twilio what to do next when our recipient answers their phone. This TwiML tells Twilio to read a message using text to speech and then play an MP3.
Before this code will work, though, we need to edit it a little to work with your Twilio account.
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: