How to Send SMS Notifications and Alerts Using JavaScript and Node.js by@plivo

How to Send SMS Notifications and Alerts Using JavaScript and Node.js

Plivo HackerNoon profile picture


Plivo — Enterprise-grade cloud communications stack for your business.

You say you have a great idea for an application and all you need is a toolkit to help you add texting capabilities to it?

Maybe you’re sending out delivery notifications, or using text messages for two-factor authentication, or you just want to use texting in your marketing campaigns.

You’re in luck, because you can add the ability to send and receive text messages easily with the help of a cloud communications platform.

A communications platform as a service (CPaaS) provides APIs that you can call from your programs.

On the back end, these APIs connect to the telecommunications network to exchange SMS messages and make and receive voice calls. By using a CPaaS, developers are insulated from the complexities of the telecom infrastructure; they can concentrate on making their applications better.

A Real-World Example

To see how this works, let’s walk through the exact process you might use as a developer.

CPaaS platforms provide software development kits (SDK) to make it easy to use their services in various languages — Python, PHP, Java, and more. We’ll use JavaScript and Node.js.

For our CPaaS, we picked Plivo, the service that tops the peer review site G2 CPaaS list for customer satisfaction. To use Plivo (or any CPaaS) you have to have an account. Plivo lets you sign up for free and provides usage credits so you can write test applications.

You also have to reserve a phone number from which to send messages, which you can do with a few clicks on the Plivo management console.

Since you’re reading this article I’m going to assume you’re already familiar with JavaScript and Node.js. Plivo provides a Node.js SDK, which is simple to install. Once you’ve done that, adding text notifications to your program is just a matter of a few lines of code.

var plivo = require('plivo');
(function main() {
	'use strict';
	var client = new plivo.Client("<auth_id>", "<auth_token>");
      	src: "<sender_id>",
      	dst: "<destination_number>",
      	text: "Appointment reminder: 12:00 noon tomorrow"
  	).then(function (response) {

It should be obvious what this code snippet does. The tokens in <angle brackets> are placeholders.

You have to replace the auth placeholders with authentication credentials that you can copy from the home page of the Plivo console. Replace the source and destination phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234). Boom — you’re done.

In a production environment, you would never hard-code authentication credentials into the code. You can store your credentials in environment variables instead.

If you set the variables `auth_id` and `auth_token`, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables.

You can use process.env to store environment variables and fetch them when initializing the client.

What About Receiving Texts?

Many organizations are concerned more with sending text messages than receiving them, but chances are some of the people you send messages to are going to text you back.

Fortunately, once you’ve got a CPaaS account, receiving texts is almost as simple as sending them. If you’re using Plivo, you can visit their “how to receive SMS messages” doc page for another code sample and instructions on how to tie the code to your web application server.

Before you roll your newly text-enabled application out to the masses, be sure to educate yourself about best practices for using SMS. You need to be aware of things like opt-in/opt-out rules and the kinds of message content that carriers prohibit.

If you’re in the US, you should also learn about 10DLC (10-digit long codes), a recent innovation by US carriers that promises high throughput and lowers the risk of spam messaging.


Signup or Login to Join the Discussion


Related Stories