Developer Guide: Sending and Receiving SMS & WhatsApp Messages with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Messages API and Node.js SDK. After setting up a Vonage application and configuring webhooks, you can send SMS messages by making a POST request to the `/send-message` endpoint with the recipient's number and the message text in the request body. The Vonage SDK simplifies the process of interacting with the API.
The Vonage Messages API is a versatile API that enables sending and receiving messages across various channels like SMS, MMS, WhatsApp, Facebook Messenger, and Viber. This API is used in conjunction with the Vonage Node.js SDK to build applications capable of rich, multi-channel communication.
Vonage uses JWT (JSON Web Tokens) for webhook security to ensure that incoming webhook requests originate from Vonage and haven't been tampered with. The `verifyVonageSignature` middleware in the example code verifies the JWT against your signature secret, protecting your application from unauthorized access.
ngrok is useful for testing webhooks during local development. It provides a publicly accessible URL that forwards requests to your local server. However, for production, use a stable, publicly accessible URL and deploy your application to a server.
Yes, the provided Node.js application and Vonage setup supports sending and receiving WhatsApp messages via the Vonage WhatsApp Sandbox. Ensure your WhatsApp number is allowlisted in the sandbox for testing. The code uses the WhatsAppText class from the Vonage Messages API to send WhatsApp messages.
Configure the inbound webhook URL in your Vonage application settings to point to your application's `/webhooks/inbound` endpoint. The Vonage server will send an HTTP POST request to this URL with message details whenever an SMS is received on your Vonage virtual number.
Vonage sends message status updates (e.g., delivered, read) to the Status URL you configured in the application settings. Implement the `/webhooks/status` route in your Express application to receive these updates and process them accordingly, such as storing them in a database.
A Vonage application acts as a container for your communication settings and authentication. It manages API keys, webhooks, and other configuration related to your Vonage services. It is essential for routing messages and handling security.
Use the `@vonage/jwt` library, specifically the `verifySignature` function. Implement middleware that extracts the JWT from the `Authorization` header and verifies it against your signature secret. Reject requests with invalid signatures.
Node.js version 18 or higher is recommended for Vonage integration. Ensure compatibility within your specific target Node.js version while keeping up-to-date with Node.js releases and security patches.
Navigate to the Messages Sandbox under Developer Tools in your Vonage dashboard. Activate the sandbox and allowlist your WhatsApp number by scanning the QR code or sending the provided message to the sandbox number. Configure the inbound and status webhooks within the sandbox settings.
Several reasons could prevent webhooks from working: ngrok tunnel may not be running or correctly configured, the webhook URL in the Vonage settings may be incorrect, credentials may be wrong, server may not be running or experiencing errors, or the private key may not exist.
Your Vonage API key and secret are found on the main page of your Vonage API dashboard. You should store these securely in environment variables, especially in a production setting.
Never commit your Vonage `private.key` file to version control. For production, store its contents securely, ideally in a dedicated secrets management service or as a secure environment variable. Avoid storing it directly on the server file system.