Bulk SMS

Build Scalable Bulk SMS Broadcasting with Fastify, Sinch API, and BullMQ

Complete guide to building a production-ready bulk SMS system using Node.js, Fastify v5, Sinch API, BullMQ v5.x queue, Prisma ORM, and PostgreSQL. Includes error handling, retries, rate limiting, database tracking, and scalability patterns for sending thousands of SMS messages reliably.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 2, 2026 β€’ 20 min read

Developer Quick-Start: Complete guide to building a production-ready bulk SMS system using Node.js, Fastify v5, Sinch API, BullMQ v5.x queue, Prisma ORM, and PostgreSQL. Includes error handling, retries, rate limiting, database tracking, and scalability patterns for sending thousands of SMS messages reliably. 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.

Sending bulk SMS messages – whether for marketing campaigns, notifications, or alerts – requires more than just a simple loop calling an API. A production-ready system needs to handle potential failures, manage rate limits, provide status tracking, and scale reliably.

This comprehensive tutorial shows you how to send bulk SMS using Node.js with the high-performance Fastify framework, leveraging the Sinch SMS API for message delivery and Redis with BullMQ for robust background job queuing. Learn how to build a scalable SMS broadcasting system that handles thousands of messages with automatic retries, error handling, and real-time status tracking.

What you'll build:

  • A Fastify API endpoint to accept bulk SMS requests (list of recipients and a message body).
  • A background job queue (using BullMQ and Redis) to process each SMS message individually, ensuring reliability and resilience.
  • A worker process that picks up jobs from the queue and sends SMS messages via the Sinch API.
  • Database persistence (using Prisma and PostgreSQL) to track the status of each message and the overall batch.
  • An API endpoint to check the status of a submitted bulk messaging batch.
  • Robust error handling, retries, logging, and basic security measures.

Core Technologies:

  • Node.js: The JavaScript runtime environment. Required: Node.js v20 LTS "Iron" (Maintenance LTS through April 2026) or v22 LTS "Jod" (Active LTS through October 2025, Maintenance LTS through April 2027). Node.js v18 reached End of Life on April 30, 2025 and no longer receives security updates.
  • Fastify: A high-performance, low-overhead web framework for Node.js. Fastify v5 (requires Node.js v20+) offers 5 – 10% performance improvements over v4 and includes security updates including fixes for CVE-2025-32442. Chosen for its speed, extensibility, and focus on developer experience. Fastify v4 is supported until June 30, 2025.
  • Sinch SMS API: The third-party service used to send SMS messages. Sinch provides direct connections to 600+ mobile operators worldwide with 95% US population coverage. You'll interact with its REST API. API regions include US (us.sms.api.sinch.com) and EU, chosen based on data protection requirements.
  • Redis: An in-memory data structure store, used here as the backend for your message queue.
  • BullMQ: A robust, fast, and reliable Redis-based queue system for Node.js. BullMQ v5.x (latest v5.60.0 as of 2025) requires explicit connection objects and no longer supports integer job IDs. Essential for handling background jobs like sending individual SMS messages.
  • Prisma: A modern database toolkit for Node.js and TypeScript, simplifying database access, migrations, and type safety. Prisma ORM v6.x (latest v6.16.0) includes Rust-free preview for PostgreSQL and requires PostgreSQL v9.6 minimum (v12+ recommended). Used here with PostgreSQL.
  • PostgreSQL: A powerful, open-source object-relational database system.
  • Axios: A promise-based HTTP client for making requests to the Sinch API.

System Architecture:

+-----------+      +-----------------+      +--------------+      +----------------+      +-----------+
|  Client   |----->|  Fastify API    |----->|  Redis Queue |----->|  Node.js Worker|----->| Sinch API |
| (Browser/ |      | (Receives Bulk  |      |   (BullMQ)   |      | (Processes Job)|      | (Sends SMS)|
|   App)    |      | Request, Queues |      +--------------+      +----------------+      +-----------+
+-----------+      |   Individual    |             |                       |
                   |     Jobs)       |             |                       |
                   +-----------------+             |                       |
                         |   ^                     |                       |
                         |   | Check Status        | Update Status         |
                         v   |                     v                       v
                   +-----------------------------------------------------------+
                   |                  PostgreSQL Database (Prisma)             |
                   | (Stores Batch Info, Individual Message Status, Errors)    |
                   +-----------------------------------------------------------+

