Make outbound phone calls
In this tutorial, you'll learn how to make and manage outbound phone calls with Twilio Programmable Voice and your preferred programming language. You'll use the Calls resource in the Twilio Voice API.
- Install Python.
- Install the Twilio Python SDK. To install using pip, run:
pip install twilio
- Sign up for Twilio and get a phone number:
- Sign up for Twilio.
- To get a phone that has voice capabilities, do either of the following in the Twilio Console:
- In the Account Dashboard, click Get a phone number.
- In the navigation menu, go to Phone Numbers > Manage > Buy a number.
 
- In the Account Dashboard, copy your Account SID and Auth Token and paste them in a temporary local file for use later in this quickstart.
 
- 
Create and open a new file called make_call.pyanywhere on your machine and paste in the following code:1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011call = client.calls.create(12twiml="<Response><Say>Ahoy, World</Say></Response>",13to="+14155551212",14from_="+15017122661",15)1617print(call.sid)To learn all of the API response values that you can return with print(), see the response for Create a call in the API documentation. Precede the response value withcall.(for example:print(call.sid)returns thesidvalue).
- 
Set the environment variables for your Account SID and Auth Token. (warning)Improve security with API keysTo better control access, use API keys instead of the Account SID and Auth Token when you deploy to production. To learn more, see Why you should use API keys. - 
On Mac or Linux: - Run the following commands to add your credentials as environment variables in a twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID and replaceyour_auth_tokenwith your Auth Token.
 1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_AUTH_TOKEN='your_auth_token'" >> twilio.env3source ./twilio.env- 
If you're committing code with git, run the following command to add the twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
 
- Run the following commands to add your credentials as environment variables in a 
- 
On Windows command line (cmd.exe): Run the following commands. Replace ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID and replaceyour_auth_tokenwith your Auth Token.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_AUTH_TOKEN=your_auth_token
 Learn more about storing your Twilio credentials safely. 
- 
- 
In the make_call.pyfile, do the following:- 
Replace the value for towith the recipient phone number in E.164 format.
- 
Replace the value for fromwith your Twilio phone number in E.164 format.
- 
Replace the value for twimlwith your desired TwiML instructions.- 
To use a hosted TwiML URL, replace twimlwithurland set the value to the URL that hosts your TwiML instructions. For example:url="http://demo.twilio.com/docs/voice.xml"The following voice.xmlfile is hosted athttp://demo.twilio.com/docs/voice.xmluses the<Say>and the<Play>TwiML tags to read a message and play an MP3 file for the user.1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Say voice="woman">Thanks for trying our documentation. Enjoy!</Say>4<Play>http://demo.twilio.com/docs/classic.mp3</Play>5</Response>6</xml>
- 
To use a TwiML application to handle calls, replace twimlwithapplication_sidand set the value to your TwiML application's SID. You can create and configure applications on the TwiML Apps page in the Twilio Console.application_sid="<YOUR_TWIML_APP_SID>"When your outbound call is connected, Twilio will make a request to the Voice URL set on your application. (warning)WarningThe application_sidtake precedence over thetwimlandurlparameters and ignores the following parameters:method,fallback_url,fallback_method,status_callback,status_callback_method. Twilio expects that your application handles all of this information. Learn more about the Calls resource.
 
- 
 
- 
- 
Save your changes and run this command from your terminal in the directory that contains make_call.py:python make_call.py
After a few moments, you receive a call from your Twilio number.
Optionally, you can configure additional parameters when making outbound calls to customize call behavior. Below are some common use cases.
To dial an extension, set the SendDigits parameter to the sequence of digits to send after the call connects.
- You can include any digit (0-9),A,B,C,D,#, or*.
- To add pauses, use wfor a half-second pause orWfor a one-second pause between digits.
Learn more about the request body parameters.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createCall() {11const call = await client.calls.create({12from: "+18668675310",13method: "GET",14sendDigits: "1234#",15to: "+14155551212",16url: "http://demo.twilio.com/docs/voice.xml",17});1819console.log(call.sid);20}2122createCall();
Response
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+18668675310",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14155551212",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
To receive the outcome of calls, set the StatusCallback parameter to your status callback URL.
After Twilio completes your call, it'll make an asynchronous request to the URL. If you don't provide a StatusCallback URL, Twilio will end the call without sending any status updates to your application.
- To specify the HTTP request method Twilio should use when making requests to the StatusCallbackURL, set theStatusCallbackMethodparameter to eitherGETorPOST. The default isPOST.
- To specify which call progress events should trigger a request to your StatusCallbackURL, set theStatusCallbackEventparameter to eitherinitiated,ringing,answered, orcompleted. To specify multiple values, separate them with a space. The default iscompleted.
Learn more about the StatusCallback and StatusCallbackEvent parameters.
Warning
The StatusCallback URL must contain a valid hostname. You can't use underscores.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createCall() {11const call = await client.calls.create({12from: "+18668675310",13method: "GET",14statusCallback: "https://www.myapp.com/events",15statusCallbackEvent: ["completed"],16statusCallbackMethod: "POST",17to: "+14155551212",18url: "http://demo.twilio.com/docs/voice.xml",19});2021console.log(call.sid);22}2324createCall();
Response
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+18668675310",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14155551212",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
Warning
Recording a call is subject to the same obligations and requirements as the Recordings resource and the <Record> TwiML verb. For workflows subject to PCI or the Health Insurance Portability and the Accountability Act (HIPAA), see the applicable documentation.
To record your outbound call, set the Record parameter and set its value to true. You can also configure the following optional parameters to manage your call recordings:
- To receive the recording and other information about the call, set the RecordingStatusCallbackparameter to a URL that Twilio will request when the recording is available.
- To specify the HTTP request method Twilio should use when making requests to the RecordingStatusCallbackURL, set theRecordingStatusCallbackMethodparameter to eitherGETorPOST. The default isPOST.
- To specify which recording status changes should trigger a request to your RecordingStatusCallbackURL, set theRecordingStatusCallbackEventparameter to eithercompleted,absent, orin-progress. To specify multiple values, separate them with a space.
Learn more about the RecordingStatusCallback and RecordingStatusCallbackEvent parameters.
To programmatically pause, resume, or stop recordings, see the Recordings resource and How to record phone calls.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createCall() {11const call = await client.calls.create({12from: "+15017122661",13record: true,14recordingStatusCallback:15"https://www.example.com/recording-status-callback",16recordingStatusCallbackEvent: ["completed", "in-progress"],17recordingStatusCallbackMethod: "POST",18to: "+14155551212",19url: "http://demo.twilio.com/docs/voice.xml",20});2122console.log(call.sid);23}2425createCall();
Response
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+15017122661",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14155551212",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
After Twilio completes your call, it'll make an asynchronous request to your status callback URL. From here, it's up to you to decide what to do next. For example, you can trigger another event like send an SMS to the phone number you just called with a follow-up message, or try to place the call again if the call status returns failed.
- Learn more about the Calls resource and all of its parameters to customize your outbound calls.
- Add voice capabilities to your web application with the Voice JavaScript SDK or Twilio's mobile client SDKs for your Android or iOS applications.
- View more Voice tutorials to learn how to build interactive voice response (IVR) systems, call recording, and more.