Top 5 NPM SMS Packages – Send SMS in Node.js

Integrating SMS functionalities into your Node.js application can extend its capabilities to send text messages directly to your users’ mobile phones. In this tutorial, we explore the top 5 SMS packages in Node.js that will help you implement robust SMS communications with ease.

NPM SMS Packages

1. Twilio

Twilio is a popular cloud communications platform that allows developers to send SMS in Node.js applications. With its robust API, you can easily integrate a variety of messaging services.

const twilio = require('twilio');
const client = new twilio('Your_Twilio_Account_SID', 'Your_Twilio_Auth_Token');

client.messages.create({
    to: 'Recipient_Number',
    from: 'Your_Twilio_Number',
    body: 'Hello from Twilio!'
}).then((message) => {
    console.log(message.sid);
});

Output:

SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2. Vonage (Previously Nexmo)

Vonage provides a powerful API for sending SMS messages across the globe. The Node.js SDK facilitates quick and secure message delivery.

const Vonage = require('@vonage/server-sdk');

const vonage = new Vonage({
    apiKey: "Your_Vonage_API_Key",
    apiSecret: "Your_Vonage_API_Secret"
});

const from = "Vonage_Number";
const to = "Recipient_Number";
const text = 'Hello from Vonage!';

vonage.message.sendSms(from, to, text, (err, responseData) => {
    if (err) {
        console.log(err);
    } else {
        if(responseData.messages[0]['status'] === "0") {
            console.log("Message sent successfully.");
        } else {
            console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`);
        }
    }
});

Output:

Message sent successfully.

3. Plivo

Plivo’s SMS API offers developers the flexibility to send SMS messages worldwide with minimal effort. Their Node.js library eases integration with their services.

const plivo = require('plivo');
const client = new plivo.Client('Your_Plivo_Auth_ID', 'Your_Plivo_Auth_Token');

client.messages.create(
    'Plivo_Number',
    'Recipient_Number',
    'Hello from Plivo!')
    .then(function(message_created) {
        console.log(message_created);
    });

Output:

Message UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

4. Sinch

Sinch provides a developer-friendly API for sending SMS messages. With a focus on engagement and interaction, Sinch is an excellent choice for Node.js developers.

const sinch = require('sinch-rest-api')(Your_Sinch_App_Key, Your_Sinch_App_Secret);

const message = {
    Number: 'Recipient_Number',
    Message: 'Hello from Sinch!'
};

sinch.sms({...message}).then(response => {
    console.log(response);
}).catch(error => {
    console.log(error);
});

Output:

{"MessageId":"XXXXXXXXXXXXXX","Recipient":{"Number":"Recipient_Number"}}

5. Bandwidth

Bandwidth offers a straightforward API for sending and receiving SMS in Node.js. Their platform is designed for scalability and simplicity.

const bandwidth = require('@bandwidth/messaging');
const client = new bandwidth.MessagingClient('Your_Bandwidth_API_Token');

const messageData = {
    to: 'Recipient_Number',
    from: 'Bandwidth_Number',
    text: 'Hello from Bandwidth!'
};

client.createMessage('Your_Bandwidth_Account_Id', messageData)
    .then(async (response) => {
        console.log(response);
    }).catch(e => console.log(e));

Output:

{"id":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","time":"2020-01-01T00:00:00Z"}

Conclusive Summary

In this article, we discussed the top 5 SMS packages to send SMS in Node.js. Each package offers unique features that cater to different developer needs for various project scopes. Remember to choose the package that aligns best with your application’s requirements and budget.

Integrating SMS functionality creates a direct communication line with your users, enhancing the overall experience. The examples provided should help you get started with incorporating these SMS services into your Node.js applications.

References and Further Reading: