Send and Receive 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 linking a WhatsApp number, you can send messages programmatically using the `vonage.messages.send()` method with a properly formatted message object (e.g., using `WhatsAppText` from `@vonage/messages`).
The Vonage Messages API is a unified platform for sending and receiving messages across multiple channels, including WhatsApp, SMS, and MMS. It provides a single interface for interacting with these services, simplifying development and integration.
Webhooks allow your application to receive real-time updates from the Vonage platform. When a user sends a WhatsApp message or there's a status update (like 'delivered'), Vonage sends a webhook request to your server containing this information.
The `apiHost` setting in the Vonage SDK configuration directs API calls to the sandbox environment. You should remove this setting when transitioning your application from development/testing to production to use the standard Vonage Messages API endpoint.
The provided example focuses on text messages. To handle other message types like images, you'll need to enhance the '/inbound' route to process different `message.content.type` values (e.g., 'image') and access the content accordingly within the `req.body.message.content` structure.
Combine Node.js with Express and the Vonage Messages API. Create an Express server, install necessary Vonage SDKs, configure webhooks to receive messages and status updates, and implement message sending logic using the Vonage client.
Ngrok creates a public tunnel to your local development server. This allows Vonage's webhooks to reach your application running locally, which is necessary for testing before deployment.
Dotenv loads environment variables from a `.env` file into `process.env`. This is a secure way to manage sensitive information like API keys and secrets, keeping them out of your codebase and version control.
Implement JWT verification using the `@vonage/jwt` package. Extract the token from the 'Authorization' header in the webhook request, then use the `verifySignature` function with your signature secret (`VONAGE_API_SIGNATURE_SECRET`) to ensure its validity.
A Vonage Application acts as a container for your communication settings. It links phone numbers, webhooks, and other configurations, allowing you to manage your services through the Vonage platform.
Inside the `/inbound` webhook handler, extract the sender's number and the message content. Use the `sendWhatsAppReply` function (or similar logic) with the `vonage.messages.send` method to send a reply message using the Vonage Messages API.
The WhatsApp Sandbox is a testing environment ideal for development and initial testing. It allows you to send and receive WhatsApp messages without needing a fully approved WhatsApp Business Account, but with certain limitations.
Status updates inform you of the delivery status of your WhatsApp messages. These updates are sent via webhooks to your `/status` endpoint and include statuses like 'submitted', 'delivered', 'read', or 'failed'.
A WhatsApp user's message is routed via Vonage to your application through webhooks. Your app processes the message and sends a reply back through Vonage, which delivers it to WhatsApp. Status updates also flow through webhooks back to your application.