Compliance Quick Reference: Complete guide to building a production-ready bulk SMS broadcasting service using NestJS, TypeScript, and Infobip API. Includes authentication, validation, rate limiting, error handling, and retry logic. Verify live Sender ID preservation and carrier filtering before launch with SMSRoute Live Carrier Testing or route directly via the SMSRoute SMS API.
This guide provides a complete walkthrough for building a robust bulk SMS broadcasting service using NestJS and the Infobip API. We will create a backend application capable of accepting lists of phone numbers and a message, then efficiently dispatching these messages via Infobip's communication platform.
This solution addresses the need for applications to send notifications, alerts, or marketing messages to multiple recipients simultaneously, leveraging Infobip's scalable infrastructure. We'll focus on creating a reliable, maintainable, and secure service suitable for production environments.
Prerequisites:
- Node.js 14+ (LTS version recommended) and npm or yarn
- An active Infobip account with API access
- Basic understanding of TypeScript, Node.js, NestJS, and REST APIs
- Docker (optional, for containerized deployment)
- A code editor (e.g., VS Code)
- A tool for making API requests (e.g., curl, Postman)
Technology Stack:
- NestJS: A progressive Node.js framework for building efficient, reliable server-side applications. Its modular architecture and built-in features (dependency injection, validation, configuration) accelerate development.
- Infobip API & Node.js SDK (
@infobip-api/sdk): Provides the interface to Infobip's SMS sending capabilities. We use the official SDK for ease of integration. - TypeScript: Enhances JavaScript with static typing for better code quality and maintainability.
- Dotenv /
@nestjs/config: For managing environment variables securely. class-validator/class-transformer: For robust request data validation.@nestjs/throttler: For rate limiting API requests.- Winston / NestJS Logger /
nestjs-pino: For structured logging. helmet: For basic security headers.p-retry: For implementing retry logic on API calls.
System Architecture:
(Note: A graphical diagram illustrating this flow is recommended.)
The system involves a client (like Postman or another application) sending a POST request to the /broadcast endpoint of the NestJS API Gateway (specifically, the BroadcastController). This controller validates the request and calls the InfobipService. The InfobipService handles the core logic, using the Infobip Node.js SDK (@infobip-api/sdk) to interact with the external Infobip API, which ultimately sends the SMS messages. Configuration is managed via .env files loaded by @nestjs/config, and various dependencies like validation, rate limiting, security headers, and retry mechanisms support the core functionality. The API Gateway returns a response (e.g., 202 Accepted with a bulkId) back to the client.
Final Outcome:
By the end of this guide, you will have a NestJS application with a single API endpoint (POST /broadcast) that:
- Accepts a list of phone numbers and a message payload.
- Validates the input data.
- Rate-limits incoming requests.
- Uses the Infobip Node.js SDK to send the message to all specified recipients in a single API call (bulk send).
- Handles potential errors gracefully, including retries.
- Provides structured logs for monitoring.
- Returns the bulkId provided by Infobip for tracking purposes.
1. Setting up the Project
Let's initialize our NestJS project and install the necessary dependencies.
- Install NestJS CLI (if you haven't already):
npm install -g @nestjs/cli
yarn global add @nestjs/cli
- Create a new NestJS project:
nest new infobip-bulk-sender
Choose your preferred package manager (npm or yarn) when prompted.
- Navigate into the project directory:
cd infobip-bulk-sender
- Install required dependencies:
- @infobip-api/sdk: The official Infobip SDK for Node.js.
- @nestjs/config: For managing environment variables (integrates well with dotenv).
- @nestjs/throttler: For implementing rate limiting.
- class-validator & class-transformer: Peer dependencies for @nestjs/common's built-in ValidationPipe.
- helmet: Middleware for setting security-related HTTP headers.
- p-retry: Utility for retrying asynchronous operations.
- dotenv: Loads environment variables from a .env file.
npm install @infobip-api/sdk @nestjs/config @nestjs/throttler class-validator class-transformer helmet p-retry dotenv
yarn add @infobip-api/sdk @nestjs/config @nestjs/throttler class-validator class-transformer helmet p-retry dotenv
- Set up Environment Variables: Create a
.envfile in the project root for your Infobip credentials and application settings. Never commit this file to version control.
ini
PORT=3000
INFOBIP_BASE_URL=your_infobip_base_url.api.infobip.com
INFOBIP_API_KEY=your_infobip_api_key
INFOBIP_SENDER_ID=YourSenderID
Also, create a .env.example file to track required variables: ini
PORT=3000
INFOBIP_BASE_URL=
INFOBIP_API_KEY=
INFOBIP_SENDER_ID=
Add .env to your .gitignore file if it's not already there.
- Configure Core Modules (
app.module.ts): Updatesrc/app.module.tsto import and configure theConfigModuleandThrottlerModule.
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { InfobipModule } from './infobip/infobip.module';
import { BroadcastModule } from './broadcast/broadcast.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
ThrottlerModule.forRoot([{
ttl: 60000,
limit: 10,
}]),
InfobipModule,
BroadcastModule,
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}
- ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env' }): Loads variables from .env and makes ConfigService available everywhere without needing to import ConfigModule in other modules.
- ThrottlerModule.forRoot(...): Sets up default rate limiting rules. We allow 10 requests per minute per IP address. This protects our API from abuse.
- { provide: APP_GUARD, useClass: ThrottlerGuard }: Applies the rate limiting guard globally.
- Update Main Application File (
main.ts): Modifysrc/main.tsto enable theValidationPipeglobally, usehelmet, enable CORS, and use the configured port.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { Logger, ValidationPipe } from '@nestjs/common';
import helmet from 'helmet';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const port = configService.get<number>('PORT', 3000);
app.use(helmet());
app.enableCors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
});
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
}));
const logger = new Logger('Bootstrap');
await app.listen(port);
logger.log(`Application listening on port ${port}`);
logger.log(`Infobip Base URL configured: ${configService.get('INFOBIP_BASE_URL')}`);
}
bootstrap();
- app.get(ConfigService): Retrieves the ConfigService instance.
- configService.get<number>('PORT', 3000): Gets the PORT environment variable, defaulting to 3000.
- app.use(helmet()): Adds various HTTP headers to improve security (e.g., X-Frame-Options, HSTS). We installed helmet in Step 4.
- app.enableCors(): Enables Cross-Origin Resource Sharing. Configure appropriately for your frontend's origin in production.
- app.useGlobalPipes(new ValidationPipe(...)): Enables automatic request validation based on DTOs decorated with class-validator. This is crucial for API robustness.
2. Implementing Core Functionality (Infobip Service)
We'll encapsulate the Infobip SDK interaction within a dedicated NestJS service.
- Generate the Infobip Module and Service:
nest generate module infobip
nest generate service infobip --no-spec
This creates src/infobip/infobip.module.ts and src/infobip/infobip.service.ts. The --no-spec flag skips generating test files for now.
- Implement the
InfobipService: Editsrc/infobip/infobip.service.ts.
import { Injectable, Logger, InternalServerErrorException, BadGatewayException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Infobip, AuthType } from '@infobip-api/sdk';
import pRetry from 'p-retry';
@Injectable()
export class InfobipService {
private readonly logger = new Logger(InfobipService.name);
private infobipClient: Infobip;
private defaultSenderId: string;
constructor(private configService: ConfigService) {
const apiKey = this.configService.get<string>('INFOBIP_API_KEY');
const baseUrl = this.configService.get<string>('INFOBIP_BASE_URL');
this.defaultSenderId = this.configService.get<string>('INFOBIP_SENDER_ID');
if (!apiKey || !baseUrl || !this.defaultSenderId) {
this.logger.error('Infobip API Key, Base URL, or Sender ID missing in configuration.');
throw new InternalServerErrorException('Infobip service configuration is incomplete.');
}
try {
this.infobipClient = new Infobip({
baseUrl: baseUrl,
apiKey: apiKey,
authType: AuthType.ApiKey,
});
this.logger.log('Infobip client initialized successfully.');
} catch (error) {
this.logger.error('Failed to initialize Infobip client', error.stack);
throw new InternalServerErrorException('Failed to initialize Infobip client');
}
}
async sendBulkSms(recipients: string[], messageText: string, senderId?: string): Promise<any> {
if (!recipients || recipients.length === 0) {
this.logger.warn('Attempted to send bulk SMS with no recipients.');
return { success: false, message: 'No recipients provided.' };
}
const uniqueRecipients = [...new Set(recipients)];
if (uniqueRecipients.length < recipients.length) {
this.logger.warn(`Removed ${recipients.length - uniqueRecipients.length} duplicate recipients.`);
}
const destinations = uniqueRecipients.map(recipient => ({ to: recipient }));
const effectiveSenderId = senderId || this.defaultSenderId;
const payload = {
messages: [
{
destinations: destinations,
from: effectiveSenderId,
text: messageText,
},
],
};
this.logger.log(`Attempting to send bulk SMS to ${uniqueRecipients.length} unique recipients via Infobip...`);
this.logger.debug(`Payload: ${JSON.stringify(payload)}`);
try {
const runInfobipCall = async () => {
this.logger.debug('Executing Infobip API call attempt...');
const response = await this.infobipClient.channels.sms.send(payload);
return response;
};
const response = await pRetry(runInfobipCall, {
retries: 3,
minTimeout: 1000,
factor: 2,
onFailedAttempt: error => {
this.logger.warn(
`Infobip API call attempt ${error.attemptNumber} failed. Retries left: ${error.retriesLeft}. Error: ${error.message}`
);
const statusCode = error.response?.status || error.code;
if (statusCode && statusCode >= 400 && statusCode < 500) {
this.logger.error(`Client-side error detected (${statusCode}). Aborting retries.`);
throw error;
}
},
});
this.logger.log(`Infobip API call successful. Bulk ID: ${response.data.bulkId}`);
this.logger.verbose(`Infobip Full Response: ${JSON.stringify(response.data)}`);
return response.data;
} catch (error) {
this.logger.error(`Failed to send bulk SMS via Infobip after retries. Error: ${error.message}`, error.stack);
if (error.response?.data) {
this.logger.error(`Infobip Error Details: ${JSON.stringify(error.response.data)}`);
const errorMessage = error.response.data?.requestError?.serviceException?.text || error.message;
throw new BadGatewayException(`Infobip API Error: ${errorMessage}`);
}
throw new BadGatewayException(`Failed to communicate with Infobip API after retries: ${error.message}`);
}
}
}
- Constructor: Initializes the Infobip client using credentials from ConfigService.
sendBulkSmsMethod:- Includes basic recipient deduplication.
- Maps recipients to the correct destinations format [{ to: string }, ...].
- Constructs the Infobip payload.
- Wraps the this.infobipClient.channels.sms.send call within pRetry for resilience against transient errors.
- Logs attempts, success, and detailed errors.
- Throws BadGatewayException if the call fails after all retries.
- Update the
InfobipModule: Make sure theInfobipServiceis provided and exported.
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { InfobipService } from './infobip.service';
@Module({
imports: [ConfigModule],
providers: [InfobipService],
exports: [InfobipService],
})
export class InfobipModule {}
3. Building the API Layer (Broadcast Controller)
Now, let's create the API endpoint.
- Generate the Broadcast Module and Controller:
nest generate module broadcast
nest generate controller broadcast --no-spec
- Create a Data Transfer Object (DTO) for Validation: Create
src/broadcast/dto/create-broadcast.dto.ts. (Optional: For Swagger documentation, install@nestjs/swagger swagger-ui-expressand configure it inmain.ts)
npm install @nestjs/swagger swagger-ui-express
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString, ArrayMinSize, IsPhoneNumber, IsOptional } from 'class-validator';
export class CreateBroadcastDto {
@ApiProperty({
description: 'Array of recipient phone numbers in E.164 format',
example: ['+447123456789', '+14155552671'],
type: [String],
})
@IsArray()
@ArrayMinSize(1)
@IsPhoneNumber(undefined, { each: true, message: 'Each recipient must be a valid phone number in E.164 format (e.g., +447123456789)' })
recipients: string[];
@ApiProperty({
description: 'The text message content to send',
example: 'Hello from our service!',
})
@IsString()
@IsNotEmpty()
message: string;
@ApiProperty({
description: 'Optional: Sender ID to use for this broadcast, overriding the default.',
example: 'InfoSMS',
required: false,
})
@IsString()
@IsNotEmpty()
@IsOptional()
senderId?: string;
}
- Uses class-validator decorators (@IsArray, @IsString, @IsNotEmpty, @ArrayMinSize, @IsPhoneNumber, @IsOptional).
- @IsPhoneNumber: Provides basic validation. See Section 6 for more on robust validation.
- @IsOptional: Makes senderId optional.
- @ApiProperty: Added for potential Swagger integration.
- Implement the
BroadcastController: Editsrc/broadcast/broadcast.controller.ts.
import { Controller, Post, Body, HttpCode, HttpStatus, Logger } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
import { InfobipService } from '../infobip/infobip.service';
import { CreateBroadcastDto } from './dto/create-broadcast.dto';
@Controller('broadcast')
export class BroadcastController {
private readonly logger = new Logger(BroadcastController.name);
constructor(
private readonly infobipService: InfobipService,
) {}
@Post()
@HttpCode(HttpStatus.ACCEPTED)
@Throttle({ default: { limit: 5, ttl: 60000 } })
async sendBroadcast(@Body() createBroadcastDto: CreateBroadcastDto) {
this.logger.log(`Received broadcast request for ${createBroadcastDto.recipients.length} recipients.`);
try {
const result = await this.infobipService.sendBulkSms(
createBroadcastDto.recipients,
createBroadcastDto.message,
createBroadcastDto.senderId,
);
if (result && result.success === false) {
this.logger.warn(`Broadcast request rejected by service: ${result.message}`);
return {
statusCode: HttpStatus.BAD_REQUEST,
message: result.message || 'Broadcast request could not be processed.',
};
}
this.logger.log(`Broadcast request processed successfully by Infobip. Bulk ID: ${result.bulkId}`);
return {
message: 'Broadcast request accepted for processing by Infobip.',
bulkId: result.bulkId,
};
} catch (error) {
this.logger.error(`Broadcast request failed: ${error.message}`, error.stack);
throw error;
}
}
}
- @Controller('broadcast'): Defines the base route.
- @Post(): Handles POST requests.
- @HttpCode(HttpStatus.ACCEPTED): Sets default success status to 202.
- @Throttle(...): Overrides global rate limit for this endpoint.
- @Body() createBroadcastDto: CreateBroadcastDto: Validates the request body using the DTO and global ValidationPipe.
- Injects InfobipService.
- Calls infobipService.sendBulkSms.
- Returns the bulkId on success.
- Rethrows errors from the service layer.
- Update the
BroadcastModule: ImportInfobipModule.
import { Module } from '@nestjs/common';
import { BroadcastController } from './broadcast.controller';
import { InfobipModule } from '../infobip/infobip.module';
@Module({
imports: [
InfobipModule,
],
controllers: [BroadcastController],
providers: [],
})
export class BroadcastModule {}
- Testing the API Endpoint: Start the application:
npm run start:dev
yarn start:dev
Use curl or Postman: Using curl: (Replace placeholders)
curl -X POST http://localhost:3000/broadcast \
-H "Content-Type: application/json" \
-d '{
"recipients": ["+14155550100", "+447123456789"],
"message": "Test broadcast from NestJS!",
"senderId": "OptionalSender"
}'
Expected Success Response (202 Accepted):
{
"message": "Broadcast request accepted for processing by Infobip.",
"bulkId": "some-unique-bulk-id-from-infobip"
}
Example Validation Error Response (400 Bad Request): (If sending invalid data, e.g., missing message)
{
"statusCode": 400,
"message": [
"message should not be empty",
"message must be a string"
],
"error": "Bad Request"
}
4. Integrating with Infobip (Details & Configuration)
Recap of the integration specifics handled in InfobipService.
- Obtaining Credentials:
- Log in to your Infobip Portal.
- Navigate to Developers -> API Keys.
- Create/copy your API Key.
- Note your account-specific Base URL (e.g., xxxxx.api.infobip.com).
- Check Channels and Numbers -> Sender Names for your INFOBIP_SENDER_ID. Ensure it's registered/approved if needed. Free trials often have restrictions.
- Environment Variables: Ensure these are correctly set in
.env: - INFOBIP_BASE_URL
- INFOBIP_API_KEY
- INFOBIP_SENDER_ID
- Secure Handling:
- Use .env locally (add to .gitignore).
- Use platform-specific secrets management (AWS Secrets Manager, Kubernetes Secrets, etc.) in production. Never hardcode secrets.
- Fallback Mechanisms (Retry):
- The InfobipService now uses p-retry to automatically retry failed API calls (due to network issues or temporary Infobip 5xx errors) with exponential backoff.
- A circuit breaker (e.g., using opossum) could be added for prolonged outages, preventing repeated calls to a known-failing service.
5. Error Handling, Logging, and Retry Mechanisms
Robustness is key.
- Error Handling Strategy:
- Validation Errors (400): Handled by ValidationPipe.
- Rate Limiting Errors (429): Handled by @nestjs/throttler.
- Configuration Errors (500): Handled in InfobipService constructor.
- Infobip API Errors (502): Caught in InfobipService after retries, logged, and rethrown as BadGatewayException.
- Unexpected Errors (500): Handled by NestJS default exception filter.
- Logging:
- Uses built-in Logger (@nestjs/common). Logs info, errors, warnings, debug, verbose messages.
- Structured Logging (Recommended): For production, use nestjs-pino for JSON logs compatible with aggregation systems (Datadog, Splunk, ELK).
- Install:
npm install nestjs-pino pino-http pino-pretty
- Setup (Conceptual):
import { Logger } from 'nestjs-pino';
app.useLogger(app.get(Logger));
import { LoggerModule } from 'nestjs-pino';
@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: {
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
transport: process.env.NODE_ENV !== 'production'
? { target: 'pino-pretty' }
: undefined,
},
}),
],
})
export class AppModule {}
Replace new Logger(...) calls with dependency injection or ensure the global logger is used correctly.
- Retry Mechanisms:
- Implemented in InfobipService using p-retry (installed in Section 1, used in Section 2).
- Retries up to 3 times with exponential backoff (1s, 2s, 4s delays).
- Logs failed attempts.
- Avoids retrying client-side (4xx) errors.
6. Database Schema and Data Layer (Recommendation)
While not implemented here, persistence is crucial for production.
- Why a Database? Track broadcast history, statuses, manage recipients, schedule messages, store delivery reports (via webhooks).
- Recommendation: Use PostgreSQL/MySQL with Prisma or TypeORM.
- Conceptual Prisma Schema:
Prisma
// schema.prisma
datasource db {
provider = ""postgresql"" // or ""mysql"", ""sqlite""
url = env(""DATABASE_URL"")
}
generator client {
provider = ""prisma-client-js""
}
model Broadcast {
id String @id @default(cuid())
bulkId String? @unique // Infobip bulk ID
messageText String
senderId String
status String @default(""PENDING"") // PENDING, SENT, FAILED, PARTIAL
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
recipients RecipientStatus[]
}
model RecipientStatus {
id String @id @default(cuid())
broadcast Broadcast @relation(fields: [broadcastId], references: [id])
broadcastId String
phoneNumber String // E.164 format
messageId String? // Infobip message ID for this recipient
status String // e.g., PENDING, SENT, DELIVERED, FAILED, REJECTED
statusReason String?
updatedAt DateTime @updatedAt
@@unique([broadcastId, phoneNumber])
}
- Implementation: Integrate Prisma/TypeORM into NestJS, create a service to handle database operations (create broadcast record, update statuses), and call this service from the
BroadcastControllerand potentially a webhook handler for delivery reports. This is beyond the scope of this initial guide but essential for a complete solution. Consider using a message queue (like BullMQ) for large broadcasts to decouple API requests from database writes and Infobip calls.
Navigate Global SMS Compliance & Carrier Filtering With SMSRoute
Carrier rules, Sender ID restrictions, and opt-out enforcement vary sharply by country. SMSRoute pairs direct Tier-1 carrier interconnects with automatic opt-out suppression and real-SIM verification so your legitimate transactional and OTP traffic lands reliably without multi-week aggregator delays.
curl -X POST https://api.sendsmsnokyc.com/v3/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910. Reply STOP to opt out."
}'
- Sender ID & Content Verification: Fire real handset probes to verify Sender ID preservation and catch silent carrier filtering via SMSRoute Live Carrier Testing.
- Automatic Opt-Out Enforcement: Inbound
STOPreplies are suppressed automatically before future sends hit the carrier. - Transparent Per-Country Rates: Check exact termination costs across 190+ markets on our live pricing page.
Verify Carrier Delivery & Sender IDs Before You Launch
Run real-SIM carrier probes, audit DLR latency, and send global SMS over direct SMSRoute interconnects.
Run a Live Carrier Test β