Set up Your NodeJS 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.

    Prerequisites

    Install Node.js and the Plivo Node.js SDK

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

    Install Java

    Operating SystemInstructions
    OS X & LinuxYou would already have Node.js installed, you can check this by running the command node --version in the terminal. If you don't have it installed, you can install it from here .
    WindowsTo install Node.js on Windows you can download it from here and install.

    Install Plivo Node.js Package

    • Create a project directory, run the following command:

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

      $ cd mynodeapp
      
    • Install the SDK using npm

      $ npm install plivo
      

    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:

    • Now, create a file called Makecall.js and paste the below code.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    var plivo = require('plivo');
    
    (function main() {
        'use strict';
    
        var client = new plivo.Client("auth_id","auth_token");
        client.calls.create(
            "the_callerID", // from
            "the_destination_number", // to
            "http://s3.amazonaws.com/static.plivo.com/answer.xml", // answer url
            {
                answerMethod: "GET",
            },
        ).then(function (response) {
            console.log(response);
        }, function (err) {
            console.error(err);
        });
    })();
    
    • Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
    Note:
    • 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 process.env to store environment variables and fetch them while initializing the client.
    • Replace the placeholder the_callerID with the Phone number which you have purchased and the_destination_number with the phone number you will be making calls to.
    • Both the_callerID and the_destination_number should be in E.164 format, for example +15671234567.
    • Save the file and use the below command to run it.
    node Makecall.js
    
    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 Express Server to Serve XML & Manage Callbacks

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

    Use the following code snippet to start a local server.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    var plivo = require('plivo');
    var express = require('express');
    var app = express();
    
    app.all('/receive_call/', function(req, res) {
        var plivo = require('plivo');
        var response = plivo.Response();
        var speak_body = "Hello, you just received your first call";
        response.addSpeak(speak_body);
        res.writeHead(200, {'Content-Type': 'text/xml'});
        res.end(response.toXML());
    })
    
    app.set('port', (process.env.PORT || 5000));
    app.listen(app.get('port'), function() {
        console.log('Node app is running on port', app.get('port'));
    });
    

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

    node receive_call.js
    

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

    Ngrok Setup

    To sever 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.

    ngrok

    This will give you a UI with links that look like ngrok.io/* 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/

    ngrok

    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.