Prerequisites:

  • Node.js v20 LTS or v22 LTS and npm/yarn. Download Node.js. Important: Node.js v18 reached End of Life on April 30, 2025 and no longer receives security updates.
  • Access to a terminal or command prompt.
  • A Sinch account with SMS API credentials (Service Plan ID, API Token) and a configured Sinch phone number in E.164 format (e.g., +12025550100).
  • Docker installed and running (for easily running Redis and PostgreSQL locally) or access to separate Redis v7+ and PostgreSQL v12+ instances.
  • Basic understanding of Node.js, asynchronous programming, REST APIs, and databases.
  • curl or a tool like Postman for testing the API.

Final Outcome:

By the end of this guide, you'll have a scalable and reliable Node.js application capable of accepting requests to send thousands of SMS messages, processing them reliably in the background, handling errors gracefully, and allowing clients to check the status of their bulk sends.


1. Setting up the project

Initialize your Node.js project and install the necessary dependencies.

1. Initialize the project:

Open your terminal, create a project directory, and navigate into it.

mkdir fastify-sinch-bulk-sms
cd fastify-sinch-bulk-sms
npm init -y

This creates a basic package.json file.

2. Install dependencies:

Install Fastify for the web server, Axios for HTTP requests, BullMQ for the queue, ioredis as the Redis client for BullMQ, dotenv for environment variables, Prisma for database interaction, and pino-pretty for human-readable logs during development.

npm install fastify axios bullmq ioredis dotenv @prisma/client pino-pretty @fastify/rate-limit @fastify/helmet
npm install --save-dev prisma nodemon
  • fastify: The web framework.
  • axios: To make requests to the Sinch API.
  • bullmq: The job queue library.
  • ioredis: Redis client required by BullMQ and potentially rate limiting.
  • dotenv: To load environment variables from a .env file.
  • @prisma/client: Prisma's database client.
  • pino-pretty: Makes Fastify's logs readable during development.
  • @fastify/rate-limit: Plugin for API rate limiting.
  • @fastify/helmet: Plugin for setting security headers.
  • prisma (dev): The Prisma CLI for migrations and generation.
  • nodemon (dev): Automatically restarts the server during development when files change.

3. Configure Development Scripts:

Open your package.json and modify the scripts section:

