Server-side quickstart for Programmable Voice
This quickstart shows you how to build a server-side application that makes and receives phone calls. The code in this quickstart makes an outbound call with the Twilio Voice API and it handles an inbound call with text-to-speech.
Select your programming language and complete the prerequisites:
-
Install Flask and Twilio's Python helper library. To install using pip, run:
pip install flask twilio
Follow these steps to get your Twilio account credentials and set them as environment variables.
- Go to the Twilio Console.
- Copy your Account SID and set it as an environment variable using the following command. Replace YOUR_ACCOUNT_SID with your actual Account SID.
export TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
- Copy your Auth Token and set it as an environment variable using the following command. Replace YOUR_AUTH_TOKEN with your actual Auth Token.
export TWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
Follow these steps to make a phone call from your Twilio number.
- Create a file named
make_call.py
and add 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(12url="http://demo.twilio.com/docs/voice.xml",13to="+18005550100",14from_="+18005550199",15)1617print(call.sid) - Replace the
from_
phone number with your Twilio number. - Replace the
to
phone number with your own phone number. - Save your changes.
- Run the script:
Your phone rings and you hear the short message in the TwiML document that your script links to.python make_call.py
Follow these steps to receive a phone call with your Twilio number and respond using text-to-speech.
-
Create a file named
answer_phone.py
and add the following code:1from flask import Flask2from twilio.twiml.voice_response import VoiceResponse34app = Flask(__name__)567@app.route("/", methods=['GET', 'POST'])8def hello_monkey():9"""Respond to incoming calls with a simple text message."""1011resp = VoiceResponse()12resp.say("Hello from your pals at Twilio! Have fun.")13return str(resp)141516if __name__ == "__main__":17app.run(debug=True) -
Save the file.
-
In a new terminal window, run the following command to start the Python development server:
python answer_phone.py -
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrok http 5000 -
Set up a webhook that triggers when your Twilio phone number receives a phone call:
- Go to the Active Numbers page in the Twilio Console.
- Click your Twilio phone number.
- Go to the Configure tab and find the Voice Configuration section.
- In the A call comes in row, select the Webhook option.
- Paste your ngrok public URL in the URL field. For example, if your ngrok console shows
Forwarding https://1aaa-123-45-678-910.ngrok-free.app
, enterhttps://1aaa-123-45-678-910.ngrok-free.app
. - Click Save configuration.
-
With the Python development server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in
answer_phone.py
.
- Browse reference documentation for the Programmable Voice API
- Learn about TwiML markup language for responding to voice calls
- Browse all voice tutorials by topic