Developer Quick-Start: A guide to building a secure webhook endpoint using Node.js and Fastify to receive, validate, and store Infobip SMS delivery reports. 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.
Track the status of your sent SMS messages in real-time by setting up a webhook endpoint using Fastify to receive delivery reports directly from Infobip. This guide provides a step-by-step walkthrough for building a webhook handler foundation in Node.js.
We'll cover everything from initial project setup and core logic implementation to security considerations, database integration, error handling, deployment, and testing.
Project Overview and Goals
This guide details how to build a reliable webhook endpoint using Node.js and the Fastify framework to receive and process SMS delivery status updates sent by Infobip.
Problem Solved: When you send SMS messages via an API like Infobip's, you often need confirmation of whether the message was successfully delivered, failed, or is still pending. Relying solely on the initial API response isn't enough for final status tracking. Infobip provides delivery report webhooks, pushing status updates to an endpoint you specify. This project builds that endpoint.
Key Goals:
- Create a Fastify application to listen for incoming HTTP POST requests from Infobip.
- Securely validate incoming webhook requests to ensure they originate from Infobip.
- Parse the delivery report payload sent by Infobip.
- Store the relevant status information (e.g., message ID, status, timestamp) in a database.
- Implement robust logging and error handling.
- Provide guidance on deployment and testing.
Technologies Used:
- Node.js: The JavaScript runtime environment.
- Fastify: A high-performance, low-overhead web framework for Node.js, chosen for its speed, extensive plugin ecosystem, and developer experience.
- Infobip API: Used for sending SMS messages (assumed) and configured to send delivery reports via webhooks.
- Prisma: A modern database toolkit for Node.js (used here for database interaction examples).
@prisma/client: The runtime client for Prisma.dotenv: For managing environment variables.pino: Fastify's default high-performance JSON logger.
Prerequisites:
- A working Node.js environment (LTS version recommended).
- npm or yarn package manager.
- An active Infobip account with API access. You should know how to send SMS messages via their API.
- Access to a database (PostgreSQL is used in examples, but adaptable).
- A way to expose your local development server to the internet (e.g., ngrok) for testing webhooks, or a deployed environment.
- Basic understanding of JavaScript, Node.js, REST APIs, and webhooks.
Final Outcome: A functional Node.js application capable of securely receiving, validating, processing, and storing Infobip SMS delivery reports, providing a solid foundation for production deployment.
1. Setting up the project
Let's initialize our Node.js project and install the necessary dependencies.
- Create Project Directory: Open your terminal and create a new directory for the project, then navigate into it.
mkdir infobip-webhook-handler
cd infobip-webhook-handler
- Initialize npm Project: Initialize the project using npm. You can accept the defaults or customize them.
npm init -y
This creates a package.json file.
- Install Dependencies: We need Fastify, dotenv for environment variables, @fastify/sensible for utilities, the Prisma client for database interaction, and Prisma CLI/pino-pretty for development.
npm install fastify dotenv @fastify/sensible @prisma/client
npm install --save-dev pino-pretty prisma nodemon
- fastify: The core web framework.
- dotenv: Loads environment variables from a .env file.
- @fastify/sensible: Adds useful decorators and error handling utilities.
- @prisma/client: The runtime Prisma client library.
- pino-pretty: Formats Pino logs during development (dev dependency).
- prisma: The Prisma CLI for migrations and generation (dev dependency).
- nodemon: Utility to auto-restart the server during development (dev dependency).
- Configure
package.jsonScripts: Add scripts to yourpackage.jsonfor running the application:
{
""scripts"": {
""start"": ""node src/server.js"",
""dev"": ""nodemon --watch src --exec \""node src/server.js | pino-pretty\"""",
""db:migrate"": ""prisma migrate dev"",
""db:generate"": ""prisma generate""
},
}
- start: Runs the application directly with Node.
- dev: Runs the application using nodemon for auto-restarts on file changes and pipes logs through pino-pretty.
- db:migrate: Applies database migrations using Prisma during development.
- db:generate: Generates the Prisma client based on your schema.
- Create Project Structure: Organize your project files:
infobip-webhook-handler/
βββ prisma/
β βββ schema.prisma
βββ src/
β βββ routes/
β β βββ webhooks.js
β βββ plugins/
β β βββ prisma.js
β βββ server.js
β βββ app.js
βββ .env
βββ .gitignore
βββ package.json
- Create
.gitignore: Add common Node.js ignores:
# .gitignore
node_modules
.env
dist
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Optional: Store only migration definition, not generated SQL
# /prisma/migrations/*/*.sql
- Create
.envFile: This file will hold your environment variables. Do not commit this file to version control.
.env
# .env
PORT=3000
HOST=0.0.0.0
LOG_LEVEL=info
NODE_ENV=development
# Infobip Configuration
INFOBIP_WEBHOOK_SECRET=your_strong_shared_secret_here # Obtain from Infobip or define yourself
# Database Configuration (Example for PostgreSQL)
DATABASE_URL=""postgresql://user:password@localhost:5432/infobip_webhooks?schema=public""
- INFOBIP\_WEBHOOK\_SECRET: A secret string shared between your application and Infobip, used for verifying webhook signatures. You must define this and configure it in your Infobip webhook settings.
- DATABASE\_URL: Connection string for your database. Adjust accordingly.
- Initialize Prisma: Set up Prisma in your project.
npx prisma init
This creates the prisma/schema.prisma file and updates .env with a placeholder
DATABASE_URL if it wasn't already present. Configure prisma/schema.prisma as shown
in the next section.
2. Creating a database schema and data layer (Prisma)
We need a way to store the delivery status updates. We'll use Prisma for this.
- Define Prisma Schema: Open
prisma/schema.prismaand define the data source, generator, and a model to store status updates.
Prisma
// prisma/schema.prisma
generator client {
provider = ""prisma-client-js""
}
datasource db {
provider = ""postgresql"" // Or your chosen database: mysql, sqlite, sqlserver, mongodb
url = env(""DATABASE_URL"")
}
model MessageStatus {
id String @id @default(cuid()) // Unique DB identifier
messageId String @unique // Infobip's message ID
status String // e.g., DELIVERED_TO_HANDSET, UNDELIVERABLE
groupName String // e.g., DELIVERED, UNDELIVERABLE, PENDING
description String? // Detailed status description
errorCode Int? // Error code if applicable
receivedAt DateTime @default(now()) // Timestamp when webhook was received
updatedAt DateTime @updatedAt // Timestamp of last update to this record
@@index([messageId])
@@index([status])
@@index([receivedAt])
}
- This schema defines a MessageStatus table.
- messageId is marked unique as we typically want the latest status for a given message.
- Adjust fields based on the actual data you need from the Infobip payload. Consult Infobip's documentation for the exact structure of the delivery report.
- Run Initial Migration: Create the database table based on the schema. Make sure your
database server is running and accessible using the
DATABASE_URLin.env.
npx prisma migrate dev --name init
This command:
- Creates the SQL migration file in prisma/migrations/.
- Applies the migration to your database.
- Generates the Prisma Client based on your schema (node\_modules/.prisma/client).
- Create Prisma Plugin for Fastify: Create a Fastify plugin to instantiate and provide the Prisma client to your routes.
'use strict'
const fp = require('fastify-plugin')
const { PrismaClient } = require('@prisma/client')
async function prismaPlugin (fastify, options) {
const prisma = new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'info', 'warn', 'error'] : ['warn', 'error']
})
await prisma.$connect()
fastify.log.info('Prisma client connected.')
fastify.decorate('prisma', prisma)
fastify.addHook('onClose', async (instance) => {
instance.log.info('Disconnecting Prisma client...')
await instance.prisma.$disconnect()
instance.log.info('Prisma client disconnected.')
})
}
module.exports = fp(prismaPlugin)
- This plugin creates a PrismaClient instance.
- It connects the client when the Fastify server starts.
- It decorates the Fastify instance with fastify.prisma, making the client available in request handlers.
- It disconnects the client gracefully when the server shuts down using the onClose hook.
3. Implementing core functionality & API layer
Now, let's build the main application logic and the webhook endpoint.
- Configure Fastify Application (
src/app.js): Set up the core Fastify application, register plugins, and routes.
'use strict'
require('dotenv').config()
const path = require('node:path')
const Fastify = require('fastify')
const sensible = require('@fastify/sensible')
const prismaPlugin = require('./plugins/prisma')
const webhookRoutes = require('./routes/webhooks')
async function build (opts = {}) {
const app = Fastify({
logger: {
level: process.env.LOG_LEVEL || 'info',
},
ajv: {
customOptions: {
removeAdditional: false,
coerceTypes: false,
allErrors: true
}
},
...opts
})
app.addContentTypeParser('application/json', { parseAs: 'buffer' }, function (req, body, done) {
try {
req.rawBody = body;
const json = JSON.parse(body.toString('utf8'));
done(null, json);
} catch (err) {
err.statusCode = 400;
done(err, undefined);
}
});
app.register(sensible)
app.register(prismaPlugin)
app.register(webhookRoutes, { prefix: '/webhooks' })
app.get('/health', async (request, reply) => {
try {
await app.prisma.$queryRaw`SELECT 1`
return { status: 'ok', timestamp: new Date().toISOString(), db: 'connected' }
} catch (dbError) {
request.log.error({ err: dbError }, 'Health check failed - DB connection error')
reply.code(503)
return { status: 'error', timestamp: new Date().toISOString(), db: 'disconnected' }
}
})
return app
}
module.exports = { build }
- Loads environment variables using dotenv.
- Initializes Fastify with logging options.
- Crucially, adds a custom content type parser for application/json. This parser does two things:
- Stores the original raw buffer of the request body onto req.rawBody. This is essential for verifying the webhook signature later, as the signature is calculated based on the raw, unparsed body.
- Parses the incoming JSON payload so your handler can access it easily (done(null, json)).
- Registers the @fastify/sensible and our prismaPlugin.
- Registers the webhook routes under the /webhooks prefix.
- Includes a basic /health check endpoint.
- Create Server Entry Point (
src/server.js): This file builds and starts the Fastify server.
'use strict'
require('dotenv').config()
const { build } = require('./app')
const start = async () => {
const host = process.env.HOST || '127.0.0.1'
const port = parseInt(process.env.PORT || '3000', 10)
let server
try {
server = await build()
await server.listen({ port: port, host: host })
const signals = ['SIGINT', 'SIGTERM']
signals.forEach(signal => {
process.on(signal, async () => {
server.log.info(`Received ${signal}, shutting down gracefully...`)
await server.close()
process.exit(0)
})
})
} catch (err) {
if (server) {
server.log.error(err, 'Server startup error after build')
} else {
console.error('Server startup error before build:', err)
}
process.exit(1)
}
}
start()
- Builds the Fastify app using app.js.
- Starts the server listening on the configured host and port.
- Includes basic graceful shutdown handling for SIGINT and SIGTERM.
- Implement Webhook Route (
src/routes/webhooks.js): Define the endpoint that Infobip will call.
'use strict'
const crypto = require('node:crypto')
const deliveryReportSchema = {
body: {
type: 'object',
required: ['results'],
properties: {
results: {
type: 'array',
items: {
type: 'object',
required: ['messageId', 'status'],
properties: {
messageId: { type: 'string' },
to: { type: 'string' },
sentAt: { type: 'string', format: 'date-time' },
doneAt: { type: 'string', format: 'date-time' },
smsCount: { type: 'integer' },
price: {
type: 'object',
properties: {
pricePerMessage: { type: 'number' },
currency: { type: 'string' }
}
},
status: {
type: 'object',
required: ['groupId', 'groupName', 'id', 'name', 'description'],
properties: {
groupId: { type: 'integer' },
groupName: { type: 'string' },
id: { type: 'integer' },
name: { type: 'string' },
description: { type: 'string' }
}
},
error: {
type: 'object',
required: ['groupId', 'groupName', 'id', 'name', 'description', 'permanent'],
properties: {
groupId: { type: 'integer' },
groupName: { type: 'string' },
id: { type: 'integer' },
name: { type: 'string' },
description: { type: 'string' },
permanent: { type: 'boolean' }
}
}
}
}
}
}
}
}
async function webhookRoutes (fastify, options) {
fastify.addHook('preHandler', async (request, reply) => {
const secret = process.env.INFOBIP_WEBHOOK_SECRET
if (!secret) {
request.log.error('CRITICAL: INFOBIP_WEBHOOK_SECRET environment variable is not configured. Cannot verify webhook signature.');
throw fastify.httpErrors.internalServerError('Webhook security configuration is missing.');
}
const signatureHeader = request.headers['x-infobip-signature']
if (!signatureHeader) {
request.log.warn('Missing X-Infobip-Signature header from incoming request.');
throw fastify.httpErrors.unauthorized('Missing webhook signature header');
}
if (!request.rawBody) {
request.log.error('FATAL: Raw request body (request.rawBody) is missing. Check Fastify content type parser setup in app.js.');
throw fastify.httpErrors.internalServerError('Internal server configuration error processing request body.');
}
try {
const hmac = crypto.createHmac('sha256', secret);
const digest = Buffer.from(hmac.update(request.rawBody).digest('hex'), 'utf8');
const receivedSignature = Buffer.from(signatureHeader, 'utf8');
if (receivedSignature.length !== digest.length || !crypto.timingSafeEqual(digest, receivedSignature)) {
request.log.warn(`Invalid webhook signature received. Header: ${signatureHeader}`);
throw fastify.httpErrors.unauthorized('Invalid webhook signature');
}
request.log.info('Webhook signature verified successfully.');
} catch (error) {
if (error.statusCode === 401) {
throw error;
}
request.log.error({ err: error }, 'Error occurred during signature verification process.');
throw fastify.httpErrors.internalServerError('Signature verification failed due to an internal error.');
}
});
fastify.post('/infobip', { schema: deliveryReportSchema }, async (request, reply) => {
request.log.info({ payload: request.body }, 'Received verified Infobip delivery report');
const results = request.body.results;
const processingPromises = results.map(async (result) => {
const { messageId, status, error } = result;
if (!messageId) {
request.log.warn({ result }, 'Skipping result: Missing required field ""messageId""');
return { messageId: null, status: 'skipped', reason: 'Missing messageId' };
}
try {
const updatedStatus = await fastify.prisma.messageStatus.upsert({
where: { messageId: messageId },
update: {
status: status.name,
groupName: status.groupName,
description: status.description,
errorCode: error ? error.id : null,
},
create: {
messageId: messageId,
status: status.name,
groupName: status.groupName,
description: status.description,
errorCode: error ? error.id : null
}
});
request.log.info({ messageId: updatedStatus.messageId, status: updatedStatus.status }, 'Message status updated/created in DB.');
return { messageId, status: 'processed' };
} catch (dbError) {
request.log.error({ err: dbError, messageId }, 'Failed to update/create message status in DB');
return { messageId, status: 'failed', reason: dbError.message };
}
});
const outcomes = await Promise.allSettled(processingPromises);
request.log.info({ outcomes }, 'Finished processing batch of delivery reports.');
const failedOutcomes = outcomes.filter(o => o.status === 'rejected' || (o.status === 'fulfilled' && o.value.status === 'failed'));
if (failedOutcomes.length > 0) {
request.log.warn({ count: failedOutcomes.length, details: failedOutcomes }, 'Some delivery reports in the batch failed processing.');
}
reply.code(200).send({ received: true, processed: outcomes.length });
})
}
module.exports = webhookRoutes
- Schema: Defines the expected structure (deliveryReportSchema). This is explicitly marked as a placeholder. You must replace this with the actual schema based on Infobip's documentation.
- Signature Verification (
preHandler): - Runs before the main route handler.
- Checks for INFOBIP\_WEBHOOK\_SECRET.
- Gets the signature from headers (assuming X-Infobip-Signature β verify this header name).
- Checks for request.rawBody.
- Calculates HMAC signature (assuming sha256 β verify this algorithm) using the secret and request.rawBody.
- Compares signatures using crypto.timingSafeEqual.
- Throws appropriate HTTP errors (401 Unauthorized or 500 Internal Server Error) on failure.
- Main Handler (
fastify.post('/infobip', ...)): - Logs the received payload.
- Iterates through the results array.
- Uses fastify.prisma.messageStatus.upsert for idempotent database updates/creates.
- Handles database errors gracefully using Promise.allSettled.
- Sends a 200 OK response back to Infobip.
4. Integrating with Infobip
Your application is ready to receive webhooks, but you need to tell Infobip where to send them and provide the necessary secret.
- Obtain Public URL: Your webhook endpoint must be accessible from the public internet.
- Development: Use a tool like ngrok to expose your local server. Run ngrok http 3000 (if your app runs on port 3000). ngrok provides a temporary public HTTPS URL (e.g., https://<unique-subdomain>.ngrok.io). Note: Free ngrok URLs change each time you restart it, and have usage limitations; they are suitable only for temporary development testing.
- Production: Use the stable, public URL of your deployed application (e.g., https://your-app-domain.com). This URL must be HTTPS.
- Configure Infobip Webhook:
- Log in to your Infobip account/portal.
- Navigate to the section for configuring API settings, applications, or webhooks. Look for terms like ""Delivery Reports,"" ""Callbacks,"" or ""Webhooks."" (This might be under an ""Applications"" section or general ""API Settings"").
- Find the option to set a URL for receiving delivery reports (often called ""Delivery Report URL"" or similar).
- Enter the full public HTTPS URL of your endpoint: https://<your-public-url>/webhooks/infobip.
- Look for a field to configure webhook security or a ""Secret Key."" Enter the exact same secret string you defined in your .env file for INFOBIP\_WEBHOOK\_SECRET.
- Ensure the signature algorithm selected in Infobip (e.g., HMAC-SHA256) exactly matches the one used in your preHandler code (crypto.createHmac('sha256', ...)).
- Save the configuration in Infobip.
- API Key Security: While this guide focuses on receiving webhooks, remember that
your sending application needs an Infobip API Key. Store this key securely (e.g., environment
variable) and never commit it to version control. The
INFOBIP_WEBHOOK_SECRETis also highly sensitive and should be treated like a password.
5. Implementing proper error handling, logging, and retry mechanisms
- Error Handling:
- @fastify/sensible provides convenient HTTP error generation (e.g., fastify.httpErrors.unauthorized()).
- The signature verification preHandler catches authentication/configuration errors early.
- The main handler uses try...catch within the mapping loop (via Promise.allSettled) to handle potential database errors for individual status updates without crashing the entire request.
- Fastify's default error handler catches unhandled exceptions and sends a generic 500 response. Customize using fastify.setErrorHandler() if needed.
- Logging:
- Fastify uses pino for efficient JSON logging. Configured in src/app.js. Set LOG\_LEVEL via .env.
- Log key events: receiving webhook, signature verification success/failure, database update success/failure, batch processing completion, configuration errors.
- In development, pipe to pino-pretty (package.json's dev script) for readability. In production, ingest structured JSON logs into a log management system (e.g., Datadog, ELK stack, Splunk, Loki).
- Retry Mechanisms:
- Infobip Retries: If your endpoint fails to respond with 200 OK (e.g., downtime, crash, signature failure causing 4xx/5xx), Infobip may retry sending the webhook. Check their documentation for specific retry policies (timing, number of attempts).
- Application Idempotency: Because Infobip might retry, your endpoint must be idempotent. Processing the same webhook multiple times should yield the same final state. Using prisma.upsert based on the unique messageId achieves idempotency at the database level. The signature check prevents processing unauthorized duplicates.
- Internal Retries: For transient database errors within your handler, you could implement limited retry logic (e.g., using async-retry). However, often it's better to log the error clearly and rely on idempotency + Infobip's retries, or push failed updates to a dead-letter queue for separate investigation/processing. Ensure your endpoint still returns 200 OK quickly even if some internal processing fails, to prevent unnecessary Infobip retries.
6. Adding security features
- Webhook Signature Verification: Mandatory. Implemented in the preHandler hook using HMAC (algorithm verified against Infobip docs) and a strong, unique shared secret. Ensures authenticity and integrity.
- Input Validation: Fastify's schema validation (deliveryReportSchema) provides basic structure validation. Crucially, ensure this schema matches Infobip's actual payload. Set removeAdditional: false cautiously; for stricter security, define a precise schema and consider setting removeAdditional: true or explicitly handle unexpected properties.
- HTTPS: Mandatory. Always use HTTPS for the webhook endpoint URL configured in Infobip and exposed by your server. ngrok provides this for development. In production, ensure your load balancer or server terminates TLS/SSL.
- Rate Limiting: Protect against DoS or abuse. Use @fastify/rate-limit.
npm install @fastify/rate-limit
const rateLimit = require('@fastify/rate-limit')
async function build (opts = {}) {
const app = Fastify({ })
await app.register(rateLimit, {
max: 100,
timeWindow: '1 minute',
})
return app
}
- Secrets Management: Store INFOBIP\_WEBHOOK\_SECRET and DATABASE\_URL securely using environment variables. Never hardcode or commit them. Use a secrets management system in production (e.g., HashiCorp Vault, AWS Secrets Manager, Doppler, platform-specific secrets).
- Least Privilege (Database): The database user specified in DATABASE\_URL should have only the minimum required permissions (e.g., SELECT, INSERT, UPDATE on the MessageStatus table) and not broad administrative rights.
- Disable Introspection Endpoints: In production, ensure no debugging or introspection endpoints (like GraphQL playground if used elsewhere) are accidentally exposed publicly.
7. Handling special cases relevant to the domain
- Payload Variations: Infobip might send different fields or structures for different statuses (e.g., errors vs. success). Your schema and parsing logic must be robust. Constantly refer to Infobip documentation.
- Duplicate Webhooks (Idempotency): Handled by upsert and the unique constraint on messageId. Signature verification prevents processing unauthorized duplicates.
- Out-of-Order Webhooks: It's possible to receive status updates out of sequence (e.g., DELIVERED before ACCEPTED). The upsert logic inherently handles this by always storing the latest received state for a given messageId. If sequence matters critically, you might need to compare timestamps (doneAt from Infobip or receivedAt from your system).
- Large Payloads / Batching: The example code handles the results array. If batches become extremely large, consider performance implications. Ensure database transactions or operations can handle the batch size efficiently.
- Time Zones: Be mindful of time zones when storing and comparing timestamps (sentAt, doneAt, receivedAt). Store timestamps in UTC (DateTime in Prisma typically maps to timestamp with time zone in PostgreSQL) and perform conversions as needed for display or comparison.
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 β