Skip to contentSkip to navigationSkip to topbar
On this page

Modify Calls In Progress With Python


In this guide we'll show you how to manipulate live phone calls using Python. We'll also cover how to retrieve information about in progress and completed calls from your Twilio account.

We'll use Twilio's Python SDK in these examples to interact with the Twilio REST API's Calls endpoint.


Modifying Twilio phone calls in progress with Python

modifying-twilio-phone-calls-in-progress-with-python page anchor

The simplest way to control the flow of a Twilio phone call is with TwiML itself.

You can use the "action" parameters of verbs like <Gather> and <Record> to tell Twilio to get new instructions from your applications during a call. You can also use the <Redirect> verb to explicitly tell Twilio to fetch new TwiML.

But sometimes you need to change a live phone call outside of Twilio's normal request-response cycle. For those cases you can update the Call to tell Twilio to immediately change the TwiML it's using in a phone call.

Update an in progress Call with new TwiML instructionsLink to code sample: Update an in progress Call with new TwiML instructions
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
import os
3
from twilio.rest import Client
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
7
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
8
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
9
client = Client(account_sid, auth_token)
10
11
call = client.calls("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update(
12
twiml="<Response><Say>Ahoy there</Say></Response>"
13
)
14
15
print(call.sid)

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"annotation": null,
4
"answered_by": null,
5
"api_version": "2010-04-01",
6
"caller_name": null,
7
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
8
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
9
"direction": "inbound",
10
"duration": "15",
11
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
12
"forwarded_from": "+141586753093",
13
"from": "+14158675308",
14
"from_formatted": "(415) 867-5308",
15
"group_sid": null,
16
"parent_call_sid": null,
17
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"price": "-0.03000",
19
"price_unit": "USD",
20
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
21
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
22
"status": "completed",
23
"subresource_uris": {
24
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
25
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
26
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
27
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
28
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
29
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
30
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
31
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
32
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
33
},
34
"to": "+14158675309",
35
"to_formatted": "(415) 867-5309",
36
"trunk_sid": null,
37
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
38
"queue_time": "1000"
39
}

In order to update the call you will need to use the CallSid. Twilio returns this when you initiate an outgoing call, and also includes the CallSid in its request to your application for an incoming call's initial TwiML.

As an alternative to updating your call with TwiML directly, you may also redirect a Call to a new URL that responds with your requested TwiML.

1
# Download the helper library from https://www.twilio.com/docs/python/install
2
import os
3
from twilio.rest import Client
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
7
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
8
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
9
client = Client(account_sid, auth_token)
10
11
call = client.calls("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update(
12
method="POST", url="http://demo.twilio.com/docs/voice.xml"
13
)
14
15
print(call.sid)

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"annotation": null,
4
"answered_by": null,
5
"api_version": "2010-04-01",
6
"caller_name": null,
7
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
8
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
9
"direction": "inbound",
10
"duration": "15",
11
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
12
"forwarded_from": "+141586753093",
13
"from": "+14158675308",
14
"from_formatted": "(415) 867-5308",
15
"group_sid": null,
16
"parent_call_sid": null,
17
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"price": "-0.03000",
19
"price_unit": "USD",
20
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
21
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
22
"status": "completed",
23
"subresource_uris": {
24
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
25
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
26
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
27
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
28
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
29
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
30
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
31
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
32
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
33
},
34
"to": "+14158675309",
35
"to_formatted": "(415) 867-5309",
36
"trunk_sid": null,
37
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
38
"queue_time": "1000"
39
}

Hanging up a call in progress

hanging-up-a-call-in-progress page anchor

Twilio will end phone calls for you when it encounters a <Hangup> verb or when it runs out of TwiML to process. But you can also end phone calls whenever you like by passing a "completed" status to a CallSid in progress.

1
# Download the helper library from https://www.twilio.com/docs/python/install
2
import os
3
from twilio.rest import Client
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
7
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
8
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
9
client = Client(account_sid, auth_token)
10
11
call = client.calls("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update(
12
status="completed"
13
)
14
15
print(call.to)

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"annotation": null,
4
"answered_by": null,
5
"api_version": "2010-04-01",
6
"caller_name": null,
7
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
8
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
9
"direction": "inbound",
10
"duration": "15",
11
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
12
"forwarded_from": "+141586753093",
13
"from": "+14158675308",
14
"from_formatted": "(415) 867-5308",
15
"group_sid": null,
16
"parent_call_sid": null,
17
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"price": "-0.03000",
19
"price_unit": "USD",
20
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
21
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
22
"status": "completed",
23
"subresource_uris": {
24
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
25
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
26
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
27
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
28
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
29
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
30
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
31
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
32
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
33
},
34
"to": "+14158675309",
35
"to_formatted": "(415) 867-5309",
36
"trunk_sid": null,
37
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
38
"queue_time": "1000"
39
}

Check out Modifying Live Calls in the reference docs for more details.


Check out our tutorials to see full implementations of Twilio Voice in Python with Flask and Django.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.