Sending SMS using NEXMO API in Node.js
Last Updated :
14 Feb, 2020
Introduction: SMS is a common method of sending short messages between cell phones, but these SMS can be sent using API in Node.js. Now there are lots of API in the market for sending SMS like Twilio, Exotel, etc to the user but the popular among them is Nexmo.
Features of NEXMO: Integrating this module in your code is very simple and efficient. Using Nexmo module users can send SMS and also use Nexmo voice APIs for sending voice calls.
Introduction:
- It’s easy to get started and easy to use.
- It is widely used and popular module for sending SMS.
- User can send SMS to desired mobile number fastly and efficiently.
Installation of Nexmo module:
- You can visit the link to Install nexmo module. You can install this package by using the following command.
npm install nexmo
- After installing nexmo you can check your nexmo version in command prompt using the command.
npm version nexmo
- After that, you can create a folder and add a file. For example index.js. To run this file you need to run the following command.
node index.js
- Requiring module: You need to include nexmo module in your file by using these lines.
const Nexmo = require('nexmo');
Filename: index.js
const Nexmo = require( 'nexmo' );
const nexmo = new Nexmo({
apiKey: 'YOUR_API_KEY' ,
apiSecret: 'YOUR_API_SECRET_KEY' ,
});
const from = 'sender_name' ;
const to = 'reciever_number' ;
const text = 'Greetings from Geeksforgeeks' ;
nexmo.message.sendSms(from, to, text,
function (error, result) {
if (error) {
console.log( "ERROR" , error)
}
else {
console.log( "RESULT" , result)
}
});
|
Steps to run the program:
- The project structure will look like this:
- Make sure you have installed nexmo using following commands:
npm install nexmo
- Run index.js file using below command:
node index.js
- If error occurs, then following message will be displayed:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...