Python Quickstart: Making an Outgoing Call

To make a call, submit a POST request to the Calls resource.

POST https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Calls

The twilio-python library makes this very easy to do. Open a file called make_call.py and write the following:

make_call.py
# Download the library from twilio.com/docs/libraries
from twilio.rest import Client

# Get these credentials from http://twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
client = Client(account_sid, auth_token)

# Make the call
call = client.api.account.calls\
      .create(to="+14085551234",  # Any phone number
              from_="+12125551234", # Must be a valid Twilio number
              url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")

print(call.sid)

Then make the call by running the following in your Terminal:

python make_call.py

Lets look at the details:

  • First over to the Twilio website and log into your Twilio Account page. On the Dashboard there is a section labeled "API Credentials". There you will find your Account SID and Auth Token. Copy those values and paste them into the Account SID and Auth Token variables.
  • We then instantiate a new client object, set the request method to 'POST', fill in the 'From', 'To' and 'Url' parameters in an associative array, and fire off the request to Twilio!

Call end callback

By default, your application does not get any notification when a call is complete, if the line is busy, or if no one answers. To get notifications about the status of a call, include the status_callback parameter with your request. The full list of parameters that can be passed is available in the twilio-python helper library documentation.

When the call is complete, or if the line is busy or no one answers, Twilio will make a request to the URL you specify in the status_callback parameter.


Next: Access your call logs via our API »

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.