How to Send SMS in Node.js and Express.js Using Plivo's SMS API

Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive messages through Plivo in a Node.js application.

Install the Plivo SDK and Express.js Framework

We’ll presume you already have your Node.js environment set up. Change to the directory into which you want to install the Plivo Node.js SDK and run

$ npm i -S plivo express body-parser

Find your Auth ID and Auth Token

You have to have proper credentials before you can use the Plivo API. We provide an Auth ID and Auth Token in the Account section at the top of your Plivo Console.

Find Your Auth Credentials on Plivo Console

Choose a phone number

You need an SMS-enabled Plivo phone number to send messages to the US and Canada. Check the Numbers screen of your Plivo console to see what numbers you have available and which of them support SMS capabilities. You can also buy numbers from this screen.

Buy a New Plivo Number

SMS regulations followed by carriers vary from country to country. For messages to countries other than the US and Canada, you might want to register an alphanumeric sender ID for your messages. You can learn more about the use of alphanumeric sender ID and register one from your Plivo Console.

Send an SMS message

Now you’re ready to start. Create a file called SendSMS.js and paste in this 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({
    src: '+14151234567',
    dst: 'your_destination_number',
    text: 'Hello, world!'
}).then(function(message_created) {
    console.log(message_created)
});

Replace the placeholders auth_id and auth_token with actual values from your Plivo Console. Save the file and run it with the command

$ node SendSMS.js

Note: If you’re using a Plivo trial account, you can send messages only to phone numbers that have been verified with Plivo. You can verify a phone number using the Sandbox Numbers page of the Console.

Receive an SMS message

Of course sending messages is only half of the equation. Plivo supports receiving SMS text messages in many countries (see our SMS API coverage page and click on the countries you’re interested in). When someone sends an SMS message to a Plivo phone number, you can receive it 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.

Use this code 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 — we’ll call ours receive_sms.js. To run this file on the server, go to the folder where this file resides and issue the command

$ node receive_sms.js

You should see your basic server app in action on http://localhost:3000/receive_sms/.

That’s fine for testing, but it’s not much good if you can’t connect to the internet to receive incoming messages and handle callbacks. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages:

$ ./ngrok http [portnum]

Ngrok will display a forwarding link that you can use as a webhook to access your local server using the public network.

Sample ngrok CLI

Now you can create an application to receive SMS messages (follow our Quickstart guide for details).

Conclusion

And that’s all there is to sending and receiving SMS messages using Plivo’s Node.js SDK. Don’t use Node.js? Don’t worry — we have SDKs for Java, Python, PHP, Ruby, .NET Core, .NET Framework, and Go.

Haven’t tried Plivo yet? Getting started is easy and only takes 5 minutes! Sign up today.

comments powered by Disqus