Sending SMS and Receiving Delivery Status Callbacks with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Messages API with the `@vonage/server-sdk` in your Node.js application. Initialize the Vonage client with your API key, secret, application ID, and private key. Then, use `vonage.messages.send()` with the recipient's number, your Vonage virtual number, and the message text to send SMS messages.
The Vonage Messages API is a multi-channel communication platform that allows you to send and receive SMS, MMS, WhatsApp messages, and more. This guide demonstrates its use for sending SMS messages and receiving delivery status updates.
Webhooks provide a real-time, asynchronous mechanism for Vonage to push delivery status updates to your application. Instead of constantly polling the API, your application receives immediate notifications when the message status changes, like being delivered or failing.
Use a Vonage Application ID and its associated private key when using the Messages API. This ensures secure authentication using JWT (JSON Web Tokens), which are important when handling webhooks for security reasons.
Alphanumeric Sender IDs might be allowed, but you must check country-specific regulations. In some regions (e.g., the US), you must use a Vonage virtual number. If allowed, ensure it's pre-registered with Vonage.
Configure a 'Status URL' in your Vonage Application settings. This URL should point to a publicly accessible endpoint in your application (e.g., using ngrok for development). Vonage will send an HTTP POST request to this URL with delivery status updates.
ngrok creates a temporary public URL that tunnels to your local development server. This allows Vonage to send webhooks to your locally running application during development, essential for receiving delivery status updates.
Ensure your webhook endpoint returns a 2xx HTTP status code (ideally 200 OK or 204 No Content) as quickly as possible. This tells Vonage that you've received the webhook. Your endpoint logic should be idempotent to handle potential duplicate webhook deliveries.
Common statuses include 'submitted', 'delivered', 'failed', 'accepted', 'buffered', 'expired', and 'unknown'. 'Buffered' often means the recipient's device is offline. Each status describes a stage in the delivery process.
Verify that ngrok is running and correctly forwarding to your server's port, the Status URL in your Vonage Application settings is accurate (including the '/webhooks/status' path), and your server application is running without errors.
The `@vonage/server-sdk` simplifies interaction with Vonage APIs in Node.js. It handles authentication, request construction, and response parsing for sending SMS messages and other Vonage services.
Sensitive data, such as API keys, secrets, and private keys, should never be hardcoded. Environment variables (like in a `.env` file) offer a secure way to manage these credentials locally, keeping them out of your codebase.
A suggested schema includes columns for `message_uuid`, `to_number`, `from_number`, `message_body`, submission and status update timestamps, current status, error codes, and usage details.
Respond to the webhook request with a 200 OK immediately. Perform database updates or other time-consuming tasks asynchronously in the background to prevent blocking the webhook response and causing Vonage retries.
Use HTTPS, potentially IP whitelisting or secrets in URLs. Currently, simple signature validation is not supported for messages API webhooks. Cryptographically signed webhooks are more complex to implement for these particular APIs.