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

Frequently Asked Questions

Use the Vonage Messages API and Node.js SDK. Install the `@vonage/server-sdk` package, initialize a Vonage client with your API credentials, and use the `vonage.messages.send()` method with a new `SMS` object containing the recipient, sender, and message text. This allows you to send text messages programmatically.
A Vonage webhook is a mechanism for receiving real-time notifications from the Vonage APIs about events related to your application, such as delivery status updates for SMS messages or incoming messages sent to your Vonage number. Your application provides URLs where Vonage sends these updates as HTTP POST requests.
Webhooks provide reliable delivery confirmation. Sending an SMS through an API is often "fire and forget," meaning the initial response only confirms the message was *accepted*, not *delivered*. Webhooks give you real-time feedback as the message travels through the mobile network to the recipient's device.
Create a Vonage application in your dashboard, enable the Messages capability, and provide the URL of your webhook endpoint for the 'Status URL' field. This URL will receive POST requests from Vonage containing delivery updates. ngrok can be used during development to expose a local server URL.
The Vonage Messages API is a versatile API enabling you to send and receive messages across various channels, including SMS, MMS, WhatsApp, and more. This tutorial demonstrates its use specifically for sending and tracking the delivery status of SMS messages.
Set up an 'Inbound URL' for your Vonage application in the dashboard. When someone sends an SMS to your Vonage number, Vonage sends an HTTP POST request to this URL containing the message content. Your application can then process this inbound message data.
You will need Node.js and npm installed, a Vonage account (free tier is available), a rented Vonage virtual number, and ngrok installed and authenticated. The Vonage account gives you API credentials and test credits. ngrok is used to expose your local development server.
Ngrok creates a public URL that tunnels to your local development server. This is necessary because Vonage's webhooks need to reach your local machine, which isn't directly accessible from the internet. Ngrok provides the bridge between your local server and Vonage's webhooks.
Do *not* use ngrok or unprotected webhook endpoints in production. Secure your webhooks using either Signed Webhooks with a shared secret or JWT verification as described in the Vonage documentation. This prevents unauthorized parties from sending fake requests to your server.
The `message_uuid` is a unique identifier for each message sent through the Vonage Messages API. It is included in both the initial API response when sending the message and in all subsequent status webhook updates. Use it to track the lifecycle of individual messages.
Vonage provides various status updates, including 'submitted' (accepted by Vonage), 'delivered' (confirmed by the carrier), 'rejected' (rejected by Vonage or the carrier), and 'undeliverable'. Consult the Vonage documentation for a complete list and explanation of these statuses.
The Vonage Node.js SDK (`@vonage/server-sdk`) simplifies interaction with Vonage APIs within a Node.js environment. It handles the complexities of API requests and responses, making it easier to send SMS messages, receive webhooks, and work with other Vonage services.
Yes, emojis are generally supported. However, using emojis or non-GSM characters might cause the message to be encoded as Unicode, which can reduce the maximum character count per SMS part (segment). Vonage handles this, but it's something to be aware of.
The `express.json()` middleware is crucial for parsing incoming JSON data. Vonage's webhook POST requests contain the payload (delivery status, inbound message data) as JSON in the request body. Without `express.json()`, you won't be able to access this JSON data in your route handlers.