We have developed some examples to show you how to use our node.js helper and to help you get started quickly. These examples are available at http://github.com/plivo/plivo-examples-node.

Further information on the Plivo Developer API and related concpets is available at https://www.plivo.com/docs/. Helper libraries for other languages are available at https://www.plivo.com/docs/helpers/.

RestAPI takes one argument i.e. an object that contains two keys - authId and authToken, like so:

var plivo = require('plivo');

var api = plivo.RestAPI({

authId: '<your AUTH ID>',

authToken: '<your AUTH TOKEN>',

});

The RestAPI object exposes all the Plivo APIs and associated methods. Every method exposed by RestAPI object accepts two parameters:

params: an object containing a map of API params and their values.

callback: a callback that gets called after receiving response. Callbacks get two parameters:

status: HTTP Response Status Code. Example: 200, 201

response: a Javascript object because all our APIs send responses in JSON.

So for example, to make a call, you may do something like this:

/**

* api.make_call accepts params and callback

*/

// Keys and values to be used for params are the same as documented for our REST API.

// So for using RestAPI.make_call, valid params can be checked

// at https://www.plivo.com/docs/api/call/#outbound.

var params = {

from: '<your number>',

to: '<recipient's number>',

answer_url: 'http://your-server.com/answer_url',

};

api.make_call(params, function(status, response) {

if (status >= 200 && status < 300) {

console.log('Successfully made call request.');

console.log('Response:', response);

} else {

console.log('Oops! Something went wrong.');

console.log('Status:', status);

console.log('Response:', response);

}

});

Some RestAPI methods that implement Plivo REST API that do not have required parameters may ommit the use of params if not required. For example, to get Call Detail Records using our Call API, you may do something like this:

XML Generation can be used to generate XML that Plivo understands to synchronously control calls and messages. You may want to use it in with a web framework like Express, Geddy or whichever you prefer.

To use this feature, use the object returned by the plivo.Response function. Use it like so:

var plivo = require('plivo');

var response = plivo.Response();

// generates XML string.

console.log(response.toXML());

/*

OUTPUTS to screen:

<Response></Response>

*/

Response exposes the following methods:

toXML: generates the XML string response. This method does not require any parameter.

add<XML Element>: there are many methods that follow this kind of naming pattern. Replace <XML Element> with any valid XML element listed on https://www.plivo.com/docs/xml/. Some of these methods accept body param (a string) as an argument, some accept attributes (an object that is a map of valid attributes of the <XML Element>) as an argument and some accept both. Which method accepts which paramter depends upon the element.

addConference: accepts body and attributes as arguments

addNumber: accepts body and attributes as arguments

addUser: accepts body as argument

addDial: accepts attributes as argument

addGetDigits: accepts attributes as argument

addHangup: accepts attributes as argument

addMessage: accepts body and attributes as arguments

addPlay: accepts body and attributes as arguments

addPreAnswer: accepts no argument.

addRecord: accepts body and attributes as arguments

addRedirect: accepts body and attributes as arguments

addSpeak: accepts body and attributes as arguments

addWait: accepts attributes as argument

addDTMF: accepts body as argument

So, you may use the above functions like so:

/**

* Add a Speak Element.

*/

// add the Speak element

// Speak accepts both "body" and "attributes" as params.

// note that "loop" is a valid attribute for Speak element - https://www.plivo.com/docs/xml/speak/

response.addSpeak('Hello world!', { loop: 2 });

// add the Wait element

// Wait accepts only "attributes" as a param - https://www.plivo.com/docs/xml/wait/

response.addWait({ length: 3 });

// add the DTMF element

// DTMF accepts only "body" as a param - https://www.plivo.com/docs/xml/dtmf/