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

Frequently Asked Questions

Use the Vonage Messages API and Node.js SDK. The `sendSms` function in the provided code demonstrates how to initialize the Vonage client and send an SMS message using your API credentials and a Vonage virtual number. It handles asynchronous delivery receipts via webhook, and it includes error handling using try-catch blocks. It includes a clientRef that you can use to correlate messages with your internal system identifiers.
The Vonage Messages API is a multi-channel communication platform that allows you to send SMS, MMS, WhatsApp messages, and more. In this Node.js application, it's used to send SMS messages and configure status webhooks to receive delivery updates.
Vonage uses the Status URL to send real-time delivery receipts (DLRs) to your application as webhooks. This lets your application know the status of sent messages (e.g., delivered, failed) asynchronously, without having to poll the API. You must use a publicly accessible URL like one provided by ngrok during development or your production server's URL for receiving these updates.
Update the Vonage Application's Status URL in the Vonage Dashboard *before* sending an SMS message, or after restarting your application. This ensures that Vonage knows where to send delivery status updates to your webhook endpoint.
Yes, use ngrok to create a secure tunnel to your locally running Express server. This provides a public HTTPS URL that Vonage can use to reach your webhook endpoint during development. Update your `.env` file's BASE_URL and your application's Status URL on Vonage Dashboard with this ngrok URL. Be sure to stop and restart your application after setting this URL.
Set up a webhook endpoint in your Node.js Express application (e.g., `/webhooks/status`) and configure this URL as the Status URL in your Vonage application settings. The webhook endpoint receives real-time delivery status updates from the Vonage API, typically a few seconds to a minute after the message has been sent, depending on network availability.
The Vonage Application ID is a unique identifier for your application within the Vonage platform. It's used along with your private key to authenticate requests to the Messages API and link your virtual numbers and webhooks to your application.
The private key is crucial for secure authentication with the Vonage Messages API. It's used to generate JWTs (JSON Web Tokens) for requests, ensuring only authorized applications can send messages and access related data. Never expose your private key publicly and never commit it to version control.
Use ngrok during local development to expose your local webhook server to the public internet so Vonage can send delivery status updates to your webhook endpoint. Ngrok isn't needed in production once deployed to a server with a public URL.
Implement webhook signature verification using `verifySignature` method from `@vonage/server-sdk`. This ensures requests are from Vonage, preventing spoofing attacks. Configure signature secret in Vonage account settings. This step is critical for production.
The 'delivered' status in a Vonage delivery receipt (DLR) webhook indicates that the SMS message has successfully reached the recipient's mobile handset. Not all carriers or countries reliably report this final status. Sometimes the status might remain 'submitted' or 'accepted', even if delivered. Thorough testing is necessary. If you are getting 'submitted', try texting to a different carrier.
Verify ngrok is running and that the URL matches the Status URL configured in your Vonage account. If the Node.js server is running and no webhooks are received, also confirm no firewall is blocking incoming traffic. Inspect the ngrok inspection interface to see if the requests are being received. If the requests are not being received, check the Vonage Status page for system issues. Also check that the recipient number and virtual number formats are valid. Ensure you are responding to the Vonage webhook with a 2xx status code. Ensure your application has Messages set as the default SMS API.
The `clientRef` is an optional user-defined reference string when sending SMS messages using the Vonage API. It lets you associate a unique identifier from your system with each message, allowing for easier tracking, logging, and reconciliation within your own application logic. It appears in status webhooks and logs.