Implement SMS Delivery Status Webhooks with Fastify, Node.js, and Vonage Messages API - code-examples -

Frequently Asked Questions

Set up a Vonage application, enable the Messages API, and configure the Status URL to point to your Fastify webhook endpoint (e.g., 'https://your-app.com/webhooks/status'). Use ngrok for local development to expose your server publicly. Ensure the Messages API is the default in your Vonage settings for correct webhook handling. Finally, link your Vonage number to the application to receive delivery receipts.
The Vonage Messages API status webhook sends a JSON payload to your specified endpoint. This payload includes details such as 'message_uuid', 'to', 'from', 'timestamp', 'status', 'usage', 'client_ref', and 'error' (if any). Each field provides information about the delivery status of your sent message, including success, failure reasons, and cost information.
Vonage retries sending the status webhook if it doesn't receive a 2xx HTTP response (like 200 OK) from your server within a certain timeframe. This ensures your application receives crucial delivery updates even if there are temporary network issues or server downtime. It's essential to always reply with 200 OK, even if processing the status update fails internally. Handle such failures asynchronously after acknowledging the webhook receipt.
Use the Messages API when you need unified messaging capabilities across different channels (SMS, WhatsApp, Viber, etc.) or require the latest features and status reporting. The Messages API provides a more flexible and robust platform, while the SMS API is the older, simpler version primarily for basic SMS functionalities. The tutorial specifically uses the Messages API and its associated webhooks.
Always respond with a 200 OK status to Vonage, even if your internal processing encounters errors. Log the error details for debugging, and implement asynchronous mechanisms (like queues) to retry processing the failed status updates later. This prevents Vonage from continuously retrying the webhook delivery and allows you to manage failures gracefully.
Ngrok creates a secure tunnel that exposes your locally running Fastify server to the public internet. This is necessary for Vonage to deliver webhooks to your development environment, as your local machine typically doesn't have a publicly accessible IP address or HTTPS setup.
Use the Vonage Node.js SDK (`@vonage/server-sdk`) with your API credentials and call `vonage.messages.send()`. Provide the recipient's number, your Vonage virtual number, and the message text. The example code includes a '/send-test-sms' route that demonstrates this functionality using environment variables for configuration. You can access this route to trigger a test SMS easily.
A Vonage Application ID is a unique identifier for your application within the Vonage platform. It's created when you set up an application in the Vonage API Dashboard. You'll need this ID, along with your private key, to authenticate with the Vonage APIs, including the Messages API used for sending SMS and receiving status updates. The private key is downloaded when creating the application.
Yes, you can use any available port. Update the `PORT` environment variable and the ngrok command accordingly (e.g., `ngrok http 8080`). Also, ensure your Fastify server listens on the correct port and that the Vonage Status URL is updated to reflect the new port and ngrok URL if used locally.
While Vonage doesn't offer built-in signature verification for status webhooks like it does for inbound messages, you can improve security through obscurity. Use a long, random, hard-to-guess path for your webhook endpoint (e.g., `/webhooks/status/xyz123`). While not foolproof, this makes accidental discovery or simple scans less likely. It's crucial to secure your API credentials and private key.
Common reasons include: ngrok not running, incorrect Status URL in the Vonage Application settings, firewalls blocking incoming connections, Vonage number not linked to the application, or using the legacy SMS API instead of the Messages API, which results in a different webhook format. Check logs for clues or consult Vonage documentation.