Learn to modify live phone calls and change active call execution with Twilio Programmable Voice. To update an active Call resource, send a POST request with a Twilio SDK. You can use this guide to send automated notifications, create self-service automation, build inbound and outbound call centers, implement AI or ML transcription, and manage active call sessions.
To learn more about the Call resource and state management parameters used in this guide, see Related reference documentation.
To control the flow of a Twilio phone call, pass TwiML documents to Twilio through its SDKs. The typical workflow involves passing instruction sets in TwiML documents from your app to Twilio in two ways:
Change instructions to Twilio during a call through the action parameters of verbs like <Gather> and <Record>.
Request a different TwiML document from Twilio with the <Redirect> verb.
To change a live phone call outside of the normal Twilio request-response cycle, make a request to the Call resource.
Extract your credentials from your Twilio account then save these credentials as environment variables.
Twilio ConsoleLegacy Console
Go to the Twilio Console. The Let's get building page appears.
Click API keys and Auth tokens. The API keys & auth tokens page appears with the Auth Tokens tab selected.
Scroll to your Account SID.
Click the copy button next to your Account SID.
Run the following command, replacing YOUR_ACCOUNT_SID with your Account SID.
macOS TerminalWindows command linePowerShell
exportTWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
setTWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
$Env:TWILIO_ACCOUNT_SID="YOUR_ACCOUNT_SID"
This command creates an environment variable on your development system for your account SID.
To display the Auth Token, click the eye button in the Primary auth token box.
Highlight and copy the Auth Token.
Run the following command, replacing YOUR_AUTH_TOKEN with your Authentication Token.
macOS TerminalWindows command linePowerShell
exportTWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
setTWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
$Env:TWILIO_AUTH_TOKEN="YOUR_AUTH_TOKEN"
This command creates an environment variable on your development system for your auth token.
Go to the Twilio Console. The Get Started with Twilio page appears.
Scroll to the Account Info block.
Click the copy button next to your Account SID.
Run the following command, replacing YOUR_ACCOUNT_SID with your Account SID.
macOS TerminalWindows command linePowerShell
exportTWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
setTWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
$Env:TWILIO_ACCOUNT_SID="YOUR_ACCOUNT_SID"
Click the copy button next to your Auth Token.
Run the following command, replacing YOUR_AUTH_TOKEN with your Authentication Token.
macOS TerminalWindows command linePowerShell
exportTWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
setTWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
$Env:TWILIO_AUTH_TOKEN="YOUR_AUTH_TOKEN"
This command creates an environment variable on your development system for your auth token.
Modify an in-progress Twilio phone call
The Call resource request requires the CallSid of the ongoing call. To get the CallSid, choose one of the following two options:
Check the sid property in the response from Twilio for an outgoing call request.
Check the CallSid attribute in TwiML document that Twilio sends to your app acting on an incoming call.
Update the call instructions
To update a live phone call, pass the update parameter with your Call resource request. This request instructs Twilio to make an immediate change the TwiML document that Twilio is using in a phone call.
PythonNode.jsPHPC# or .NETJavaRuby
Python only requires the Twilio Python helper library as noted in the prerequisites.
Change an in-progress Call with updated TwiML instructions
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
importos
3
fromtwilio.restimportClient
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
When Twilio encounters a <Hangup> verb or runs out of TwiML documents to process, Twilio ends your phone call. To end a phone call by your choice, pass a completed status to a CallSid in progress.
PythonNode.jsPHPC# or .NETJavaRuby
Python only requires the Twilio Python helper library as noted in the prerequisites.
Terminate a Running Call
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
importos
3
fromtwilio.restimportClient
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
Use cases for modifying live calls with Twilio Programmable Voice
This guide teaches the basics required for the following use cases:
Notifications with Twilio Programmable Voice
You can use this guide to dynamically manage automated broadcast or alert calls while they are in progress. For example, you can programmatically redirect a notification call to a live agent queue if a customer requests immediate assistance, or terminate a call early once an urgent message has been acknowledged.
To learn more advanced features that you can use with notifications, see Voice notifications.
Create self-service automation with Twilio Programmable Voice
Together with your IVR workflows, you can use this guide to interrupt automated self-service paths based on real-time events. Seamlessly break a caller out of an automated data-gathering script if they request a human agent, or modify the call's direction dynamically when a backend database updates during the session.
To learn more advanced features that you can use with self-service automation, see Voice self-service automation.
Build inbound call centers with Twilio Programmable Voice
You can use this guide to implement essential call center features like warm transfers, supervisor barge-ins, and call holds. Modify active calls to pull customers out of queues as agents become available, bridge multiple parties together, or redirect callers to different departments based on live agent availability.
To learn more advanced features that you can use with inbound call centers, see Voice inbound call centers.
Implement AI or ML transcription with Twilio Programmable Voice
Together with Twilio live audio streaming, you can use this guide to control transcription services dynamically during a conversation. Programmatically start or stop transcription streams to protect user privacy when sensitive information is being spoken, or inject new routing rules into a call based on live keywords detected by your transcription engine.
To learn more advanced features that you can use with AI or ML transcription, see Voice AI and ML transcription.
Build AI agents with Twilio Programmable Voice
You can use this guide to manage interactions between human callers and autonomous voice bots in real time. Create human-in-the-loop takeover mechanisms that instantly intercept an active call and redirect it to a human supervisor if the AI agent detects frustration or encounters a complex request.
To learn more advanced features that you can use with AI agents, see Voice AI agents.
Build outbound call centers with Twilio Programmable Voice
You can use this guide to optimize live outbound interactions and agent utilization. Instantly modify the path of an outbound call to play a prerecorded message the moment an answering machine is detected, or dynamically shift active outbound legs between agent tiers based on shifting campaign priorities.
To learn more advanced features that you can use with outbound call centers, see Voice outbound call centers.
Build sales dialers with Twilio Programmable Voice
You can use this guide to power advanced click-to-talk workflows and lead response systems. Initiate a call to a sales representative first, and then dynamically modify that call in progress to dial and bridge the prospect the exact moment the agent goes off-hook and is ready to talk.
To learn more advanced features that you can use with sales dialers, see Voice sales dialers.
Build call tracking with Twilio Programmable Voice
You can use this guide to control proxy routing and number masking on active lines. Modify live call parameters to dynamically mask phone numbers for privacy, track campaign attribution data mid-flight, or update routing metrics as a call moves through various marketing touchpoints.
To learn more advanced features that you can use with call tracking, see Voice call tracking.
Result
After following this guide, you can programmatically modify live phone calls using Twilio Programmable Voice and the Twilio SDKs. You will have built code that can dynamically update call directions or explicitly update the call status to complete active sessions on your account.
Next steps
Explore the following guides to build on what you've learned in this guide: