Developer Quick-Start: A comprehensive guide to building a Node.js Express application for sending bulk SMS messages using the Infobip API and Node.js SDK, covering setup, implementation, error handling, and best practices. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
This guide provides a comprehensive walkthrough for building a production-ready Node.js application using the Express framework to send bulk/broadcast SMS messages via the Infobip API and its official Node.js SDK. We'll cover everything from project setup and core implementation to error handling, security, and deployment.
Project Overview and Goals
What We're Building:
We will create a Node.js Express application featuring an API endpoint capable of receiving a list of phone numbers and a message text. This application will then leverage the Infobip API via its Node.js SDK to efficiently send that message as an SMS to all specified recipients.
Problem Solved:
This application addresses the need to programmatically send the same SMS message to multiple recipients simultaneously (broadcast/bulk messaging). This is common for marketing campaigns, notifications, alerts, or announcements where reaching a large audience quickly via SMS is required. Manually sending these messages or using basic single-send API calls is inefficient and error-prone at scale.
Technologies Used:
- Node.js: A JavaScript runtime environment enabling server-side development. Chosen for its asynchronous nature, large ecosystem (npm), and suitability for I/O-bound tasks like API interactions.
- Express.js: A minimal and flexible Node.js web application framework. Provides robust features for web and mobile applications, simplifying routing, middleware, and request/response handling.
- Infobip API and Node.js SDK (
@infobip-api/sdk): Infobip provides communication APIs, including SMS. We'll use their official Node.js SDK to simplify interactions, handle authentication, and structure API requests correctly. Using the SDK is preferred over manual HTTP requests for maintainability and leveraging built-in features. - dotenv: A zero-dependency module that loads environment variables from a .env file into process.env. Essential for managing sensitive information like API keys securely.
- (Optional but Recommended) SQLite3: A simple file-based relational database. We'll use it to demonstrate storing recipient lists persistently, though you can adapt the principles to other databases like PostgreSQL or MySQL.
- (Recommended) Pino: A fast, low-overhead JSON logger.
- (Recommended) Express Validator: Middleware for request data validation.
- (Recommended) Helmet: Middleware for setting security-related HTTP headers.
- (Recommended) Express Rate Limit: Middleware for basic rate limiting.
System Architecture:
+-----------------+ +---------------------+ +-----------------+ +-----------------+
| Client (e.g., |----->| Node.js/Express App |----->| Infobip Node.js |----->| Infobip API |
| Postman, Frontend)| | (API Endpoint) | | SDK | | (SMS Gateway) |
+-----------------+ +----------+----------+ +-----------------+ +--------+--------+
| ^
| (Optional) |
v |
+----------+----------+ | (SMS Sent)
| Database (SQLite) |<-----+
| (Recipient List) |
+---------------------+
- A client (like Postman or a frontend application) sends a POST request to our Express API endpoint (/api/broadcast).
- The Express app receives the request, validates the payload (recipients, message).
- (Optional) The app retrieves recipient phone numbers from a database.
- The app uses the Infobip Node.js SDK to format the bulk SMS request.
- The SDK sends the request (including authentication) to the Infobip API.
- Infobip processes the request and sends the SMS messages to the recipients' handsets.
- Infobip returns a response (including bulkId and individual message statuses) to the SDK, which relays it back to the Express app.
- The Express app sends a success or error response back to the client.
Prerequisites:
- Infobip Account: You need an active Infobip account. Sign up at infobip.com. Note any limitations of free trial accounts (e.g., sending only to your registered phone number).
- Node.js and npm (or yarn): A recent LTS version of Node.js installed. Verify with node -v and npm -v. (Download Node.js).
- Basic JavaScript/Node.js Knowledge: Familiarity with JavaScript syntax, asynchronous programming (async/await), and basic Node.js concepts.
- Basic Command Line/Terminal Usage: Ability to navigate directories and run commands.
- (Optional) API Testing Tool: Like Postman or curl for testing the API endpoint.
Expected Outcome:
By the end of this guide, you will have a functional Node.js Express application with a single API endpoint
(POST /api/broadcast) that accepts a list of phone numbers and a message, sends the SMS to all
recipients via Infobip, and returns the results from the Infobip API. You will also understand how to manage
configuration, handle basic errors, and implement essential security practices.
1. Setting up the Project
Let's initialize our Node.js project and install the necessary dependencies.
- Create Project Directory: Open your terminal or command prompt and create a new directory for the project, then navigate into it.
mkdir nodejs-infobip-bulk-sms
cd nodejs-infobip-bulk-sms
- Initialize Node.js Project: This creates a
package.jsonfile to manage project details and dependencies.
npm init -y
(The -y flag accepts the default settings)
- Install Dependencies: We need Express for the web server, the Infobip SDK, dotenv for environment variables, and optionally sqlite3 for the database.
npm install express @infobip-api/sdk dotenv sqlite3 pino pino-pretty express-validator helmet express-rate-limit
- express: The web framework.
- @infobip-api/sdk: The official Infobip SDK for Node.js.
- dotenv: Loads environment variables from .env.
- sqlite3: Driver for SQLite database (remove if not using the database part).
- pino, pino-pretty: Logger and development formatter.
- express-validator: Input validation middleware.
- helmet: Security headers middleware.
- express-rate-limit: Rate limiting middleware.
- Install Development Dependencies (Optional but Recommended):
nodemonautomatically restarts the server during development when file changes are detected.
npm install --save-dev nodemon
- Configure
package.jsonScripts: Openpackage.jsonand modify thescriptssection to add convenient commands for starting the server.
{
""scripts"": {
""start"": ""node index.js"",
""dev"": ""nodemon index.js"",
""test"": ""echo \""Error: no test specified\"" && exit 1""
},
}
- Create Project Structure: Let's organize our code logically. Create the following files and folders:
nodejs-infobip-bulk-sms/
βββ .env # Environment variables (API Keys, etc.) - DO NOT COMMIT
βββ .gitignore # Specifies intentionally untracked files that Git should ignore
βββ .dockerignore # Specifies files to ignore during Docker build
βββ Dockerfile # For containerizing the application
βββ index.js # Main application entry point
βββ package.json
βββ package-lock.json
βββ node_modules/ # Created by npm install (usually ignored by git)
βββ src/ # Source code directory
βββ config/ # Configuration files
β βββ infobip.js
β βββ logger.js
βββ services/ # Business logic (sending SMS, database interaction)
β βββ infobipService.js
β βββ recipientService.js # (Optional DB service)
βββ routes/ # API route definitions
β βββ api.js
βββ controllers/ # Request handlers for routes
β βββ messagingController.js
βββ middleware/ # Custom middleware (e.g., validation handlers)
β βββ validation.js
βββ database/ # Database related files (optional)
βββ database.js
βββ schema.sql
- Create
.gitignore: Prevent sensitive files and unnecessary folders from being committed to version control. Create a.gitignorefile in the project root:
# .gitignore
node_modules/
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.sqlite # Ignore database files if using SQLite
- Create
.envFile: This file will hold our sensitive configuration. Create.envin the project root:
.env
# .env - DO NOT COMMIT THIS FILE
# Infobip Credentials
INFOBIP_API_KEY=<YOUR_INFOBIP_API_KEY>
INFOBIP_BASE_URL=<YOUR_INFOBIP_BASE_URL> # e.g., xyz123.api.infobip.com
# Application Settings
PORT=3000
NODE_ENV=development # Set to 'production' in deployment
LOG_LEVEL=info
# Database (Optional - for SQLite)
DATABASE_PATH=./mydatabase.sqlite
- IMPORTANT: Replace placeholders <YOUR\_INFOBIP\_API\_KEY> and <YOUR\_INFOBIP\_BASE\_URL> with your actual Infobip credentials (see next section). Never commit .env files to Git.
- Basic
index.jsSetup: Set up the main Express application file.
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const logger = require('./src/config/logger');
const apiRoutes = require('./src/routes/api');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(helmet());
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: 'Too many requests from this IP, please try again after 15 minutes',
standardHeaders: true,
legacyHeaders: false,
handler: (req, res, ) => {
logger.warn(`Rate limit exceeded for IP: ${req.ip}`);
res.status(429).json({ error: 'Too many requests, please try again later.' });
}
});
app.use('/api', apiLimiter);
app.use(express.json());
app.use((req, res, next) => {
logger.info({ method: req.method, url: req.url, ip: req.ip }, 'Incoming request');
next();
});
app.get('/', (req, res) => {
res.send('Infobip Bulk SMS Service is running!');
});
app.use('/api', apiRoutes);
app.use((err, req, res, next) => {
logger.error({ err: err, stack: err.stack }, 'Unhandled Error');
const statusCode = err.statusCode || 500;
let message = 'Something went wrong!';
if (statusCode !== 500 || process.env.NODE_ENV !== 'production') {
message = err.message || message;
} else {
message = 'An internal server error occurred.';
}
res.status(statusCode).json({ error: message });
});
app.listen(PORT, () => {
logger.info(`Server running on port ${PORT} in ${process.env.NODE_ENV} mode`);
if (!process.env.INFOBIP_API_KEY || !process.env.INFOBIP_BASE_URL) {
logger.warn('WARNING: Infobip API Key or Base URL not found in environment variables. SMS sending will fail.');
}
});
Now we have a basic Express project structure ready for implementing the core functionality.
2. Obtaining Infobip API Credentials
To interact with the Infobip API, you need an API Key and your account-specific Base URL.
- Log in to Infobip: Access your Infobip Portal account (https://portal.infobip.com/).
- Navigate to API Keys: Go to the homepage dashboard or navigate through the menu (the exact location might change slightly, but look for sections like "Developers," "API," or "Settings"). Find the "API Keys management" section.
- Create or View API Key: You might have a default key, or you can create a new one. Copy the generated API Key. This is a secret value β treat it like a password.
- Find Your Base URL: On the same API Keys page or in the main developer/API section, find
your unique Base URL. It typically looks like
[unique_identifier].api.infobip.com. Copy this URL. - Update
.env: Paste the copied API Key and Base URL into your.envfile created in the previous step:
.env
# .env
INFOBIP_API_KEY=YourCopiedApiKeyString........
INFOBIP_BASE_URL=yourUniqueId.api.infobip.com
PORT=3000
NODE_ENV=development
LOG_LEVEL=info
DATABASE_PATH=./mydatabase.sqlite
- Security: Ensure the .env file is listed in your .gitignore and is never committed to your repository. Use environment variables in production deployment environments instead of committing the .env file.
3. Implementing Core Functionality (Bulk SMS Sending)
Now, let's implement the service responsible for interacting with the Infobip SDK and the logger.
- Configure Logger: Set up the Pino logger.
const pino = require('pino');
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: process.env.NODE_ENV !== 'production' ? {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
}
} : undefined,
});
module.exports = logger;
- Configure Infobip Client: Create the configuration file to initialize the SDK client instance.
const { Infobip, AuthType } = require('@infobip-api/sdk');
const logger = require('./logger');
if (!process.env.INFOBIP_BASE_URL || !process.env.INFOBIP_API_KEY) {
logger.error(""FATAL ERROR: Infobip Base URL or API Key is missing in environment variables."");
throw new Error(""Missing Infobip credentials in environment."");
}
let infobipClient;
try {
infobipClient = new Infobip({
baseUrl: process.env.INFOBIP_BASE_URL,
apiKey: process.env.INFOBIP_API_KEY,
authType: AuthType.ApiKey,
});
logger.info('Infobip client configured successfully.');
} catch (error) {
logger.error({ err: error }, ""Failed to initialize Infobip client."");
throw error;
}
module.exports = infobipClient;
- Why
AuthType.ApiKey? Infobip supports different authentication methods. We explicitly tell the SDK to use the API Key provided. - Why check environment variables here? It ensures that if the client is imported, the necessary configuration is expected to be present, failing early if not.
- Create Infobip Service: This service will contain the logic to format the request and call the Infobip SDK method for sending SMS.
const infobipClient = require('../config/infobip');
const logger = require('../config/logger');
async function sendBulkSms(phoneNumbers, messageText, sender = 'InfoSMS') {
if (!phoneNumbers || !Array.isArray(phoneNumbers) || phoneNumbers.length === 0) {
logger.warn('Attempted to send SMS with empty or invalid recipient list.');
throw new Error('Recipient phone numbers array cannot be empty.');
}
if (!messageText || typeof messageText !== 'string' || messageText.trim() === '') {
logger.warn('Attempted to send SMS with empty message text.');
throw new Error('Message text cannot be empty.');
}
const invalidNumbers = [];
const validDestinations = phoneNumbers
.map(num => String(num).replace(/\D/g, ''))
.filter(cleanedNum => {
const isValid = /^\d{10,15}$/.test(cleanedNum);
if (!isValid) {
invalidNumbers.push(String(num));
}
return isValid;
})
.map(validNum => ({ to: validNum }));
if (invalidNumbers.length > 0) {
logger.warn({ invalidNumbers }, `Found ${invalidNumbers.length} potentially invalid phone number formats.`);
if (validDestinations.length === 0) {
throw new Error('No valid phone number destinations after validation.');
}
}
const destinations = validDestinations;
try {
logger.info(`Attempting to send SMS via Infobip to ${destinations.length} valid recipients. Sender: ${sender}`);
const response = await infobipClient.channels.sms.send({
type: 'text',
messages: [
{
from: sender,
destinations: destinations,
text: messageText.trim(),
},
],
});
logger.info({ bulkId: response.data?.bulkId, messageCount: response.data?.messages?.length }, 'Infobip API Response Received');
return response.data;
} catch (error) {
const errorDetails = error.response ? error.response.data : error.message;
logger.error({ error: errorDetails, status: error.response?.status }, 'Error sending SMS via Infobip');
const errorMessage = error.response?.data?.requestError?.serviceException?.text || `Infobip API Error (Status: ${error.response?.status || 'N/A'})`;
const serviceError = new Error(errorMessage);
if (error.response?.status) {
serviceError.statusCode = error.response.status;
}
throw serviceError;
}
}
module.exports = {
sendBulkSms,
};
- Why
infobipClient.channels.sms.send? This is the specific SDK method for sending SMS messages. - Why the
messagesarray? The API is designed to handle multiple message definitions in one call. For a simple broadcast, we use one message object containing multiple destinations. - Why
destinationsarray? Each object represents one recipient (to field). - Why
response.data? The SDK wraps the raw HTTP response. The actual data returned by Infobip (bulkId, messages status array) is within the .data property. - Improved Validation: Basic validation now cleans numbers and logs warnings for potentially invalid ones, proceeding only with valid formats. It throws an error if no valid numbers remain.
- Error Handling: We wrap the call in try...catch, log detailed errors using Pino, and throw a new, potentially cleaner error message upwards, attaching the status code if possible.
4. Building the API Layer (Express Route)
Let's create the Express route and controller that will use our infobipService.
- Create Validation Middleware Helper: This simplifies handling validation results in routes.
const { validationResult } = require('express-validator');
const logger = require('../config/logger');
const handleValidationErrors = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
logger.warn({ errors: errors.array({ onlyFirstError: true }), url: req.originalUrl }, 'Validation errors occurred');
return res.status(400).json({ errors: errors.array({ onlyFirstError: true }) });
}
next();
};
module.exports = { handleValidationErrors };
- Create Messaging Controller: This file handles the logic for incoming requests to messaging endpoints.
const { sendBulkSms } = require('../services/infobipService');
const logger = require('../config/logger');
async function handleBroadcast(req, res, next) {
const { recipients, message, sender } = req.body;
try {
let targetRecipients = recipients;
if (!targetRecipients) {
if (!targetRecipients) {
const err = new Error('Bad Request: Recipient list is required.');
err.statusCode = 400;
throw err;
}
}
logger.info(`Initiating broadcast to ${targetRecipients.length} recipients.`);
const result = await sendBulkSms(targetRecipients, message, sender);
res.status(200).json({
message: `SMS broadcast initiated successfully to ${result.messages?.length || 0} valid recipients.`,
infobipResponse: result,
});
} catch (error) {
next(error);
}
}
module.exports = {
handleBroadcast,
};
- Relies on Validation Middleware: Assumes express-validator has already checked the basic structure and types of recipients and message.
- Database Logic Structure: The database fetching logic is clearly separated and commented out, making it easier to enable/disable. The default path assumes recipients come from the request.
- Error Handling: All errors (validation, service, DB) are consistently passed to the central error handler using next(error).
- Success Message: Uses the count of messages returned by Infobip for a more accurate success message.
- Create API Routes with Validation: Define the endpoints, link them to controllers, and apply validation rules.
const express = require('express');
const { body } = require('express-validator');
const { handleBroadcast } = require('../controllers/messagingController');
const { handleValidationErrors } = require('../middleware/validation');
const router = express.Router();
const validateBroadcast = [
body('message')
.trim()
.notEmpty().withMessage('Message cannot be empty.')
.isString().withMessage('Message must be a string.')
.isLength({ max: 1600 }).withMessage('Message length exceeds typical limits.'),
body('recipients')
.notEmpty().withMessage('Recipients array is required.')
.isArray({ min: 1 }).withMessage('Recipients must be an array with at least one number.')
.custom((recipients) => {
if (!Array.isArray(recipients)) return false;
if (!recipients.every(num => typeof num === 'string' && /^\+?\d{10,15}$/.test(String(num).trim().replace(/[\s()-]/g,'')))) {
throw new Error('One or more recipient phone numbers have an invalid format (expected international numbers like +15551234567).');
}
const uniqueRecipients = new Set(recipients.map(num => String(num).trim().replace(/[\s()-]/g,'')));
if (uniqueRecipients.size !== recipients.length) {
throw new Error('Duplicate phone numbers found in the recipient list.');
}
return true;
}),
body('sender')
.optional()
.trim()
.isLength({ min: 1, max: 11 }).withMessage('Sender ID must be between 1 and 11 characters.')
.matches(/^[a-zA-Z0-9]+$/).withMessage('Sender ID should typically be alphanumeric (check Infobip rules).')
];
router.post(
'/broadcast',
validateBroadcast,
handleValidationErrors,
handleBroadcast
);
module.exports = router;
- Improved Validation: Added max length for message, stricter phone number format check (allows optional +, removes common separators before checking digits), and optional duplicate check.
- Mount Routes in
index.js: Ensure the main application file uses these routes (already done in the setup step).
const apiRoutes = require('./src/routes/api');
app.use('/api', apiRoutes);
- Test the Endpoint: Start the server:
npm run dev
Use curl or Postman to send a POST request: Using curl:
curl -X POST http://localhost:3000/api/broadcast \
-H "Content-Type: application/json" \
-d '{
"recipients": ["+15551234567", "+15559876543"],
"message": "Hello from the Node.js Bulk Sender! (Test 1)"
}'
(Replace the example +1555... numbers with your actual, internationally formatted test phone
numbers, e.g., +447123456789. Remember potential trial account restrictions on recipient
numbers.) Expected JSON Response (Success):
{
"message": "SMS broadcast initiated successfully to 2 valid recipients.",
"infobipResponse": {
"bulkId": "some-unique-bulk-id-generated-by-infobip",
"messages": [
{
"to": "15551234567",
"status": { },
"messageId": "some-unique-message-id-1"
},
{
"to": "15559876543",
"status": { },
"messageId": "some-unique-message-id-2"
}
]
}
}
Expected JSON Response (Error - e.g., bad input):
{
"errors": [
{
"type": "field",
"msg": "Message cannot be empty.",
"path": "message",
"location": "body"
}
]
}
Expected JSON Response (Error - e.g., Infobip API failure): (Handled by the central error handler)
{
"error": "Infobip API Error: Invalid login details"
}
You now have a working API endpoint capable of sending bulk SMS via Infobip!
5. (Optional) Creating a Database Schema and Data Layer (SQLite Example)
Storing recipients in a database allows for managing larger lists and potentially tracking send statuses or user preferences.
(Ensure sqlite3 is installed: npm install sqlite3)
- Define Database Schema: Create a file to define the table structure.
CREATE TABLE IF NOT EXISTS recipients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone_number TEXT NOT NULL UNIQUE,
first_name TEXT,
last_name TEXT,
subscribed BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_phone_number ON recipients (phone_number);
CREATE INDEX IF NOT EXISTS idx_subscribed ON recipients (subscribed);
CREATE TRIGGER IF NOT EXISTS update_recipients_updated_at
AFTER UPDATE ON recipients FOR EACH ROW
BEGIN
UPDATE recipients SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id;
END;
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β