Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Programmable Voice Quickstart for Python


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(link takes you to an external page) to ease development.

In this quickstart, you will learn how to:

  1. Sign up for Twilio and get your first voice-enabled Twilio phone number
  2. Set up your development environment to make and receive phone calls
  3. Make an outbound phone call which plays an MP3
  4. Receive and respond to an inbound phone call which reads a message to the caller using Text to Speech

Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with Python on Youtube(link takes you to an external page).


Sign up for Twilio and get a phone number

sign-up-for-twilio-and-get-a-phone-number page anchor
(information)

Info

If you already have a Twilio account and a voice-enabled Twilio phone number you're all set here! Log in(link takes you to an external page) 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(link takes you to an external page) to get your free trial account or log in(link takes you to an external page) to an account you already have.

The next thing you'll need is a voice-capable Twilio phone number(link takes you to an external page). 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(link takes you to an external page) page, check the "Voice" box and click "Search."

Search for a voice-enabled phone number.

_10
python --version

You should see something like:


_10
$ python --version
_10
Python 3.9 # Python 3.7+ is okay, too

Windows users can follow this excellent tutorial for installing Python on Windows(link takes you to an external page).

Twilio's Python SDK only supports Python 3.7+.

Install the Twilio Python helper library

install-the-twilio-python-helper-library page anchor

The easiest way to install the library is using pip(link takes you to an external page). Just run this in the terminal:


_10
pip 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:


_10
easy_install twilio

For a manual installation, you can download the source code (ZIP)(link takes you to an external page) for twilio-python and then install the library by running:


_10
python setup.py install

in the folder containing the twilio-python library.

And with that, it's time to write some code.


Make an outgoing phone call with Python

make-an-outgoing-phone-call-with-python page anchor

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.

Make a phone call using Twilio

make-a-phone-call-using-twilio page anchor
Python

_18
# Download the helper library from https://www.twilio.com/docs/python/install
_18
import os
_18
from twilio.rest import Client
_18
_18
_18
# Find your Account SID and Auth Token at twilio.com/console
_18
# and set the environment variables. See http://twil.io/secure
_18
account_sid = os.environ['TWILIO_ACCOUNT_SID']
_18
auth_token = os.environ['TWILIO_AUTH_TOKEN']
_18
client = Client(account_sid, auth_token)
_18
_18
call = client.calls.create(
_18
url='http://demo.twilio.com/docs/voice.xml',
_18
to='+15558675310',
_18
from_='+15017122661'
_18
)
_18
_18
print(call.sid)

Output

_37
{
_37
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"answered_by": null,
_37
"api_version": "2010-04-01",
_37
"caller_name": null,
_37
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"direction": "inbound",
_37
"duration": "15",
_37
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"forwarded_from": "+141586753093",
_37
"from": "+15017122661",
_37
"from_formatted": "(501) 712-2661",
_37
"group_sid": null,
_37
"parent_call_sid": null,
_37
"phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"price": "-0.03000",
_37
"price_unit": "USD",
_37
"sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37
"status": "completed",
_37
"subresource_uris": {
_37
"notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37
"recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37
"payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37
"events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37
"siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37
"streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37
"user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37
},
_37
"to": "+15558675310",
_37
"to_formatted": "(555) 867-5310",
_37
"trunk_sid": null,
_37
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
_37
"queue_time": "1000"
_37
}

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.

Replace the placeholder credential values

replace-the-placeholder-credential-values page anchor

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

Go to https://www.twilio.com/console(link takes you to an external page) 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:

Reveal Your Auth Token.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.