This guide shows how to set up a development environment in five minutes to trigger API requests and start serving Plivo XML to control your call flow. It also shows how to test your code using tunneling software to expose the local dev server to the public internet.
To get started, install Python, the Flask web framework, and Plivo’s Python SDK.
Linux and macOS users should already have Python installed. Windows users can download and install it. Follow instructions to install Flask.
To install the Plivo Python SDK, first create a project directory using the command mkdir mypythonapp
, then change to the directory and install the SDK using pip:
pip install plivo
Alternatively, you can download the source code from our GitHub repo and run
python setup.py install
We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo
with your commands and overwriting dependencies.
Now you can create a file in the project directory and execute code to trigger any Plivo API. Here’s some example code that makes an outbound call. Create a file called makecall.py and paste into it this code.
1
2
3
4
5
6
7
8
9
import plivo
client = plivo.RestClient('<auth_id>','<auth_token>')
response = client.calls.create(
from='<Caller_ID>',
to='<Destination_Number>',
answer_url='http://s3.amazonaws.com/static.plivo.com/answer.xml',
answer_method='GET')
print(response)
process.env
to store environment variables and fetch them while initializing the client. Save the file and run it.
python makecall.py
You can follow the same approach to trigger other API requests. Refer to our detailed API reference to see all the API requests available on the Voice API platform.
When you receive a call on a Plivo voice-enabled number, you can control the call flow by declaring an Answer URL for the Plivo application associated with that phone number. Plivo will invoke the Answer URL specified and expect a valid XML response to handle the call.
In addition to requests to the Answer URL, Plivo initiates other HTTP requests to your application server based on specific XML elements in your Answer XML document. Such requests are broadly classified into two categories:
Action URL requests: These requests are typically invoked at the end of an XML element’s execution, and the server expects XML instructions to carry forward the call in response to these requests. This happens, for example, when a caller provides Touch-Tone input during GetInput XML execution.
Callback URL requests: These requests serve as webhooks to pass the application server information about events through the course of an XML element’s execution, such as when a conference participant is muted or unmuted. No XML instructions are expected in response to these requests.
Here’s how to set up a Flask server to serve XML documents and manage callbacks.
Use this code snippet to start a local server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from flask import Flask, Response
from plivo import plivoxml
app = Flask(__name__)
@app.route('/receive_call/', methods=['GET','POST'])
def speak_xml():
# Generate a Speak XML with the details of the text to play on the call.
response = (plivoxml.ResponseElement()
.add(plivoxml.SpeakElement('Hello, you just received your first call')))
return Response(response.to_string(), mimetype='application/xml')
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
Save this code in any file — we called ours receive_call.py. To run this file on the server, go to the folder where this file resides and enter:
python receive_call.py
You should see your basic server app in action on http://localhost:5000/receive_call/.
To serve XML documents, your local server must connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.
Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (5000 in this case):
./ngrok http 5000
This will start the ngrok server on your local server. Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.
You should be able to see your basic server app in action on https://<nrgok_URL>/receive_call/.
You can follow the same approach to serve other XML documents to manage call flows. Refer to our detailed XML reference to check all the XML elements available on the Voice API platform.