Send SMS and Handle 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. The send-sms.js example demonstrates how to initialize the Vonage client and use the vonage.messages.send() method to send text messages. Ensure your Vonage application is set up correctly with the necessary API credentials.
A Vonage Application acts as a container that connects your Vonage API keys, private key, virtual numbers, and webhook URLs. It's essential for sending SMS messages and receiving inbound messages and delivery receipts. Create one through the Vonage API Dashboard.
First, run ngrok to create a public URL for your local server. Then, in your Vonage Application settings, set the Inbound URL to your ngrok URL + /webhooks/inbound and the Status URL to your ngrok URL + /webhooks/status. This tells Vonage where to send incoming messages and delivery updates.
A 200 OK response from your webhook endpoint acknowledges to Vonage that you've successfully received the webhook. If Vonage doesn't receive this acknowledgment, it will retry sending the webhook multiple times to avoid data loss.
The private.key file is crucial for authenticating your application with Vonage. It is generated when you create a Vonage Application and should be kept secure, never committed to version control.
Set up a webhook endpoint (e.g., /webhooks/status) in your Express server. Vonage will send POST requests to this URL with delivery status updates, including 'submitted', 'delivered', 'failed', or 'rejected'. Your endpoint should parse this data and update your application's internal state accordingly.
Configure the /webhooks/inbound endpoint on your server and link it to your Vonage Application and virtual number. When someone sends an SMS to your Vonage number, Vonage will send the message data to your inbound webhook URL.
The Messages API is Vonage's recommended API for sending and receiving SMS messages. It's more versatile than the older SMS API and offers richer features, including unified handling of different message types.
The delivery receipt webhook includes the message_uuid, status (e.g., delivered, failed), timestamp, and any error codes/reasons if the delivery failed. Log this information for debugging and monitoring.
Yes, it is highly recommended to use environment variables (.env file for development, system environment variables for production) to store sensitive credentials like API keys and secrets. This prevents accidental exposure in your codebase.
Implement a try...catch block around the vonage.messages.send() call to handle potential errors during the API request. Log detailed error information, including any Vonage-specific error responses, and implement retry logic if necessary.
ngrok provides a public URL that tunnels to your local development server. This is necessary for Vonage to send webhook requests to your local machine during development and testing.
Use a structured logging library like Winston or Pino for better organization, different log levels (info, error, warning), and easier log management in a production environment.
Start your Express server and ngrok. Run the send-sms.js script to send a test SMS and observe the logs in your server console and the ngrok web interface for delivery status updates and webhook requests. Also, send an SMS to your Vonage number to test the inbound webhook.