Set up your Python dev environment for Voice Calls

    In this guide, you will learn how to set up a development environment in under 5 minutes to trigger API requests and to start serving valid Plivo XML to control your call flow. Also, Using a tunnelling software like ngrok, we will see how you can expose your local dev server with a public address to test out your integration with Plivo.


    Install Python and the Plivo Python SDK

    You must set up and install Python and Plivo’s Python SDK to make your first call. Here’s how.

    Install Python

    Operating SystemInstructions
    OS X & LinuxYou would already have Python installed, you can check this by running the command python --version in the terminal
    WindowsTo install Python on Windows you can follow the instructions from here.

    Install Plivo Python Package

    • Create a project directory, run the following command:

      $ mkdir mypythonapp
    • Change the directory to our project directory in the command line:

      $ cd mypythonapp
    • Install the SDK using pip

      pip install plivo
    • Alternatively, you can download the source code from this repo and run

      python install

    We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo with your commands and overwriting dependencies.

    Trigger an API Request

    To trigger any Plivo API, you can create a file in the project directory and execute the code. Please check below example to make an outbound call:

    • Create a file called and paste the below code.
    import plivo
    client = plivo.RestClient('auth_id','auth_token')
    response = client.calls.create(
    • Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use os module(os.environ) to store environment variables and fetch them while initializing the client.
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be making calls to.
    • Both +14151234567 and +14157654321 should be in E.164 format
    • Save the file and use the below command to run it.
    Note: You can follow the same approach to trigger any other API requests. Please refer to the detailed API reference to check all the API requests available on the Voice API platform.

    Serve an XML Document and Manage Callbacks

    When a call is received on a Plivo voice-enabled number, you can control the call flow by declaring an answer URL for your Plivo application associated with that Plivo phone number. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call. We will see how to create and set up a new Plivo Application through the Plivo console in the following section.

    Notice how the concept of Answer URLs applies to both outbound API calls as well as incoming calls to your Plivo numbers. In the outbound API call example above, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.

    In addition to requests to the answer URL, Plivo initiates HTTP requests to your application server through the course of a call based on the specific XML elements in your answer XML. Such requests can be broadly classified into two categories:

    Action URL Requests: XML instructions to carry forward the call are expected in response to these requests. These requests are typically invoked at the end of an XML element’s execution. For example: when an IVR input is received from the caller during a GetInput XML execution.

    Callback URL Requests: No XML instructions are expected in response to these requests. Such requests serve as webhooks to notify your application server of important events through the course of an XML element’s execution. For example: when a conference participant is muted or unmuted.

    Set up a Flask Server to Serve XML & Manage Callbacks

    In this section, we’ll walk you through how to set up a Flask server in under five minutes to serve XML documents & manage callbacks.

    Use the following code snippet to start a local server.

    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__":'', debug=True)

    Save this code in any file (name the file something like To run this file on the server, go to the folder where this file resides and use the following command:


    And you should see your basic server app in action on http://localhost:5000/receive_call/

    Ngrok Setup

    To serve XML documents and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Use Ngrok to Expose Your Local Server to the Internet

    Run ngrok on the port which currently hosts your application. For example, if your port number is 5000, run the following command:

    ./ngrok http 5000

    This will start the ngrok server on your local server. Refer to the below screenshot.

    Sample ngrok CLI

    This will give you a UI with links that look like* which you can use to access your local server using the public network. And you should see your basic server app in action on http://<nrgok_URL>/receive_call/

    Sample ngrok CLI

    Note: You can follow the same approach to serve any other XML documents to manage various call flows. Please refer to the detailed XML reference to check all the XML elements available on the Voice API platform.