{

  "scripts": {
    "start": "node src/server.js",
    "dev": "nodemon --watch src --exec 'node -r pino-pretty src/server.js'",
    "worker": "node src/worker.js",
    "dev:worker": "nodemon --watch src src/worker.js",
    "prisma:migrate": "prisma migrate dev",
    "prisma:generate": "prisma generate",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

}
  • start: Runs the main API server for production.
  • dev: Runs the API server in development mode using nodemon and pino-pretty.
  • worker: Runs the background job worker for production.
  • dev:worker: Runs the worker in development mode using nodemon.
  • prisma:migrate: Applies database schema changes.
  • prisma:generate: Generates the Prisma Client based on your schema.

4. Set up Project Structure:

Create the following directory structure within your project root:

fastify-sinch-bulk-sms/
β”œβ”€β”€ prisma/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ workers/
β”‚   β”œβ”€β”€ server.js
β”‚   └── worker.js
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ docker-compose.yml
└── package.json
  • prisma/: Will contain your database schema and migrations.
  • src/: Contains all the application source code.
  • src/config/: Configuration files (e.g., queue setup).
  • src/lib/: Shared libraries/utilities (e.g., Prisma client, Sinch client).
  • src/routes/: Fastify route handlers.
  • src/workers/: Background worker logic.
  • src/server.js: Entry point for the Fastify API server.
  • src/worker.js: Entry point for the BullMQ worker process.
  • .env: Stores environment variables (API keys, database URLs, etc.).
  • .gitignore: Specifies files/directories to ignore in Git.
  • docker-compose.yml: Defines local development services (Postgres, Redis).

5. Create .gitignore:

Create a .gitignore file in the project root to avoid committing sensitive information and unnecessary files:

# .gitignore

node_modules
dist
.env
*.log

# Prisma
prisma/migrations/*/*.sql
prisma/dev.db*

# OS specific
.DS_Store
Thumbs.db

6. Set up Environment Variables (.env):

Create a .env file in the project root. This file will hold your secrets and configuration. Never commit this file to version control.

.env

# .env

# Sinch API Credentials
# Get these from your Sinch Customer Dashboard under SMS -> APIs
SINCH_SERVICE_PLAN_ID="YOUR_SERVICE_PLAN_ID"
SINCH_API_TOKEN="YOUR_API_TOKEN"
SINCH_NUMBER="YOUR_SINCH_VIRTUAL_NUMBER" # e.g., +12025550100
# Sinch API Base URL - adjust region if needed (e.g., us.sms.api.sinch.com)
# See: https://developers.sinch.com/docs/sms/api-reference/regions/
SINCH_API_BASE_URL="https://us.sms.api.sinch.com"

# Redis Connection URL (for BullMQ & Rate Limiting)
# Example for local Docker Redis: redis://localhost:6379
# Can include auth: redis://:yourpassword@localhost:6379
REDIS_URL="redis://localhost:6379"

# Database Connection URL (for Prisma)
# Example for local Docker PostgreSQL: postgresql://user:password@localhost:5432/mydb?schema=public
DATABASE_URL="postgresql://postgres:password@localhost:5432/bulk_sms_db?schema=public"
# Important Security Note: The example uses 'password' for the database.
# NEVER use default or weak passwords in production. Use strong, unique
# passwords and manage secrets securely (e.g., via environment variables
# injected by your deployment platform or a secrets manager), even locally.

# Server Configuration
PORT=3000
HOST="0.0.0.0"

# Queue Name
SMS_QUEUE_NAME="sms_sending_queue"

How to obtain Sinch Credentials:

  1. Log in to your Sinch Customer Dashboard.
  2. Navigate to SMS in the left-hand menu, then select APIs.
  3. You will find your Service plan ID and API token listed here. Click Show to reveal the API token.
  4. To find your Sinch Number, click on the Service plan ID link. Scroll down the service plan details page to find the phone numbers assigned to your account. Use one of these numbers (in E.164 format, e.g., +12xxxxxxxxxx).
  5. Note the Region your service plan is associated with (e.g., US, EU) and ensure the SINCH\_API\_BASE\_URL reflects this.

7. Set up Local Database and Redis (Using Docker):

For local development, Docker Compose is an excellent way to manage dependencies like Redis and PostgreSQL. Create a docker-compose.yml file in the project root:

version: '3.8'
services:
  postgres:
    image: postgres:15
    container_name: bulk_sms_postgres
    environment:
      POSTGRES_DB: bulk_sms_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: bulk_sms_redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:

Run docker-compose up -d in your terminal to start the database and Redis containers in the background. Ensure your .env file's REDIS_URL and DATABASE_URL match the credentials and ports defined here (and change the default password!).


2. Implementing core functionality (Queuing)

Directly sending SMS messages within the API request handler is inefficient and unreliable for bulk operations. A loop sending messages one by one would block the server, timeout easily, and offer no mechanism for retries or status tracking if the server crashes.

The solution is a background job queue. The API endpoint will quickly validate the request and add individual SMS sending tasks (jobs) to a Redis-backed queue (BullMQ). A separate worker process will then consume these jobs independently.

1. Configure BullMQ Queue:

Create a file to manage the queue instance.

import { Queue } from 'bullmq';
import { config } from 'dotenv';

config();

const redisUrl = process.env.REDIS_URL;
if (!redisUrl) {
  throw new Error('REDIS_URL environment variable is not set.');
}

let connection;
try {
  const parsedUrl = new URL(redisUrl);
  connection = {
    host: parsedUrl.hostname || 'localhost',
    port: parseInt(parsedUrl.port || '6379', 10),
    password: parsedUrl.password || undefined,


  };
} catch (error) {
  console.error(`Invalid REDIS_URL format: ${redisUrl}`, error);
  throw new Error(`Invalid REDIS_URL: ${error.message}`);
}

const smsQueue = new Queue(process.env.SMS_QUEUE_NAME || 'sms_sending_queue', {
  connection,
  defaultJobOptions: {
    attempts: 3,
    backoff: {
      type: 'exponential',
      delay: 1000,
    },
    removeOnComplete: true,
    removeOnFail: 500,
  },
});

console.log(`SMS Queue '${smsQueue.name}' initialized.`);
console.log(`Connected to Redis at ${connection.host}:${connection.port}`);

smsQueue.on('error', (error) => {
  console.error(`Queue Error: ${error.message}`);
});

smsQueue.on('waiting', (jobId) => {

});

smsQueue.on('active', (job) => {

});

smsQueue.on('completed', (job, result) => {

});

smsQueue.on('failed', (job, err) => {
  console.error(`Job ${job?.id} failed with error: ${err.message}`);
});

export default smsQueue;
  • We load environment variables using dotenv.
  • We parse the REDIS\_URL using the built-in URL constructor, which is more robust than basic string splitting and handles complex URLs (with auth, different ports, etc.).
  • We create a Queue instance, naming it based on the environment variable.
  • Crucially, we define defaultJobOptions:
  • attempts: 3: If a job fails (e.g., Sinch API is down), BullMQ will retry it up to 3 times.
  • backoff: Specifies how long to wait between retries. exponential increases the delay after each failure (1s, 2s, 4s).
  • removeOnComplete: Cleans up successful jobs from Redis to save space.
  • removeOnFail: Keeps a history of the last 500 failed jobs for debugging.
  • Basic event listeners are added for logging queue activity and errors.

3. Building the API layer (Fastify)

Now, let's create the Fastify server and the API endpoints.

1. Initialize Prisma:

Run the Prisma init command. This creates the prisma directory and a basic schema.prisma file.

npx prisma init --datasource-provider postgresql

Make sure the url in prisma/schema.prisma points to your DATABASE_URL environment variable:

Prisma

// prisma/schema.prisma
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL") // Ensures it reads from .env
}

// Add models in Section 6 (Note: Section 6 is not provided in the input, but this comment remains)

2. Set up Prisma Client:

Create a singleton instance of the Prisma client to be reused across the application.

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: process.env.NODE_ENV === 'development' ? ['query', 'info', 'warn', 'error'] : ['error'],
});

console.log('Prisma Client initialized.');

export default prisma;

3. Create the Sinch Client:

Create a module to encapsulate interactions with the Sinch API.

import axios from 'axios';
import { config } from 'dotenv';

config();

const sinchApi = axios.create({
  baseURL: process.env.SINCH_API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.SINCH_API_TOKEN}`,
  },

  timeout: 10000,
});

export const sendSms = async (to, from, body) => {
  const servicePlanId = process.env.SINCH_SERVICE_PLAN_ID;
  const endpoint = `/xms/v1/${servicePlanId}/batches`;

  try {
    console.log(`Attempting to send SMS via Sinch to: ${to}`);
    const response = await sinchApi.post(endpoint, {
      to: [to],
      from: from,
      body: body,


    });
    console.log(`Sinch API response for ${to}:`, response.status, response.data);

    return response.data;
  } catch (error) {
    console.error(`Error sending SMS to ${to}:`, error.response?.status, error.response?.data || error.message);

    const err = new Error(error.response?.data?.text || `Sinch API error: ${error.message}`);
    err.statusCode = error.response?.status;
    err.details = error.response?.data;
    throw err;
  }
};
  • We use axios.create to configure a base URL and default headers (including the crucial Authorization bearer token). A timeout is added.
  • The sendSms function takes the recipient, sender number, and message body.
  • It constructs the correct endpoint using the SINCH\_SERVICE\_PLAN\_ID.
  • It makes a POST request with the payload required by the Sinch /batches endpoint. Note that even for a single message, the to field expects an array.
  • Basic logging is included.
  • Error handling catches potential issues with the API request and throws a more informative error if possible.

4. Define API Routes:

Create the route handler for submitting bulk SMS jobs.

import smsQueue from '../config/queue.js';
import prisma from '../lib/prismaClient.js';
import { randomUUID } from 'crypto';

const sendBulkSchema = {
  body: {
    type: 'object',
    required: ['recipients', 'message'],
    properties: {
      recipients: {
        type: 'array',
        minItems: 1,
        items: {
          type: 'string',


          pattern: '^\\+[1-9]\\d{1,14}$',
        },
      },
      message: {
        type: 'string',
        minLength: 1,
        maxLength: 1600,
      },
    },
  },
  response: {
    202: {
      type: 'object',
      properties: {
        message: { type: 'string' },
        batchId: { type: 'string', format: 'uuid' },
        jobCount: { type: 'integer' },
      },
    },

    500: {
      type: 'object',
      properties: {
        message: { type: 'string' }
      }
    }
  },
};

const getStatusSchema = {
    params: {
        type: 'object',
        required: ['batchId'],
        properties: {
            batchId: { type: 'string', format: 'uuid' }
        }
    },
    response: {
        200: {
            type: 'object',
            properties: {
                batchId: { type: 'string', format: 'uuid' },
                status: { type: 'string', enum: ['PENDING', 'PROCESSING', 'COMPLETED', 'COMPLETED_WITH_ERRORS', 'FAILED'] },
                totalJobs: { type: 'integer' },
                processedJobs: { type: 'integer' },
                successfulJobs: { type: 'integer' },
                failedJobs: { type: 'integer' },
                createdAt: { type: 'string', format: 'date-time'},
                updatedAt: { type: 'string', format: 'date-time'},
                jobs: {
                    type: 'array',
                    items: {
                        type: 'object',
                        properties: {
                            id: { type: 'string' },
                            recipient: { type: 'string' },
                            status: { type: 'string', enum: ['PENDING', 'ACTIVE', 'COMPLETED', 'FAILED'] },
                            attemptsMade: { type: 'integer' },
                            failedReason: { type: ['string', 'null'] },
                            processedOn: { type: ['string', 'null'], format: 'date-time'},
                            finishedOn: { type: ['string', 'null'], format: 'date-time'}
                        }
                    }
                }
            }
        },
        404: {
            type: 'object',
            properties: {
                message: { type: 'string'}
            }
        },
        500: {
          type: 'object',
          properties: {
              message: { type: 'string'}
          }
        }
    }
};

async function smsRoutes(fastify, options) {
  fastify.post('/send-bulk', { schema: sendBulkSchema }, async (request, reply) => {
    const { recipients, message } = request.body;
    const sinchNumber = process.env.SINCH_NUMBER;
    const batchId = randomUUID();

    if (!sinchNumber) {
        fastify.log.error('Server configuration error: SINCH_NUMBER not set.');
        reply.code(500).send({ message: 'Server configuration error: Sinch number not set.' });
        return;
    }

    let batch;
    try {

      batch = await prisma.smsBatch.create({
          data: {
              id: batchId,
              totalJobs: recipients.length,
              messageBody: message,
              status: 'PENDING',
          },
      });
      fastify.log.info(`Created batch ${batchId} with ${recipients.length} recipients.`);
    } catch (dbError) {
        fastify.log.error(`Database error creating batch ${batchId}: ${dbError.message}`);
        reply.code(500).send({ message: 'Failed to initiate bulk SMS batch.' });
        return;
    }


    const jobPromises = recipients.map((recipient) => {
      const jobId = randomUUID();
      const jobData = {
        jobId,
        batchId,
        to: recipient,
        from: sinchNumber,
        body: message,
      };




      return smsQueue.add('send-single-sms', jobData, { jobId: jobId });
    });

    try {
      const addedJobs = await Promise.all(jobPromises);
      fastify.log.info(`Successfully added ${addedJobs.length} jobs to queue for batch ${batchId}`);


      reply.code(202).send({
        message: 'Bulk SMS request accepted and queued for processing.',
        batchId: batchId,
        jobCount: addedJobs.length,
      });

    } catch (queueError) {
        fastify.log.error(`Error adding jobs to queue for batch ${batchId}: ${queueError.message}`);

        try {
            await prisma.smsBatch.update({
                where: { id: batchId },
                data: { status: 'FAILED', failedReason: 'Failed to queue jobs' }
            });
        } catch (dbError) {
            fastify.log.error(`Failed to update batch ${batchId} status to FAILED after queue error: ${dbError.message}`);
        }
        reply.code(500).send({ message: 'Failed to queue SMS jobs.' });
    }
  });


  fastify.get('/status/:batchId', { schema: getStatusSchema }, async (request, reply) => {
      const { batchId } = request.params;

      try {
          const batch = await prisma.smsBatch.findUnique({
              where: { id: batchId },
              include: {

                  messageJobs: {
                      orderBy: { createdAt: 'asc' }
                  },
              }
          });

          if (!batch) {
              reply.code(404).send({ message: 'Batch not found.' });
              return;
          }


          const processedJobs = batch.messageJobs.filter(j => ['COMPLETED', 'FAILED'].includes(j.status)).length;
          const successfulJobs = batch.messageJobs.filter(j => j.status === 'COMPLETED').length;
          const failedJobs = batch.messageJobs.filter(j => j.status === 'FAILED').length;


          let overallStatus = batch.status;



          if (overallStatus !== 'FAILED') {
              const totalJobs = batch.totalJobs;
              if (processedJobs === totalJobs && totalJobs > 0) {
                 const newStatus = failedJobs > 0 ? 'COMPLETED_WITH_ERRORS' : 'COMPLETED';
                 if (batch.status !== newStatus) {
                    overallStatus = newStatus;

                    prisma.smsBatch.update({ where: {id: batchId}, data: { status: overallStatus, updatedAt: new Date() }})
                      .catch(err => fastify.log.error(`Failed to auto-update batch ${batchId} status to ${overallStatus}: ${err.message}`));
                 }
              } else if (processedJobs > 0 || batch.messageJobs.some(j => j.status === 'ACTIVE')) {
                  const newStatus = 'PROCESSING';
                   if (batch.status === 'PENDING') {
                      overallStatus = newStatus;
                      prisma.smsBatch.update({ where: {id: batchId}, data: { status: overallStatus, updatedAt: new Date() }})
                        .catch(err => fastify.log.error(`Failed to auto-update batch ${batchId} status to ${overallStatus}: ${err.message}`));
                   } else if (batch.status !== 'PROCESSING') {


                      overallStatus = batch.status;
                   } else {
                      overallStatus = 'PROCESSING';
                   }
              }
          }

          reply.code(200).send({
              batchId: batch.id,
              status: overallStatus,
              totalJobs: batch.totalJobs,
              processedJobs: processedJobs,
              successfulJobs: successfulJobs,
              failedJobs: failedJobs,
              createdAt: batch.createdAt,
              updatedAt: batch.updatedAt,
              jobs: batch.messageJobs.map(job => ({
                  id: job.id,
                  recipient: job.recipient,
                  status: job.status,
                  attemptsMade: job.attemptsMade,
                  failedReason: job.failedReason,
                  processedOn: job.processedOn,
                  finishedOn: job.finishedOn,
              }))
          });

      } catch (error) {
          fastify.log.error(`Error fetching status for batch ${batchId}: ${error.message}`);
          reply.code(500).send({ message: 'Internal server error retrieving batch status.' });
      }
  });

}

export default smsRoutes;
  • We import the smsQueue and prisma client.
  • sendBulkSchema and getStatusSchema use Fastify's built-in JSON Schema validation. The regex pattern in sendBulkSchema is corrected to include an end anchor ($) and remove the erroneous trailing comma.
  • /send-bulk endpoint:
  • Generates a unique batchId.
  • Creates a SmsBatch record in the database first.
  • Loops through the recipients array.
  • For each recipient, it creates a jobData object containing all necessary info (to, from, body, batchId, jobId).
  • It adds the job to the smsQueue using smsQueue.add(). We give the job a name ('send-single-sms') and pass the jobData. We also use our generated jobId as the BullMQ job ID.
  • Promise.all waits for all jobs to be added to the queue.
  • Responds with 202 Accepted, indicating the task is queued, along with the batchId.
  • Includes error handling for database and queue operations.
  • /status/:batchId endpoint:
  • Retrieves the batchId from the URL parameters.
  • Queries the database using Prisma to find the SmsBatch and its associated SmsMessageJob records.
  • Calculates summary statistics.
  • Determines an overall status for the batch based on the status of individual jobs, potentially updating the database status asynchronously if it has changed (e.g., from PENDING to PROCESSING or PROCESSING to COMPLETED).
  • Returns the batch details and the status of each individual message job.
  • Handles the case where the batchId is not found (404).

5. Create the Fastify Server:

Set up the main server entry point.

import Fastify from 'fastify';
import { config } from 'dotenv';
import smsRoutes from './routes/smsRoutes.js';
import prisma from './lib/prismaClient.js';
import rateLimit from '@fastify/rate-limit';
import helmet from '@fastify/helmet';
import Redis from 'ioredis';

config();

const fastify = Fastify({
  logger: {
    level: process.env.LOG_LEVEL || 'info',

    transport: process.env.NODE_ENV !== 'production'
      ? { target: 'pino-pretty' }
      : undefined,
  },



});

await fastify.register(helmet, {


});

if (process.env.REDIS_URL) {
  try {
    const redisClient = new Redis(process.env.REDIS_URL, {

        maxRetriesPerRequest: 3,
        showFriendlyErrorStack: process.env.NODE_ENV !== 'production',
    });
    redisClient.on('error', (err) => fastify.log.error('Rate Limit Redis Error:', err));

    await fastify.register(rateLimit, {
      max: 100,
      timeWindow: '1 minute',
      redis: redisClient,


    });
     fastify.log.info('Rate limiting enabled with Redis backend.');
  } catch (redisError) {
      fastify.log.error('Failed to initialize Redis for rate limiting:', redisError);






  }
} else {

  await fastify.register(rateLimit, { max: 100, timeWindow: '1 minute' });
  fastify.log.warn('REDIS_URL not set. Rate limiting falling back to in-memory store (less effective when scaled).');
}

await fastify.register(smsRoutes, { prefix: '/api/sms' });

const signals = ['SIGINT', 'SIGTERM'];
signals.forEach((signal) => {
  process.on(signal, async () => {
    fastify.log.info(`Received ${signal}, shutting down gracefully...`);
    await fastify.close();

    await prisma.$disconnect();


    fastify.log.info('Server shut down.');
    process.exit(0);
  });
});

const start = async () => {
  try {
    const port = parseInt(process.env.PORT || '3000', 10);
    const host = process.env.HOST || '0.0.0.0';
    await fastify.listen({ port, host });
    fastify.log.info(`Server listening on port ${port}`);

    if (process.env.NODE_ENV !== 'production') {
        console.log(fastify.printRoutes());
    }

    await prisma.$connect().then(() => {
        fastify.log.info('Prisma client connected successfully.');
    }).catch(err => {
        fastify.log.error('Prisma client failed to connect:', err);


    });

  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();

Frequently Asked Questions (FAQ)

How do I send bulk SMS messages with Fastify and Sinch API?

Build a Fastify API endpoint that accepts a list of recipients and a message body, then use BullMQ to queue individual SMS jobs for background processing. Install dependencies with npm install fastify axios bullmq ioredis @prisma/client, configure Sinch API credentials (Service Plan ID, API Token, and phone number in E.164 format), set up Redis and PostgreSQL, and create a worker process that consumes jobs from the queue and sends SMS via Sinch's REST API. The system handles retries automatically using BullMQ's exponential backoff (3 attempts with 1-second initial delay) and tracks message status in PostgreSQL using Prisma ORM. Learn more about Sinch SMS API integration with Node.js in the official documentation.

Which Node.js version should I use for a Fastify v5 bulk SMS system?

Use Node.js v20 LTS "Iron" (Maintenance LTS through April 2026) or Node.js v22 LTS "Jod" (Active LTS through October 2025, Maintenance LTS through April 2027). Fastify v5 requires Node.js v20+ minimum and offers 5 – 10% performance improvements over v4. Node.js v18 reached End of Life on April 30, 2025 and no longer receives security updates. Node.js v22 is recommended for new projects as it provides Active LTS support throughout 2025 with the latest features.

How do I configure BullMQ v5 for reliable SMS job processing?

Create a BullMQ Queue instance with explicit connection object (required in v5.x), set retry attempts to 3 with exponential backoff strategy (initial 1-second delay), enable automatic job cleanup (removeOnComplete: true, removeOnFail: 500), and configure Redis connection using ioredis client. BullMQ v5.60.0 no longer supports integer job IDs – use string UUIDs instead. Set up event listeners for monitoring queue health (error, failed, completed events) and implement graceful shutdown by closing queue connections on SIGINT/SIGTERM signals.

What Sinch API credentials do I need for bulk SMS?

Obtain three credentials from your Sinch Customer Dashboard: (1) Service Plan ID found under SMS β†’ APIs, (2) API Token (click "Show" to reveal), and (3) Sinch Virtual Number in E.164 format (e.g., +12025550100) from your service plan details. Configure the API base URL based on your region: https://us.sms.api.sinch.com for US or EU endpoint for Europe. Sinch provides direct connections to 600+ mobile operators worldwide with 95% US population coverage. Store credentials securely in .env file and never commit to version control.

How do I implement error handling and retries for bulk SMS?

Use BullMQ's built-in retry mechanism with attempts: 3 and exponential backoff (type: 'exponential', delay: 1000) to automatically retry failed jobs with increasing delays (1s, 2s, 4s). Wrap Sinch API calls in try-catch blocks, log errors with recipient context, store failure reasons in PostgreSQL via Prisma (failedReason field), and track attempt counts (attemptsMade). Implement job-level error handling in your worker processor to catch transient failures (network issues, rate limits) versus permanent failures (invalid phone numbers). Use BullMQ's failed event listener to log jobs that exceed max retry attempts.

How do I set up rate limiting for a Fastify bulk SMS API?

Install @fastify/rate-limit and configure with Redis backend for distributed rate limiting across multiple server instances. Set max requests per time window (e.g., max: 100, timeWindow: '1 minute') based on your API capacity and Sinch API rate limits. Use Redis connection for stateful tracking: redis: new Redis(process.env.REDIS_URL). Implement IP-based limiting by default or custom key generation for user-based limits. Add allowlist for trusted IPs, configure appropriate HTTP 429 responses, and log rate limit violations for monitoring.

What database schema should I use for tracking bulk SMS batches?

Implement two main tables: (1) SmsBatch with id (UUID), totalJobs, status (PENDING/PROCESSING/COMPLETED/COMPLETED\_WITH\_ERRORS/FAILED), messageBody, createdAt, updatedAt, and failedReason, and (2) SmsMessageJob with id (UUID), batchId (foreign key), recipient, status (PENDING/ACTIVE/COMPLETED/FAILED), attemptsMade, failedReason, processedOn, finishedOn, and timestamps. Add indexes on batchId and status for faster queries. Use Prisma ORM v6.x with PostgreSQL v12+ for type-safe database access, automatic migrations, and relationship management between batches and individual message jobs.

How do I deploy a Fastify bulk SMS system to production?

Use environment-specific configurations: set NODE_ENV=production, configure robust logging (replace pino-pretty with structured JSON logs), enable Fastify's trust proxy option if behind a load balancer, use managed Redis (AWS ElastiCache, Redis Cloud) and PostgreSQL (AWS RDS, Supabase) services, implement health check endpoints (/health) for load balancer monitoring, configure SSL/TLS termination, set up monitoring with Prometheus/Datadog for queue metrics, implement graceful shutdown handlers for SIGTERM signals, use PM2 or Docker for process management, and run separate worker instances scaled independently from API servers.

What are the cost considerations for sending bulk SMS with Sinch?

Sinch SMS pricing varies by destination country (US SMS typically $0.01 – $0.02 per message) with volume discounts available. Calculate infrastructure costs: Redis hosting ($10 – $50/month for managed instances), PostgreSQL hosting ($20 – $100/month depending on scale), Node.js hosting (serverless or container-based $20 – $200/month), and network egress fees. Optimize costs by batching messages efficiently, implementing intelligent retry logic to avoid wasted attempts on permanently failed numbers, using connection pooling for database and Redis, and monitoring queue metrics to right-size worker instances. Volume commitments with Sinch can reduce per-message costs significantly for high-volume senders.

How do I scale a Fastify bulk SMS system for millions of messages?

Scale horizontally by running multiple worker instances consuming from the same BullMQ queue, use Redis Cluster for distributed queue backend handling high throughput, implement database connection pooling with Prisma (set connection_limit in DATABASE\_URL), partition large batches into smaller chunks (1,000 – 10,000 messages per batch) for better progress tracking, use database read replicas for status queries to reduce load on primary database, implement caching for frequently accessed batch status with Redis, monitor queue metrics (waiting jobs, processing rate, failed jobs) and auto-scale workers based on queue depth, and use CDN/edge caching for API documentation and status endpoints.

What security measures should I implement for a bulk SMS API?

Implement multiple security layers: (1) Use @fastify/helmet for security headers (CSP, HSTS, X-Frame-Options), (2) Configure @fastify/rate-limit with Redis backend to prevent abuse (100 requests per minute per IP), (3) Implement API key authentication middleware requiring valid tokens in Authorization header, (4) Validate all inputs using Fastify JSON Schema validation with E.164 phone number pattern (^\\+[1-9]\\d{1,14}$), (5) Store sensitive credentials in environment variables never in code, (6) Use HTTPS/TLS for all API communication, (7) Implement CORS policies restricting allowed origins, (8) Log security events (failed auth, rate limit violations) for monitoring, (9) Use prepared statements via Prisma to prevent SQL injection, and (10) Implement IP allowlisting for admin endpoints.

How do I monitor and debug BullMQ job failures?

Enable detailed logging in your worker processor with recipient context, use BullMQ's event listeners (failed, error, stalled) to capture and log job failures, query PostgreSQL for failed jobs with status: 'FAILED' and review failedReason field, implement structured logging with correlation IDs linking batch β†’ job β†’ API call, set up error alerting using monitoring tools (Sentry, Datadog) triggered on high failure rates, use BullMQ's removeOnFail: 500 to retain recent failures for analysis, create admin endpoints to manually retry failed jobs by re-queuing with original data, monitor Redis memory usage to prevent queue overflow, and implement dead letter queue pattern for jobs exceeding max retry attempts requiring manual intervention.

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 β†’