Frequently Asked Questions
Set up a webhook endpoint in your NestJS application to receive DLRs. Configure your Vonage account to send POST requests to this endpoint. Use ngrok for local development to create a publicly accessible URL for your webhook.
DLRs are real-time status updates from Vonage about the delivery status of your SMS messages. They provide information about whether a message was delivered, failed, or expired, enabling better communication and troubleshooting.
NestJS provides a structured, scalable, and maintainable environment for building server-side applications. Its modular architecture, dependency injection, and TypeScript support make it ideal for handling webhook integrations.
Use ngrok during local development to expose your local server to the internet, as Vonage requires a publicly accessible URL for webhooks. For production, deploy to a server and use a permanent URL.
Create a DTO (Data Transfer Object) representing the DLR payload structure. Use class-validator and class-transformer to validate and transform incoming webhook data. Enable a global ValidationPipe in your NestJS application.
The Vonage SMS API is used for sending SMS messages and providing delivery receipt webhooks. You'll typically use the @vonage/server-sdk within your NestJS application to send the initial messages.
Create a .env file in your project's root directory. Store your VONAGE_API_KEY, VONAGE_API_SECRET, VONAGE_NUMBER, and other configurations in this file. Use @nestjs/config module to load these variables into your application.
These libraries help validate incoming webhook data against a defined DTO (Data Transfer Object) and transform the data into the DTO's format, improving type safety and preventing errors.
Logging helps track incoming requests, monitor the status of message deliveries, identify potential issues, and debug errors. Use different log levels for varying degrees of detail.
While standard DLRs lack cryptographic signature verification, you can use techniques like IP whitelisting (if available from Vonage) and secrets in the URL path to improve security, but be aware of their limitations. Use HTTPS in production and consider rate limiting.
Use unit tests to test your service logic, integration tests for combined controller-service behavior, and end-to-end tests to verify the full flow using ngrok, a real phone number, and actual SMS messages.
Use HTTPS, manage environment variables securely, update the webhook URL in Vonage settings to the permanent production URL, and implement monitoring, alerting, and scaling strategies.
Use a try-catch block to handle errors. Log all errors and return a 200 OK to Vonage to acknowledge receipt even on processing failure, unless you specifically want Vonage to retry (in which case, throw an HttpException). Consider using background job queues for heavy processing tasks.
Log the failure details, including the error code. Implement specific error handling based on your application needs, such as notifying support, attempting retries, or marking the message as permanently failed.
Use HTTPS, validate incoming DLRs with DTOs, implement IP whitelisting and rate limiting. For stronger security, consider using Vonage Messages API which supports JWT verification for webhooks, if your use-case allows it.
Track SMS Delivery Status with Vonage and NestJS
Learn how to implement Vonage SMS delivery receipts (DLR) in your NestJS application and track message delivery status in real-time. This comprehensive guide walks you through building a production-ready webhook endpoint to receive delivery status updates from Vonage's SMS API.
Knowing whether your SMS messages are delivered is essential for customer communication, troubleshooting failed deliveries, and tracking campaign performance. This tutorial demonstrates how to receive and process Vonage delivery receipts using NestJS webhooks, giving you real-time visibility into message delivery status.
Technologies Used:
@vonage/server-sdk: The official Vonage Node.js SDK (current version: v3.24.1). While this guide focuses on receiving DLRs (which doesn't directly use the SDK), use this SDK within your NestJS application to send the initial SMS messages that generate these DLRs.[3]@nestjs/config: For managing environment variables.class-validator&class-transformer: For validating incoming webhook data.System Architecture:
Understanding Vonage Delivery Receipt Status Codes
Vonage sends different delivery receipt statuses depending on whether your SMS was delivered successfully or encountered an error. Understanding these status codes helps you handle delivery failures and track message performance:
Final Outcome & Prerequisites:
By the end of this guide, you will have a NestJS application capable of:
Prerequisites:
npm install -g @nestjs/cli).gitinstalled.Setting Up Your Vonage Account for Delivery Receipts
Before building your webhook handler, you need to configure your Vonage account to send delivery receipts to your application and set up a local development environment for testing.
A. Vonage Account Configuration:
API KeyandAPI Secretfrom the main dashboard page. You'll need these later for environment variables (primarily for sending SMS and for basic DLR verification).Numbers→Buy Numbers. Search for and purchase a number with SMS capabilities in your desired country. Note this number.Settingsin the left-hand menu.SMS settingssection.Delivery receipts (DLR) webhooksfield.ngrokto create this public URL for your local machine. Set upngrokin the next step, then return here to paste the generated URL.POSTand the type isJSON. Leave the URL field blank for now or enter a temporary placeholder.B. Local Development Environment Setup (ngrok):
ngrokcreates a secure tunnel from the public internet to your local development machine.Start ngrok: Open your terminal and run the following command, replacing
3000with the port your NestJS app will run on (NestJS default is 3000):Copy Forwarding URL:
ngrokdisplays session information, including aForwardingURL ending in.ngrok-free.appor similar (e.g.,https://random-subdomain.ngrok-free.app). Copy thehttpsversion of this URL. This is your temporary public address.Update Vonage DLR Webhook URL:
Settings→SMS settings.ngrokForwarding URL into theDelivery receipts (DLR) webhooksfield./api/webhooks/vonage/delivery-receipts.https://random-subdomain.ngrok-free.app/api/webhooks/vonage/delivery-receipts.POSTandJSONare selected next to the URL field.Save changes.Creating Your NestJS Project for Vonage Webhooks
Create the NestJS application.
Create Project: Open your terminal, navigate to your desired parent directory, and run:
Choose your preferred package manager (npm or yarn) when prompted.
Navigate to Project:
Install Dependencies:
Project Structure Overview:
src/: Contains your application code.main.ts: Entry point, bootstraps the application.app.module.ts: Root module of the application.app.controller.ts: Default example controller (you'll remove/replace this).app.service.ts: Default example service (you'll remove/replace this)..env: (You will create this) Stores environment variables.nest-cli.json,package.json,tsconfig.json, etc.: Configuration files.Architectural Decision: Use NestJS modules to organize features. Create a dedicated
WebhookModuleto handle incoming webhooks from Vonage.Configuring Vonage Credentials in NestJS
Securely manage your Vonage credentials and other configurations.
Create
.envfile: In the root directory of your project, create a file named.env.Add Variables: Add the following, replacing placeholders with your actual Vonage details:
VONAGE_API_KEY: Your key from the Vonage dashboard.VONAGE_API_SECRET: Your secret from the Vonage dashboard.VONAGE_NUMBER: The Vonage virtual number you purchased.PORT: The port the application will listen on (should match the ngrok port).Create
.env.example: Copy.envto.env.exampleand remove the sensitive values. Commit.env.exampleto git, but add.envto your.gitignorefile to avoid committing secrets.Integrate
ConfigModule: Modify yoursrc/app.module.tsto load environment variables globally:Update
main.tsto use the PORT and Global Validation: Modifysrc/main.tsto respect thePORTenvironment variable and enable global validation pipes.ValidationPipeautomatically validates incoming request bodies against your DTOs (Data Transfer Objects).app.setGlobalPrefix('api')matches the/api/…path structure used in the webhook URL.Building the Vonage DLR Webhook Endpoint
Create the module, controller, and service to receive and process the DLR webhooks.
Generate Webhook Module, Controller, and Service: Use the Nest CLI:
--flatprevents creating a dedicated directory for each. Adjust if you prefer nested structures.webhook.module.ts,webhook.controller.ts, andwebhook.service.tsin thesrcdirectory (orsrc/webhookif not using--flat). EnsureWebhookModuleis imported inapp.module.tsas shown previously.Define the DLR Data Structure (DTO): Create a Data Transfer Object (DTO) to represent the expected payload from Vonage and apply validation rules. Create a file
src/webhook/dto/vonage-dlr.dto.ts:class-validator, and improve type safety.@ApiPropertyfor potential Swagger integration.Implement the Webhook Controller: Define the endpoint that matches the URL configured in Vonage (
/api/webhooks/vonage/delivery-receipts).@Controller('webhooks/vonage'): Sets the base path relative to the global prefix/api.@Post('delivery-receipts'): Defines the specific route.@Body() dlrData: VonageDlrDto: Injects the parsed and validated request body.@HttpCode(HttpStatus.OK): Ensures a200 OKstatus is sent back to Vonage upon successful handling before thereturnstatement is evaluated, unless an exception is thrown.Implement the Webhook Service: Contains the business logic for handling the DLR data.
ConfigServiceto retrieve theVONAGE_API_KEY.Handling Errors in Vonage Delivery Receipt Webhooks
NestJS provides built-in mechanisms, but let's refine them.
ValidationPipeautomatically throws aBadRequestException(400) if the incoming data doesn't match theVonageDlrDto. This response is sent back to Vonage. Since validation failures usually mean the request structure is wrong, a 400 is appropriate, and Vonage typically won't retry these.try...catchblock in the controller logs internal processing errors but still returns200 OKto Vonage. This acknowledges receipt and prevents Vonage from retrying indefinitely due to bugs in your processing logic. If you want Vonage to retry (e.g., temporary database outage), you should throw anHttpException(e.g.,ServiceUnavailableException- 503) from the controller or service.Logger. For production, configure a more robust logging strategy:log,debug,warn,error).Securing Your Vonage Webhook Endpoint
Webhook endpoints are publicly accessible, so security is paramount.
HTTPS: Always use HTTPS for your webhook endpoint in production.
ngrokprovides HTTPS locally. Ensure your production deployment environment (e.g., via load balancer, reverse proxy) enforces HTTPS.Input Validation: Handled effectively by the
VonageDlrDtoand the globalValidationPipe. This is crucial to prevent processing malformed data.Source Verification (Basic & Limitations):
WebhookServiceprovides minimal verification. As noted, the key is part of the payload itself./api/webhooks/vonage/dlr/SOME_RANDOM_SECRET_TOKEN). Check this token in your controller. This is security through obscurity and less robust than signatures.Rate Limiting: Protect your endpoint from abuse or accidental loops by implementing rate limiting. The
@nestjs/throttlermodule is excellent for this.Configure it in
app.module.ts:Denial of Service (DoS) / Resource Exhaustion: Ensure your
processDeliveryReceiptlogic is efficient. Avoid long-running synchronous operations. Offload heavy or potentially slow tasks (e.g., complex database updates, third-party API calls) to background job queues (like BullMQ, RabbitMQ) managed by separate workers.Testing Your Vonage SMS Delivery Receipt Handler
Thorough testing ensures reliability.
A. Unit Testing: Test the service logic in isolation.
Modify Service Test: Update
src/webhook/webhook.service.spec.ts:beforeEach,afterEach) to correctly mock the logger andconsole.log.B. Integration Testing: Test the controller and service together.
@nestjs/testing) to create a testing module that includes theWebhookModule.supertestto send mock HTTP POST requests to the/api/webhooks/vonage/delivery-receiptsendpoint with various valid and invalid DLR payloads.WebhookService.processDeliveryReceiptmethod to ensure it's called with the correct data.C. End-to-End (E2E) Testing:
npm run start:dev).ngrok http 3000(or your port) is running.ngrokURL +/api/webhooks/vonage/delivery-receipts.@vonage/server-sdk) to send an SMS message from your configured Vonage number to a real test phone number you have access to.ngrokconsole for incoming POST requests to your webhook path.delivered).failedorrejectedDLR).Deploying Your NestJS Vonage Webhook to Production
.envfiles with production secrets.ngrokone.Conclusion
You have successfully implemented a NestJS webhook endpoint to receive and process Vonage SMS Delivery Receipts. This allows your application to track message statuses effectively. Implement robust error handling, security measures (especially source verification limitations), and thorough testing before deploying to production. Further enhancements could include storing DLR data in a database, triggering notifications based on status changes, and integrating with analytics platforms.
Frequently Asked Questions (FAQ)
How do Vonage delivery receipts work?
Vonage delivery receipts (DLRs) are HTTP webhook callbacks sent from Vonage to your server when an SMS message's delivery status changes. After you send an SMS through Vonage, carriers report back the delivery status (delivered, failed, rejected, etc.), and Vonage forwards this information to your configured webhook endpoint as a POST request.
What is the difference between intermediate and final DLR statuses?
Intermediate statuses like
acceptedandbufferedindicate the message is in transit.acceptedmeans Vonage has accepted the message, whilebufferedmeans a carrier has queued it. Final statuses likedelivered,failed,rejected, andexpiredrepresent the terminal state of the message and won't change further.How do I handle failed SMS deliveries in NestJS?
When you receive a DLR with status
failedorrejected, check theerr-codefield for the specific error reason. Common handling strategies include: logging the failure for analytics, implementing retry logic with exponential backoff, notifying your support team for persistent failures, and validating recipient numbers before sending future messages.Can I test Vonage webhooks locally without deploying?
Yes, use ngrok to create a public tunnel to your local NestJS application. Run
ngrok http 3000(or your port), copy the HTTPS forwarding URL, and configure it in your Vonage dashboard settings. This allows Vonage to send DLR webhooks to your local development environment.How do I secure my Vonage webhook endpoint?
Vonage SMS DLR webhooks don't support cryptographic signatures, so implement these security measures: verify the
api-keyfield matches your Vonage API key, use HTTPS for all webhook URLs, implement rate limiting with@nestjs/throttler, consider IP whitelisting if Vonage publishes stable IP ranges, and validate all incoming data with DTOs and class-validator.What happens if my webhook endpoint is temporarily unavailable?
Vonage will retry sending DLR webhooks if your endpoint returns a non-2xx status code or times out. However, retry behavior has limits. To handle temporary outages, ensure your endpoint responds quickly (under 10 seconds), return 200 OK even for internal processing errors (log them instead), and implement idempotency to handle duplicate DLRs safely.
How do I store delivery receipts in a database?
In the
WebhookService.processDeliveryReceiptmethod, add database logic using TypeORM, Prisma, or Mongoose. Store themessageId,status,sctstimestamp,err-code, and recipientmsisdn. Create a Message model with fields for tracking status changes over time, and update the status when DLRs arrive.Can I receive DLRs for messages sent through different Vonage accounts?
Yes. The DLR payload includes an
api-keyfield identifying which Vonage account sent the message. Use this field to route DLRs to the appropriate handler if you manage multiple Vonage accounts through a single webhook endpoint.What Node.js and NestJS versions should I use?
Use Node.js v22 LTS (active support until October 2025) with NestJS v11.1.6 for production deployments. Minimum requirement is Node.js v18+ for NestJS 11 compatibility. These versions provide long-term stability and security updates.[1][2]
How do I troubleshoot webhook delivery issues?
Check these common issues: verify the webhook URL in Vonage dashboard settings is correct and uses HTTPS, ensure your endpoint returns 200 OK status codes, check ngrok is running for local development, review NestJS application logs for validation errors, verify the global validation pipe is configured correctly, and check Vonage dashboard logs for webhook delivery attempts and errors.
References
[1] Node.js Release Working Group. "Node.js Releases." Retrieved from https://nodejs.org/en/about/previous-releases. Node.js v22 LTS information: https://nodesource.com/blog/Node.js-v22-Long-Term-Support-LTS
[2] NestJS. "Releases." GitHub. Retrieved from https://github.com/nestjs/nest/releases. NestJS v11 announcement: https://trilon.io/blog/announcing-nestjs-11-whats-new
[3] Vonage. "@vonage/server-sdk v3.24.1." npm. Retrieved from https://www.npmjs.com/package/@vonage/server-sdk
[4] Vonage API Developer. "Webhooks Guide – SMS Delivery Receipts." Retrieved from https://developer.vonage.com/en/getting-started/concepts/webhooks