Send and Receive SMS & WhatsApp Messages with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Messages API with the Node.js SDK and Express. Set up a Vonage application and link a purchased Vonage number. Then, use `vonage.messages.send()` with the correct parameters, including your Vonage number, recipient number, and message content. Ensure your API keys and secrets are configured correctly in your .env file.
The Vonage Messages API is a unified platform for sending and receiving messages across various channels, including SMS, WhatsApp, Viber, and Facebook Messenger. It simplifies communication integration by providing a single interface for multiple messaging platforms. This allows developers to manage communication across different channels without separate integrations.
The Vonage Node.js SDK utilizes a private key to sign API requests, ensuring secure authentication. When you create a Vonage Application, you download a private key file (private.key). The SDK uses this key to sign each request. Vonage then verifies these signatures against the corresponding public key, confirming the request's authenticity and preventing unauthorized access.
ngrok is crucial during development to expose your local server's webhooks to the internet so Vonage can reach them. For production, deploy to a hosting provider and use its stable public URL for webhooks, as ngrok is not suitable for production environments.
Yes, during development, you can use the Vonage Messages API Sandbox to test sending and receiving WhatsApp messages without needing a full business account setup. Connect your personal WhatsApp number to the Sandbox to begin testing.
Use the `@vonage/jwt` package's `verifySignature` function. Pass it the JWT from the `Authorization` header, your `VONAGE_API_SIGNATURE_SECRET`, and the exact raw request body. This verification ensures that webhook requests truly originate from Vonage, enhancing security.
Vonage Applications act as containers that manage your communication settings and link your Vonage numbers to your code. They hold webhook URLs, manage linked numbers, and store authentication settings, enabling Vonage to route incoming messages and deliver status updates correctly.
In your Vonage Application settings, configure the Inbound URL to point to your server's endpoint (e.g., `https://your-app.com/webhooks/inbound`). In your Express app, define this route to handle incoming message data and always respond with a 200 OK status to prevent retries.
If your webhook endpoints don't return a 200 OK status promptly, Vonage assumes a failure and will retry the request multiple times. This can result in duplicate message processing or other unintended side effects.
Examine the `channel` and `message_type` fields in the webhook payload to determine the incoming message type (SMS, WhatsApp, etc.). Your logic can then process messages differently based on these values or other relevant fields like the content itself.
If webhooks aren't working, check if ngrok is running and correctly configured, if the right Vonage number is linked to the Vonage application, and that your Node.js server is running without errors. Also, review the ngrok interface for incoming requests and error logs for more details.
Protect API credentials (never commit `.env` or `private.key` to version control), always verify webhook signatures, validate API inputs thoroughly, and use rate limiting to prevent abuse.
Use the `WhatsAppText` class from the `@vonage/messages` package to structure the message payload. In your API request, provide the recipient's WhatsApp number, your Vonage WhatsApp Sandbox number (for testing), and the message text. Ensure your number is connected to the Vonage WhatsApp Sandbox.