Sending SMS on JavaScript (Node.js): sms-node-client API Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

sms-node-client Library: Sending SMS from Node.js



Modern web applications often require integration with SMS services for sending notifications, registration confirmations, or marketing campaigns. The sms-node-client library provides a simple and convenient interface for working with popular SMS gateways directly from Node.js. It abstracts the complexity of HTTP requests and authentication, allowing the developer to focus on business logic.



In this article, we will cover the main features of the library: installation, configuring the API connection, sending single and bulk messages, as well as handling common errors.



Installation and Setup



The library is distributed via npm. To install it, run the following command in the root of your project:



npm install sms-node-client


After installation, import the library into your JavaScript file:



const SMSClient = require('sms-node-client');

// or using ES6 modules (if configured)// import SMSClient from 'sms-node-client';


Client Configuration



To work with the API, you need to initialize a client object by passing it the connection parameters: apiKey (access key) and apiSecret (secret key). These are usually provided by your SMS provider.



const client = new SMSClient({  apiKey: 'your_api_key',  apiSecret: 'your_api_secret',  // Optional: you can specify a base URL if the provider uses a non-standard endpoint  // baseUrl: 'https://custom-sms-gateway.com/api/v1'});


Main Library Methods



The library provides three main methods for sending messages:



  • sendSMS(phone, message, options?) — sending a single SMS to one number.
  • sendBulkSMS(messages, options?) — bulk sending to multiple numbers.
  • getStatus(messageId) — checking delivery status by message ID.


Example: Sending a Single SMS



The simplest scenario is to send a message to one number. The method returns a Promise, so it is convenient to use async/await.



async function sendSingleSMS() {  try {    const result = await client.sendSMS(      '+71234567890',            // Recipient number in international format      'Hello! This is a test message from Node.js.', // Message text      { sender: 'MyApp' }       // Optional: sender name (must be registered with the provider)    );

console.log('SMS sent successfully:', result); // result.messageId - unique message ID for tracking status } catch (error) { console.error('Error sending SMS:', error.message); }}

sendSingleSMS();


Example: Bulk Sending



To send the same text to multiple numbers, use the sendBulkSMS method. It accepts an array of objects with phone numbers.



async function sendBulk() {  const recipients = [    { phone: '+71234567890' },    { phone: '+70987654321' },    { phone: '+70123456789' }  ];

try { const result = await client.sendBulkSMS(recipients, { message: 'Dear customer! Your order is ready for pickup.', sender: 'ShopBot' });

console.log(`Sent ${result.sentCount} messages`); // result.messages - array with details for each number } catch (error) { console.error('Bulk sending error:', error); }}

sendBulk();


Checking Delivery Status



After sending, you can check if the message reached the recipient. To do this, use the messageId obtained during sending.



async function checkStatus(messageId) {  try {    const status = await client.getStatus(messageId);    console.log('Message status:', status);    // status.s                    

Recommendations