Send SMS and Receive Delivery Receipts with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Node.js SDK and the `vonage.sms.send()` method. This method requires the recipient's number, your Vonage virtual number, and the message text. Ensure your API key and secret are configured correctly in your .env file and loaded into your application.
A Delivery Receipt (DLR) is a status update from the carrier network confirming the delivery status of your SMS message. Vonage forwards these updates to your application via webhooks, providing information like 'delivered', 'failed', or 'expired'.
Webhooks provide a real-time, asynchronous mechanism for Vonage to push delivery status updates to your application. This avoids the need for your application to constantly poll Vonage for status information.
Configure your webhook URL in the Vonage Dashboard *before* sending SMS messages if you want to track their delivery status. This ensures you receive DLRs as soon as they're available from the carriers.
Yes, ngrok creates a public HTTPS URL that tunnels requests to your local development server, allowing Vonage to deliver webhooks even if your server isn't publicly accessible.
Create an endpoint in your Express app (e.g., `/webhooks/dlr`) to listen for POST requests. Parse the request body, log the DLR information, and *always respond with a 2xx status code* to acknowledge receipt.
The `messageId` in the DLR corresponds to the `message-id` returned by the Vonage SMS API when you send a message. This ID is crucial for correlating DLRs with the original outbound messages.
Common status codes include 'delivered', 'failed', 'expired', 'buffered', 'rejected', 'accepted', and 'unknown'. Check the Vonage API reference for a comprehensive list and explanations of error codes.
Vonage retries sending the webhook if it doesn't receive a 2xx response quickly. Make your webhook handler idempotent so that processing the same DLR multiple times has no adverse effects. Use the messageId to check if the DLR has been processed before and skip the update if needed
Check your ngrok tunnel, server logs, and Vonage Dashboard settings. Ensure the webhook URL is correct and points to the right endpoint. Verify your server is running and responding with a 2xx status code.
Use HTTPS. Vonage signed webhooks with a JWT signature are supported by the Messages API but are not generally supported for standard SMS API DLRs. Use a rate limiter (e.g., `express-rate-limit`) to prevent abuse. Secure your API Key and secret. Though IP whitelisting is possible, it is less flexible due to potential IP range changes and generally avoided in favor of other security methods
Store message information and DLR statuses in a database. Create a table with fields for `vonage_message_id` (unique and indexed), `status`, `error_code`, timestamps, etc. Use the `messageId` from the DLR to update the corresponding record.
DLR delays depend on carrier network reporting times and can vary from seconds to minutes or occasionally longer. Design your application to tolerate these delays.
Not all carriers reliably support DLRs. Don't rely solely on DLRs for critical application logic. Check the recipient number and carrier capabilities. If issues persist, contact Vonage support.