Developer Quick-Start: A guide on building a NestJS application to send SMS via Infobip and track delivery status using callbacks, including setup, database integration, and API implementation. 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 complete walkthrough for building a robust system in NestJS (using Node.js) to send SMS messages via the Infobip API and reliably track their delivery status using Infobip's callback mechanism. We'll cover everything from project setup to production deployment considerations.
Last Updated: September 5, 2024
Project Overview and Goals
This project aims to create a NestJS application capable of:
- Sending SMS Messages: Exposing an API endpoint to trigger sending SMS messages via the Infobip platform.
- Tracking Delivery Status: Receiving asynchronous delivery status updates (callbacks) from Infobip for each sent message.
- Persisting Message State: Storing message details and their delivery status updates in a database for querying and auditing.
Problem Solved: Sending an SMS is often a ""fire and forget"" action. However, knowing if and when a message was actually delivered is crucial for many applications (e.g., OTP verification, critical alerts, appointment reminders). Relying solely on the initial API response isn't enough, as delivery can be delayed or fail downstream. Infobip's callbacks provide this essential feedback loop.
Technologies Used:
- Node.js: The JavaScript runtime environment.
- NestJS: A progressive Node.js framework for building efficient, reliable, and scalable server-side applications. Its modular architecture and built-in features (dependency injection, validation, configuration) make it ideal for this task.
- Infobip API & Node.js SDK: The official SDK simplifies interactions with Infobip's SMS API.
- Prisma: A modern database toolkit (ORM) for Node.js and TypeScript, used for database schema management and data access.
- SQLite (or PostgreSQL/MySQL): The database for storing message information. SQLite is used for simplicity in this guide, but Prisma makes switching easy.
@nestjs/config: For managing environment variables securely.class-validator&class-transformer: For robust request validation.@nestjs/throttler: For rate limiting API endpoints (See Section 7).@nestjs/terminus: For application health checks (See Section 11).
System Architecture:
+-------------+ +----------------------+ +-----------------+
| Your Client | ----> | NestJS API | ----> | Infobip API |
| (Web/Mobile)| | (Send SMS Endpoint) | | (Send Message) |
+-------------+ +----------------------+ +-----------------+
| ^
| Stores | Receives
v |
+-------------+
| Database |
| (Message |
| Status) |
+-------------+
^ |
| Updates | Sends
| v
+----------------------+ +-----------------+
| NestJS API | <---- | Infobip Callback|
| (Callback Endpoint) | | (Delivery Status)|
+----------------------+ +-----------------+
Prerequisites:
- Node.js (v18 or later recommended - ideally a current LTS version) and npm/yarn installed.
- An active Infobip account (https://www.infobip.com/).
- Basic understanding of TypeScript, REST APIs, and asynchronous programming.
- A publicly accessible URL for receiving Infobip callbacks during development (we'll use ngrok, a service that creates secure tunnels to your localhost, for this).
Final Outcome: A NestJS application with endpoints to send SMS and receive delivery reports, storing the status persistently.
1. Setting up the Project
Let's initialize our NestJS project and configure the basic structure.
1.1 Install NestJS CLI: If you don't have it, install the NestJS CLI globally.
npm install -g @nestjs/cli
1.2 Create New Project: Generate a new NestJS project.
nest new nestjs-infobip-sms
cd nestjs-infobip-sms
1.3 Install Dependencies: We need the Infobip SDK, configuration management, validation pipes, and Prisma.
npm install @infobip-api/sdk @nestjs/config class-validator class-transformer prisma @prisma/client
npm install --save-dev @types/node prisma
1.4 Initialize Prisma: Set up Prisma with SQLite (you can choose postgresql or
mysql if preferred).
npx prisma init --datasource-provider sqlite
This creates a prisma directory with a schema.prisma file and a .env
file.
1.5 Configure Environment Variables: Modify the .env file created by Prisma.
Add your Infobip credentials and an application base URL (needed for constructing the callback URL).
Important: Replace the placeholder values below (YOUR_INFOBIP_API_KEY,
YOUR_INFOBIP_BASE_URL) with your actual credentials from Infobip.
.env
# .env
# --- Database ---
# Prisma automatically sets this based on your choice in `npx prisma init`
DATABASE_URL=""file:./dev.db""
# --- Infobip ---
# Obtain from your Infobip account dashboard (API Keys section)
INFOBIP_API_KEY=""YOUR_INFOBIP_API_KEY""
# Find your Base URL on the Infobip dashboard homepage or API documentation
INFOBIP_BASE_URL=""YOUR_INFOBIP_BASE_URL"" # e.g., yxpv6d.api.infobip.com
# --- Application ---
# The base URL where your NestJS app will be publicly accessible.
# Use ngrok URL during development, e.g., https://your-ngrok-subdomain.ngrok.io
APP_BASE_URL=""http://localhost:3000""
INFOBIP_API_KEY: Your unique key for authenticating with the Infobip API. Generate one in your Infobip account under ""API Keys"". Keep this secret.INFOBIP_BASE_URL: The specific API endpoint URL assigned to your Infobip account. Find this on your Infobip dashboard.APP_BASE_URL: The root URL where your application can be reached by Infobip's servers to send callbacks. During local development, this will be an ngrok tunnel URL. In production, it's your server's public domain/IP.
1.6 Load Environment Variables: Integrate the @nestjs/config module into your
main application module (src/app.module.ts).
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Project Structure Explanation:
- src/: Contains your application code (modules, controllers, services).
- prisma/: Contains your database schema (schema.prisma) and migrations.
- .env: Stores environment-specific configuration and secrets.
- node\_modules/: Project dependencies.
- package.json: Project metadata and dependencies list.
- tsconfig.json: TypeScript compiler options.
We use @nestjs/config to load variables from .env, making them accessible via
ConfigService throughout the application, avoiding hardcoding secrets.
2. Creating a Database Schema and Data Layer
We need a way to store information about the SMS messages we send and their delivery status.
2.1 Define Prisma Schema: Open prisma/schema.prisma and define a model to
represent an SMS message.
Prisma
// prisma/schema.prisma
generator client {
provider = ""prisma-client-js""
}
datasource db {
provider = ""sqlite"" // Or ""postgresql"", ""mysql""
url = env(""DATABASE_URL"")
}
// Enum for delivery status based on Infobip's common statuses
enum MessageStatus {
PENDING // Initial state before sending or confirmation
SENT // Successfully sent to Infobip API
DELIVERED // Confirmed delivery to handset
UNDELIVERABLE // Failed delivery after retries
EXPIRED // Message validity period expired
REJECTED // Rejected by carrier or Infobip
UNKNOWN // Status could not be determined
}
model SmsMessage {
id String @id @default(uuid()) // Our internal unique ID
recipient String // Phone number SMS was sent to
senderId String? // 'from' field used (e.g., 'InfoSMS')
text String // Message content
infobipMessageId String? @unique // ID from Infobip API response (can be null initially)
infobipBulkId String? // Bulk ID if sent as part of a batch
status MessageStatus @default(PENDING) // Current delivery status
callbackData String @unique // Unique data sent to Infobip to correlate callbacks
sentAt DateTime @default(now()) // When we initiated the send
statusUpdatedAt DateTime @updatedAt // When the status was last updated
@@index([status])
@@index([recipient])
}
id: Our system's unique identifier (UUID).recipient: The destination phone number.senderId: The 'From' name/number used.text: The message body.infobipMessageId: The unique ID returned by Infobip for tracking a specific message to a recipient. Made unique to prevent accidental duplicates if callbacks somehow repeat.infobipBulkId: ID returned by Infobip when sending multiple messages in one request.status: Tracks the delivery lifecycle using our MessageStatus enum. Defaults to PENDING.callbackData: A crucial field. We'll generate a unique value (like our internal id or another UUID) and send it to Infobip. Infobip will include this callbackData in the delivery report POST request back to us, allowing easy correlation. Made unique for reliable lookup.sentAt: Timestamp of when our system processed the send request.statusUpdatedAt: Timestamp automatically updated by Prisma whenever the record changes.- Indices: Added for faster lookups on status, recipient, and the unique callbackData and infobipMessageId.
2.2 Create and Apply Migration: Generate the SQL migration file and apply it to your database.
npx prisma migrate dev --name init-sms-message
This creates the SmsMessage table in your dev.db SQLite file (or your configured
database).
2.3 Create Prisma Service: Create a reusable Prisma service according to NestJS best practices.
nest g module prisma --flat
nest g service prisma --flat
Modify src/prisma.service.ts:
import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
async enableShutdownHooks(app: INestApplication) {
process.on('beforeExit', async () => {
await app.close();
await this.$disconnect();
});
}
}
Make PrismaService available globally by exporting it from PrismaModule:
import { Module, Global } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
Finally, import PrismaModule into AppModule:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './prisma/prisma.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
PrismaModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Now PrismaService can be injected into any service that needs database access.
3. Implementing Core Functionality: Sending SMS
Let's create a dedicated module and service for handling SMS logic.
3.1 Create SMS Module and Service:
nest g module sms
nest g service sms/services/sms --no-spec
nest g controller sms/controllers/sms --no-spec
3.2 Configure Infobip Client: We'll instantiate the Infobip client within the
SmsService.
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Infobip, AuthType } from '@infobip-api/sdk';
import { PrismaService } from '../../prisma/prisma.service';
import { MessageStatus, Prisma } from '@prisma/client';
import { randomUUID } from 'crypto';
import { InfobipCallbackDto, InfobipResultDto } from '../dto/infobip-report.dto';
@Injectable()
export class SmsService implements OnModuleInit {
private readonly logger = new Logger(SmsService.name);
private infobipClient: Infobip;
private appBaseUrl: string;
constructor(
private configService: ConfigService,
private prisma: PrismaService,
) {}
onModuleInit() {
const apiKey = this.configService.get<string>('INFOBIP_API_KEY');
const baseUrl = this.configService.get<string>('INFOBIP_BASE_URL');
this.appBaseUrl = this.configService.get<string>('APP_BASE_URL');
if (!apiKey || !baseUrl || !this.appBaseUrl) {
this.logger.error(
'Infobip API Key, Base URL, or App Base URL missing in configuration.',
);
throw new Error('Missing Infobip or App configuration.');
}
this.infobipClient = new Infobip({
baseUrl: baseUrl,
apiKey: apiKey,
authType: AuthType.ApiKey,
});
this.logger.log('Infobip client initialized.');
}
async sendSingleSms(recipient: string, text: string, sender = 'InfoSMS') {
this.logger.log(`Attempting to send SMS to ${recipient}`);
const uniqueCallbackData = randomUUID();
let smsRecord;
try {
smsRecord = await this.prisma.smsMessage.create({
data: {
recipient: recipient,
text: text,
senderId: sender,
status: MessageStatus.PENDING,
callbackData: uniqueCallbackData,
},
});
this.logger.log(`Created initial SMS record ${smsRecord.id}`);
} catch (error) {
this.logger.error(
`Failed to create initial SMS record for ${recipient}: ${error.message}`,
error.stack,
);
throw new Error('Database error while preparing SMS.');
}
const payload = {
messages: [
{
destinations: [{ to: recipient }],
from: sender,
text: text,
notifyUrl: `${this.appBaseUrl}/sms/callback/infobip`,
notifyContentType: 'application/json',
callbackData: uniqueCallbackData,
},
],
};
try {
const infobipResponse =
await this.infobipClient.channels.sms.send(payload);
const messageResponse = infobipResponse.data.messages[0];
this.logger.log(
`Infobip response received for ${recipient}: Status ${messageResponse.status.groupName}, Message ID ${messageResponse.messageId}`,
);
const updatedRecord = await this.prisma.smsMessage.update({
where: { id: smsRecord.id },
data: {
infobipMessageId: messageResponse.messageId,
infobipBulkId: infobipResponse.data.bulkId,
status: this.mapInfobipStatus(messageResponse.status.groupName),
},
});
this.logger.log(
`Updated SMS record ${updatedRecord.id} with Infobip Message ID ${updatedRecord.infobipMessageId}`,
);
return {
internalMessageId: updatedRecord.id,
infobipMessageId: updatedRecord.infobipMessageId,
infobipStatus: messageResponse.status.groupName,
};
} catch (error) {
this.logger.error(
`Failed to send SMS via Infobip for record ${smsRecord.id}: ${error.message}`,
error.response?.data || error.stack,
);
await this.prisma.smsMessage.update({
where: { id: smsRecord.id },
data: {
status: MessageStatus.REJECTED,
},
});
throw new Error(`Infobip API Error: ${error.message}`);
}
}
private mapInfobipStatus(infobipStatusGroup: string): MessageStatus {
switch (infobipStatusGroup?.toUpperCase()) {
case 'PENDING':
return MessageStatus.PENDING;
case 'SENT':
return MessageStatus.SENT;
case 'DELIVERED':
return MessageStatus.DELIVERED;
case 'UNDELIVERABLE':
return MessageStatus.UNDELIVERABLE;
case 'EXPIRED':
return MessageStatus.EXPIRED;
case 'REJECTED':
return MessageStatus.REJECTED;
default:
this.logger.warn(`Unknown Infobip status group: ${infobipStatusGroup}`);
return MessageStatus.UNKNOWN;
}
}
async handleDeliveryReport(reportDto: InfobipCallbackDto) {
this.logger.log(`Processing ${reportDto.results.length} delivery report(s) from Infobip.`);
for (const result of reportDto.results) {
if (!result.callbackData) {
this.logger.warn(`Received delivery report without callbackData for messageId ${result.messageId}. Cannot correlate reliably.`);
continue;
}
try {
const message = await this.prisma.smsMessage.findUnique({
where: { callbackData: result.callbackData },
});
if (!message) {
this.logger.warn(`Received delivery report with callbackData ${result.callbackData}, but no matching message found in DB. MessageId: ${result.messageId}. Maybe already processed or test data?`);
continue;
}
const newStatus = this.mapInfobipStatus(result.status.groupName);
const isFinalStatus = [
MessageStatus.DELIVERED,
MessageStatus.UNDELIVERABLE,
MessageStatus.EXPIRED,
MessageStatus.REJECTED,
].includes(message.status);
if (isFinalStatus && message.status === newStatus) {
this.logger.log(`Message ${message.id} (CallbackData: ${result.callbackData}) already has final status ${message.status}. Ignoring duplicate report.`);
continue;
}
if (!isFinalStatus || message.status !== newStatus) {
this.logger.log(
`Updating message ${message.id} (CallbackData: ${result.callbackData}) from ${message.status} to ${newStatus}. Infobip Status: ${result.status.groupName} (${result.status.name})`,
);
await this.prisma.smsMessage.update({
where: { id: message.id },
data: {
status: newStatus,
},
});
this.logger.log(`Successfully updated message ${message.id}.`);
}
} catch (error) {
this.logger.error(
`Error processing delivery report for callbackData ${result.callbackData} (MessageId: ${result.messageId}): ${error.message}`,
error.stack,
);
}
}
}
}
Explanation:
- Initialization: In onModuleInit, we fetch Infobip credentials and the app's base URL from ConfigService and initialize the Infobip client. Error handling ensures configuration is present.
sendSingleSmsMethod:
- Generates a unique callbackData using randomUUID().
- Creates a record in the SmsMessage table with status: PENDING and the unique callbackData before calling Infobip. This ensures we have a record to update even if the Infobip call fails or the callback is delayed.
- Constructs the Infobip payload, critically including:
- notifyUrl: The absolute URL of our callback endpoint (Section 4). Infobip will POST delivery reports here.
- notifyContentType: Set to application/json.
- callbackData: The unique ID we generated.
- Calls infobipClient.channels.sms.send().
- On success, updates the corresponding database record with the infobipMessageId, infobipBulkId, and the initial status returned by Infobip (mapped via mapInfobipStatus).
- On failure, logs the error (including Infobip's response data if available) and updates the database record status to REJECTED or similar.
- Returns our internal ID and Infobip's ID for reference.
mapInfobipStatus: A helper to translate Infobip's status group names (like ""PENDING"", ""DELIVERED"") into our MessageStatus enum values.handleDeliveryReport: (Implementation detailed in Section 5) Processes incoming delivery reports from Infobip, finds the corresponding message using callbackData, checks for idempotency, and updates the message status in the database.
Register Service and Controller: Update src/sms/sms.module.ts:
import { Module } from '@nestjs/common';
import { SmsService } from './services/sms.service';
import { SmsController } from './controllers/sms.controller';
@Module({
controllers: [SmsController],
providers: [SmsService],
exports: [SmsService],
})
export class SmsModule {}
Import SmsModule into src/app.module.ts:
import { SmsModule } from './sms/sms.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
PrismaModule,
SmsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
4. Implementing the API Layer (Sending SMS)
Now, let's expose an endpoint to trigger the sendSingleSms service method.
4.1 Create Request DTO: Define a Data Transfer Object (DTO) for the request body with validation.
import { IsNotEmpty, IsString, IsPhoneNumber, MaxLength, IsOptional } from 'class-validator';
export class SendSmsDto {
@IsNotEmpty()
@IsPhoneNumber(null)
@IsString()
recipient: string;
@IsNotEmpty()
@IsString()
@MaxLength(160)
text: string;
@IsString()
@MaxLength(11)
@IsOptional()
sender?: string;
}
4.2 Implement Controller Endpoint: Add a POST endpoint to
src/sms/controllers/sms.controller.ts.
import { Controller, Post, Body, ValidationPipe, UsePipes, Logger, HttpCode, HttpStatus } from '@nestjs/common';
import { SmsService } from '../services/sms.service';
import { SendSmsDto } from '../dto/send-sms.dto';
import { InfobipCallbackDto } from '../dto/infobip-report.dto';
@Controller('sms')
export class SmsController {
private readonly logger = new Logger(SmsController.name);
constructor(private readonly smsService: SmsService) {}
@Post('send')
@UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
@HttpCode(HttpStatus.ACCEPTED)
async sendSms(@Body() sendSmsDto: SendSmsDto) {
this.logger.log(`Received request to send SMS to ${sendSmsDto.recipient}`);
try {
const result = await this.smsService.sendSingleSms(
sendSmsDto.recipient,
sendSmsDto.text,
sendSmsDto.sender,
);
this.logger.log(`SMS queued for sending with internal ID: ${result.internalMessageId}`);
return {
message: 'SMS submitted successfully.',
internalMessageId: result.internalMessageId,
infobipMessageId: result.infobipMessageId,
};
} catch (error) {
this.logger.error(`Error in /sms/send endpoint: ${error.message}`, error.stack);
throw error;
}
}
@Post('callback/infobip')
@HttpCode(HttpStatus.NO_CONTENT)
@UsePipes(new ValidationPipe({ validateCustomDecorators: true, transform: true, whitelist: true }))
async handleInfobipCallback(@Body() reportDto: InfobipCallbackDto) {
this.logger.log(`Received Infobip callback request.`);
await this.smsService.handleDeliveryReport(reportDto);
}
}
Explanation:
@Controller('sms'): Defines the base route /sms for this controller.@Post('send'): Defines the sub-route POST /sms/send.@UsePipes(new ValidationPipe(...)): (If not global) Automatically validates the incoming request body against the SendSmsDto.- whitelist: true: Strips properties not defined in the DTO.
- forbidNonWhitelisted: true: Throws an error if extra properties are present.
@Body() sendSmsDto: SendSmsDto: Injects the validated request body into the sendSmsDto parameter.@HttpCode(HttpStatus.ACCEPTED): Sets the default success response code to 202 for /send, indicating the request was accepted for processing but completion is asynchronous.- Calls smsService.sendSingleSms with data from the DTO.
- Returns a success response including the internal ID and the ID from Infobip.
@Post('callback/infobip'): Defines the sub-route POST /sms/callback/infobip for receiving delivery reports.@HttpCode(HttpStatus.NO_CONTENT): Sets the response code to 204 for /callback/infobip. This is standard practice for webhook acknowledgements β it tells Infobip we received it successfully without needing to send back a body.@UsePipes(...)on Callback: Validates the incoming callback payload against InfobipCallbackDto.@Body() reportDto: InfobipCallbackDto: Injects the validated callback payload.- Delegates processing to smsService.handleDeliveryReport.
4.3 Enable Validation Globally (Optional but Recommended): Instead of @UsePipes
on every handler, enable it globally in src/main.ts.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe, Logger } from '@nestjs/common';
import { PrismaService } from './prisma/prisma.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = new Logger('Bootstrap');
app.enableCors();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
}),
);
const prismaService = app.get(PrismaService);
await prismaService.enableShutdownHooks(app);
const port = process.env.PORT || 3000;
await app.listen(port);
logger.log(`Application listening on port ${port}`);
logger.log(`API Base URL: ${process.env.APP_BASE_URL || `http://localhost:${port}`}`);
logger.log(`Infobip Callback URL: ${process.env.APP_BASE_URL || `http://localhost:${port}`}/sms/callback/infobip`);
}
bootstrap();
Testing the Sending Endpoint:
Use curl or Postman to send a POST request:
curl -X POST http://localhost:3000/sms/send \
-H ""Content-Type: application/json"" \
-d '{
""recipient"": ""+12345678900"",
""text"": ""Hello from NestJS and Infobip!"",
""sender"": ""MyApp""
}'
Check your database (dev.db) - you should see a new record in the SmsMessage table
with status: PENDING (or SENT/REJECTED depending on immediate API response) and the correct
infobipMessageId and callbackData.
5. Handling Infobip Delivery Callbacks
This is the core of tracking delivery status. Infobip will send a POST request to the notifyUrl
we provided (/sms/callback/infobip).
5.1 Define Callback Payload DTO:
Important: The following DTO structure is an example. Always verify against the latest official Infobip API documentation for delivery report callbacks, as formats can change.
import { Type } from 'class-transformer';
import { IsArray, IsDate, IsEnum, IsNotEmpty, IsNumber, IsObject, IsOptional, IsString, ValidateNested, IsBoolean } from 'class-validator';
class InfobipPriceDto {
@IsNumber()
pricePerMessage: number;
@IsString()
currency: string;
}
class InfobipStatusDto {
@IsNumber()
groupId: number;
@IsString()
groupName: string;
@IsNumber()
id: number;
@IsString()
name: string;
@IsString()
description: string;
}
class InfobipErrorDto {
@IsNumber()
groupId: number;
@IsString()
groupName: string;
@IsNumber()
id: number;
@IsString()
name: string;
@IsString()
description: string;
@IsBoolean()
permanent: boolean;
}
class InfobipResultDto {
@IsString()
bulkId: string;
@IsString()
messageId: string;
@IsString()
to: string;
@IsDate()
@Type(() => Date)
sentAt: Date;
@IsDate()
@Type(() => Date)
doneAt: Date;
@IsNumber()
smsCount: number;
@IsOptional()
@ValidateNested()
@Type(() => InfobipPriceDto)
price?: InfobipPriceDto;
@ValidateNested()
@Type(() => InfobipStatusDto)
status: InfobipStatusDto;
@IsOptional()
@ValidateNested()
@Type(() => InfobipErrorDto)
error?: InfobipErrorDto;
@IsString()
@IsOptional()
callbackData?: string;
}
export class InfobipCallbackDto {
@IsArray()
@ValidateNested({ each: true })
@Type(() => InfobipResultDto)
results: InfobipResultDto[];
}
*Note: Refer to the latest Infobip documentation for the exact callback payload structure, as it can vary. This is a common example structure.*
5.2 Implement Callback Handler in Service: The implementation for
handleDeliveryReport was added to src/sms/services/sms.service.ts in Section 3.2. It
performs the following steps for each result in the callback:
- Checks for the presence of callbackData.
- Finds the corresponding SmsMessage in the database using callbackData.
- Performs an idempotency check: If the message is already in a final state (DELIVERED, UNDELIVERABLE, etc.) and the incoming status is the same, it logs and skips the update to prevent redundant processing.
- If the status is new or the message wasn't in a final state, it maps the Infobip status to the internal MessageStatus enum.
- Updates the SmsMessage record in the database with the new status.
- Logs errors encountered during processing.
Testing the Callback:
- Start
ngrok: Expose your local NestJS port (e.g., 3000) to the internet.
ngrok http 3000
- Copy
ngrokURL: Note the https:// forwarding URL provided by ngrok (e.g., https://<unique-subdomain>.ngrok.io). - Update
.env: Set APP\_BASE\_URL in your .env file to this ngrok URL.
.env
APP_BASE_URL=""https://<unique-subdomain>.ngrok.io""
- Restart NestJS: Ensure your application picks up the new APP\_BASE\_URL.
- Send an SMS: Use the POST /sms/send endpoint again. The notifyUrl sent to Infobip will now point to your ngrok tunnel.
- Monitor Logs: Watch your NestJS application logs and the ngrok console (http://localhost:4040). You should see Infobip making a POST request to /sms/callback/infobip shortly after the message reaches a final status (like DELIVERED or UNDELIVERABLE).
- Check Database: Verify that the status field for the corresponding SmsMessage record has been updated based on the callback.
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 β