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

Frequently Asked Questions

Use the Vonage Server SDK for Node.js and the `/send-sms` endpoint created in this tutorial. This endpoint interacts with the Vonage SMS API to send messages programmatically after receiving the recipient's number and the message text from a POST request. Remember to configure your Vonage API key and secret in the `.env` file.
A Delivery Receipt (DLR) is a status update sent by Vonage to your application after an SMS message is sent. It confirms the delivery status (e.g., delivered, failed, expired) of the message, providing crucial feedback for reliable communication workflows. Vonage uses webhooks to send DLRs, ensuring real-time updates.
Set up a webhook URL in your Vonage API dashboard pointing to a `/delivery-receipt` endpoint in your Express app. When Vonage sends the DLR as a POST request containing the delivery status, your Express app parses and logs it. Don't forget to use ngrok during development to expose the local server for webhook configuration and testing.
Vonage uses webhooks for DLRs to provide real-time updates asynchronously without your application needing to constantly poll the API. This push-based mechanism allows your application to react immediately to delivery status changes, which is essential for time-sensitive applications.
For new projects, consider using the Messages API for its unified webhook format and support for multiple messaging channels beyond just SMS (e.g., WhatsApp). If working with an existing SMS API implementation, migrating to the Messages API is possible but requires adjusting endpoints and data structures.
You need Node.js and npm (or yarn), a Vonage API account with API Key and Secret, a Vonage virtual number for sending SMS, ngrok for testing webhooks, and some familiarity with JavaScript, Node.js, Express, and REST APIs. The tutorial uses other packages such as dotenv, express-validator, and optionally, nodemon.
Run `ngrok http 3000` in a separate terminal to expose your local server. Copy the HTTPS URL provided by ngrok. Update the `BASE_URL` in your `.env` file with this HTTPS URL, and configure it as your webhook URL for Delivery Receipts in the Vonage API Dashboard. Restart your Node.js app to apply the changes.
Express-validator is used for input validation, enhancing the robustness of your application. It performs checks on incoming requests in the `/send-sms` endpoint, such as ensuring the "to" and "text" fields are present and correctly formatted before sending the SMS, improving security and data quality.
Use `try...catch` blocks around `vonage.sms.send()` to handle potential errors like network issues or incorrect API credentials. Check the `resp.messages[0].status` code for immediate submission issues, `error.response.data` for detailed error information, and return appropriate HTTP status codes with error messages.
If `vonage.sms.send()` fails due to temporary issues (e.g., network, 5xx error from Vonage), implement application-level retries using libraries like `async-retry` with exponential backoff. Be careful not to retry on permanent errors like validation failures or incorrect number formats indicated by 4xx errors.
Always respond to Vonage DLR webhooks with a `200 OK` status, even if internal processing fails. This prevents Vonage from retrying the webhook and potentially causing duplicate processing. Log any internal processing errors for debugging after sending the `200 OK`.
While this tutorial provides a conceptual schema for a database, it's crucial in production to store `messageId`, `status`, and other relevant data. This allows querying and tracking of messages. Upon sending, store initial data. On receiving a DLR, update the corresponding record with the delivery status.
Common DLR statuses include 'delivered', 'accepted', 'failed', 'expired', 'rejected', and 'buffered'. 'delivered' signifies successful delivery, 'failed' indicates issues like invalid numbers, and 'rejected' might relate to spam filters. Consult Vonage's DLR status documentation for complete details and corresponding error codes.
Replace `console.*` logging with structured loggers like Pino or Winston. These loggers provide log levels, JSON formatting, and easier integration with log management systems, helping in tracking, analyzing, and debugging applications more efficiently.
Log in to the Vonage API Dashboard. Your API Key and API Secret are prominently displayed on the main page. Securely store these credentials in a `.env` file and never commit them to version control systems like Git.