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.
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)
});
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)
});
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)
});
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 Java, Python, PHP, 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/