Get Started with Node.js

Sign up for a Plivo Account

When you sign up with Plivo, you will start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and purchase a number by clicking here. Add a number and credits to your account to start testing the full range of our voice and SMS service features.

Sign up here to get your free Plivo account today.

Follow these steps to successfully sign up for a free trial account:

  1. Sign up with your work email address
  2. Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
  3. Enter your mobile number to complete the phone verification step.

Sign up with your work email address

Sign up with email addres

If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.

Install Node.js and the Plivo Node.js Server SDK

You must set up and Node.js and install Plivo Node.js SDK to send your first SMS. Here’s how.

Install Node.js

Operating System Instructions
OS X & Linux You 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 .
Windows To install Node.js on Windows you can download it from here and install.

Install Plivo Node.js Package

Install the SDK using npm

$ npm install plivo

For features in beta, use the beta branch:

$ npm install plivo@beta

Send your first Outbound SMS

This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these prerequisites before we dive into the code.

  • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

credentials

  • Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

Buy New Number

Now, create a file called SendSMS.js and paste the below code.

Code

1
2
3
4
5
6
7
8
9
10
let plivo = require('plivo');
let client = new plivo.Client('auth_id','auth_token');

client.messages.create(
  'plivo_src_number',
  'your_destination_number',
  'Hello, world!'
).then(function(message_created) {
  console.log(message_created)
});
Note:
  • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console
  • Replace the placeholder plivo_source_number with the Phone number which you have purchased and the_destination_number with the phone number you will be sending SMS text messages to
  • Both plivo_source_number 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 SendSMS.js
Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

Set up a Express server for Incoming Messages & Callbacks

In this section, we’ll walk you through how to set up a Express server in under five minutes and start handling incoming messages & callbacks.

Plivo supports receiving SMS text messages in 9 countries (see complete SMS API coverage). When an SMS is sent to a Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.

Set up a Express server

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
18
19
20
21
22
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(function (req, response, next) {
    response.contentType('application/xml');
    next();
});
app.set('port', (process.env.PORT || 3000));
app.all('/receive_sms/', function (request, response) {
    let from_number = request.body.From || request.query.From;
    let to_number = request.body.To || request.query.To;
    let text = request.body.Text || request.query.Text;
    //Print the message
    console.log('Message received - From: ' + from_number + ', To: ' + to_number + ', Text: ' + text);
});
app.listen(app.get('port'), function () {
    console.log('Node app is running on port', app.get('port'));
});

Save this code in any file (let’s say the file name is receive_sms.js). To run this file on the server, go to the folder where this file resides and use the following command:

  node receive_sms.js

And you should see your basic server app in action on http://localhost:3000/receive_sms/

Exposing your local server to the internet

To receive Incoming Messages 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.

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

./ngrok http <port_on_which_your_local_server_is_running>

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.

ngrok

Create an Application

  1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
  2. Give your application a name. Let’s call it Receive SMS. Enter your server URL (e.g. http://example.com/receive_sms/) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
  3. Click on Create to save your application.

Create Application

Assign a Plivo number to your app

  1. Navigate to the Numbers page and select the phone number you want to use for this app.
  2. Select Receive SMS (name of the app) from the Plivo App dropdown list.
  3. Click on Update to save.

Assign Phone Number to Receive SMS App

Test and validate

Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the XML Request - Messages Documentation.

Reply to an incoming SMS

When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.

Note: In order to reply to a US or Canadian phone number, the `'src'` (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.

Create a file called reply_to_sms.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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const plivo = require('plivo');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(function (req, response, next) {
    response.contentType('application/xml');
    next();
});
app.set('port', (process.env.PORT || 3000));
app.all('/reply_sms/', function (request, response) {
    let from_number = request.body.From || request.query.From;
    let to_number = request.body.To || request.query.To;
    let text = request.body.Text || request.query.Text;
    console.log('Message received - From: ' + from_number + ', To: ' + to_number + ', Text: ' + text);

    //send the details to generate an XML
    let r = plivo.Response();
    let params = {
        'src': to_number,
        'dst': from_number,
    };
    let message_body = "Thank you, we have received your request";
    r.addMessage(message_body, params);
    console.log(r.toXML());
    response.end(r.toXML());
});
app.listen(app.get('port'), function () {
    console.log('Node app is running on port', app.get('port'));
});
Note: Create an application in the Plivo console with your Server URL in the message_URL field and assign the app to a Plivo number.

Test and validate

Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL with the parameters listed in the XML Request - Messages Documentation.