Related Articles

Related Articles

Sending SMS using NEXMO API in Node.js
  • Difficulty Level : Expert
  • 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:

  1. It’s easy to get started and easy to use.
  2. It is widely used and popular module for sending SMS.
  3. User can send SMS to desired mobile number fastly and efficiently.

Installation of Nexmo module:

  1. You can visit the link to Install nexmo module. You can install this package by using the following command.
    npm install nexmo
  2. After installing nexmo you can check your nexmo version in command prompt using the command.
    npm version nexmo
  3. 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
  4. Requiring module: You need to include nexmo module in your file by using these lines.
    const Nexmo = require('nexmo');
  5. Filename: index.js

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Include nexmo module
    const Nexmo = require('nexmo');
       
    const nexmo = new Nexmo({
        apiKey: 'YOUR_API_KEY',
        apiSecret: 'YOUR_API_SECRET_KEY',
    });
      
    // Initialize with sender and reciever
    // mobile number with text message
    const from = 'sender_name';
    const to = 'reciever_number';
    const text = 'Greetings from Geeksforgeeks';
      
    nexmo.message.sendSms(from, to, text,
                    function(error, result) {   
          
        // If some error occured
        if(error) {
            console.log("ERROR", error)
        }
          
        // If message is sent successfully
        else {
            console.log("RESULT", result)
        }
    });

    chevron_right

    
    

    Steps to run the program:

    1. The project structure will look like this:
      project structure
    2. Make sure you have installed nexmo using following commands:
      npm install nexmo
    3. Run index.js file using below command:
      node index.js

      Output of above command

    4. If error occurs, then following message will be displayed:
      Error Message
    My Personal Notes arrow_drop_up
Recommended Articles
Page :