paint-brush
Using Plivo APIs For Sending SMS messages With Node.jsby@plivo
538 reads
538 reads

Using Plivo APIs For Sending SMS messages With Node.js

by PlivoApril 4th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article guides you through how to use Plivo SMS API to send SMS from your application with Node.js. We'll send an SMS message with GSM characters, Unicode characters, and emojis, and a multipart message in the text body. Getting started is easy and only takes five minutes! Sign up today to send your first SMS message using Plivo’s Node.JS SDK. You must have an SMS-enabled Plivo phone number to send messages to the US and Canada.

Company Mentioned

Mention Thumbnail
featured image - Using Plivo APIs For Sending SMS messages With Node.js
Plivo HackerNoon profile picture

You can send your first SMS message using Plivo Node.js SDK within 5 minutes. This article guides you through how to use Plivo SMS API to send SMS from your application with Node.js. First, let’s make sure you meet the prerequisites before we dive into the code.

Prerequisites

Sign-up for a Plivo account if you haven't already!

Plivo Auth ID and Auth Token: You will find your Plivo Auth ID and Auth Token on the home screen of your Plivo console.

Plivo Phone Number: You must have an SMS-enabled Plivo phone number to send messages to the US and Canada. Purchase numbers from the Numbers section of your Plivo console. You can also purchase numbers using the Numbers API.

Install Node.js if you haven't already!

Install Plivo Node.js SDK:

npm install -g plivo

Send an SMS message

In this section, we'll send an SMS message with GSM characters, Unicode characters, and emojis, and a multipart message in the text body.

Send an SMS message with GSM characters

Now you’re ready to start. Create a file called sendSms.js and paste in this code:

const plivo = require('plivo');
const client = new plivo.Client('<auth_id>','<auth_token>');

client.messages.create(
  '+14156667777',
  '+14156667778',
  'Hello, world!'
).then(function(message_created) {
  console.log(message_created)
});

Send an SMS message with emojis

You can use this code to send an SMS with emojis in the message body:

const plivo = require('plivo');
const client = new plivo.Client('<auth_id>','<auth_token>');

client.messages.create(
  '+14156667777',
  '+14156667778',
  '👋 Hello from Plivo!'
).then(function(message_created) {
  console.log(message_created)
});

Send an SMS message with Unicode characters

You can use this code to send an SMS with Unicode characters in the message body:

const plivo = require('plivo');
const client = new plivo.Client('<auth_id>','<auth_token>');

client.messages.create(
  '+14156667777',
  '+14156667778',
  '你好,世界!'
).then(function(message_created) {
  console.log(message_created)
});

Send a multipart SMS message

You can use this code to send an SMS message with a multipart message body. The message will be sent to the destination number in parts and will be concatenated at the recipient's end.

const plivo = require('plivo');
const client = new plivo.Client('<auth_id>','<auth_token>');

client.messages.create(
  '+14156667777',
  '+14156667778',
  'This randomly generated text can be used in your layout (webdesign , websites, books, posters ... ) for free. This text is entirely free of law. Feel free to link to this site by using the image below or by making a simple text link'
).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

An SMS message will be sent to the destination number specified in the code. To receive incoming SMS messages on your Plivo number, follow our Quickstart guide.

Note: You can check our Encoding and Concatenation guide to learn how Plivo manages GSM/Unicode characters in the message body.

Conclusion

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

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

Previously published at https://www.plivo.com/blog/send-sms-in-node-express/