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

How to Record Phone Calls in Python


(warning)

Warning

Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. Learn more about the new Console(link takes you to an external page).

In this guide we'll show you how to use Twilio Programmable Voice(link takes you to an external page) to record phone calls with your Python web application.

We'll learn how to record both inbound and outbound calls. In both examples we'll use Twilio's Python SDK.


Set up your web application

set-up-your-web-application page anchor
Incoming Voice.

Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to your web application asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that instructs Twilio on what to do with the call. Those instructions can direct Twilio to read out a message, play an MP3 file, make a recording and much more.

To start answering phone calls, you must:

  • Buy and configure a Twilio-powered phone number(link takes you to an external page) capable of making and receiving phone calls, and point it at your web application
  • Write a web application to tell Twilio how to handle the incoming call using TwiML
  • Make your web application accessible on the Internet so Twilio can make an HTTP request when you receive a call
(warning)

Warning

If you are sending SMS messages to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained by purchasing them. These restrictions do not apply to Voice or other uses outside of SMS messaging. Please click here(link takes you to an external page) for details.


Buy and configure a phone number

buy-and-configure-a-phone-number page anchor

In the Twilio Console, you can search for and buy phone numbers in countries around the world. Numbers that have the Voice capability can make and receive voice phone calls from just about anywhere on the planet.

Search for voice capable numbers.

Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook(link takes you to an external page). This can be done in the number's configuration page.

configure an incoming phone number URL.

Webhooks are user-defined HTTP(link takes you to an external page) callbacks. They are usually triggered by some event, such as receiving an SMS message or an incoming phone call. When that event occurs, Twilio makes an HTTP request (usually a POST or a GET(link takes you to an external page)) to the URL configured for the webhook.

To handle a webhook, you only need to build a small web application that can accept the HTTP requests. Almost all server-side programming languages offer some framework for you to do this. Examples across languages include ASP.NET MVC(link takes you to an external page) for C#, Servlets(link takes you to an external page) and Spark(link takes you to an external page) for Java, Express(link takes you to an external page) for Node.js, Django(link takes you to an external page) and Flask(link takes you to an external page) for Python, and Rails(link takes you to an external page) and Sinatra(link takes you to an external page) for Ruby. PHP(link takes you to an external page) has its own web app framework built in, although frameworks like Laravel(link takes you to an external page), Symfony(link takes you to an external page) and Yii(link takes you to an external page) are also popular.

Whichever framework and language you choose, webhooks function the same for every Twilio application. They will make an HTTP request to a URI that you provide to Twilio. Your application performs whatever logic you feel necessary - read/write from a database, integrate with another API or perform some computation - then replies to Twilio with a TwiML response with the instructions you want Twilio to perform.

What is TwiML?

what-is-twiml page anchor

TwiML is the Twilio Markup Language, which is just to say that it's an XML(link takes you to an external page) document with special tags defined by Twilio to help you build your SMS and voice applications. TwiML is easier shown than explained. Here's some TwiML you might use to respond to an incoming phone call:


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Thanks for calling!</Say>
_10
</Response>

And here's some TwiML you might use to respond to an incoming SMS message:


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Message>We got your message, thank you!</Message>
_10
</Response>

Every TwiML document will have the root <Response> element and within that can contain one or more verbs. Verbs are actions you'd like Twilio to take, such as <Say> a greeting to a caller, or send an SMS <Message> in reply to an incoming message. For a full reference on everything you can do with TwiML, refer to our TwiML API Reference.


Now comes the fun part — writing Python that will handle an incoming HTTP request from Twilio!

In this example we'll use the Flask web framework(link takes you to an external page) to respond to Twilio's request. We'll then use TwiML to tell Twilio how to handle the call.

Record part of an incoming call

record-part-of-an-incoming-call page anchor

Use the <Record> TwiML verb to record a message from the caller

Python

_25
from flask import Flask
_25
from twilio.twiml.voice_response import VoiceResponse
_25
_25
app = Flask(__name__)
_25
_25
_25
@app.route("/record", methods=['GET', 'POST'])
_25
def record():
_25
"""Returns TwiML which prompts the caller to record a message"""
_25
# Start our TwiML response
_25
response = VoiceResponse()
_25
_25
# Use <Say> to give the caller some instructions
_25
response.say('Hello. Please leave a message after the beep.')
_25
_25
# Use <Record> to record the caller's message
_25
response.record()
_25
_25
# End the call with <Hangup>
_25
response.hangup()
_25
_25
return str(response)
_25
_25
if __name__ == "__main__":
_25
app.run()

This code is generating the \<Record> TwiML verb, check the documentation for additional options and configuration. You can find all recordings in your console. In addition, you can make use of callbacks to be notified when a recording is ready for use.


Next up, we'll record an outgoing call by writing a little Python. First, we'll need to retrieve the credentials for our Twilio account.

Retrieve your Twilio account credentials

retrieve-your-twilio-account-credentials page anchor

First, you'll need to get your Twilio account credentials. They consist of your AccountSid and your Auth Token. They can be found on the home page of the console(link takes you to an external page).

Retrieve Your Twilio Credentials.

Make and record an outbound call

make-and-record-an-outbound-call page anchor

Just pass an extra argument, record, to the client.calls.create method and Twilio will record the entire phone call.

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
# 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
record=True,
_18
url="http://demo.twilio.com/docs/voice.xml",
_18
to="+14155551212",
_18
from_="+15017122661",
_18
)
_18
_18
print(call.sid)

Output

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

Once the call is complete, you can listen to your recordings in your Twilio Console or access them directly through Twilio's REST API.

You can also receive a Webhook request when your recording is ready by passing a RecordingStatusCallback URL with your client.calls.create command. Twilio will send a request to the URL you specified, and that request will include a link to the recording's audio file: look for the RecordingUrl field.

You can learn more about the RecordingStatusCallback parameter in the Making Calls reference docs.


We just learned to record inbound and outbound phone calls using Twilio Programmable voice and Python. If you'd like to learn more about how you can use Twilio and Python, check out these handy guides:


Rate this page: