Sending and Receiving SMS & WhatsApp with Node.js and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Messages API with the Node.js SDK. After initializing the Vonage client with your API credentials, call `vonage.messages.send` with the recipient's number, your Vonage number as the sender, and the message text. Ensure your Vonage number is SMS-enabled in your dashboard.
The Vonage Messages API is a unified platform for sending and receiving messages across multiple channels, including SMS, WhatsApp, Viber, and Facebook Messenger. It simplifies multi-channel communication by providing a single API for various messaging platforms.
The Vonage WhatsApp Sandbox provides a testing environment for developing and experimenting with WhatsApp integrations without incurring real-world costs or needing full business approval. You can whitelist your own number for testing purposes.
Remove the `apiHost: MESSAGES_SANDBOX_URL_MESSAGES` configuration from your Vonage client *only* when deploying to production for live WhatsApp messaging. This setting is crucial for directing WhatsApp traffic to the Sandbox during development.
Yes, you can receive WhatsApp messages locally during development. Use ngrok to create a public, secure URL for your local server that Vonage can send webhooks to. Configure this ngrok URL in your Vonage application's webhook settings, specifically for the WhatsApp Sandbox.
In the Vonage Dashboard, create a new application and enable the Messages capability. Provide the URLs for your inbound and status webhooks, ensuring they point to your public server address (e.g., ngrok URL during development) appended with '/webhooks/inbound' and '/webhooks/status'. Link your Vonage virtual number to this application.
The Vonage Application ID uniquely identifies your application within the Vonage platform and is essential for authentication when using the Messages API with an associated private key. You'll need both the Application ID and the corresponding private key to initialize the Vonage client in your Node.js code.
Vonage automatically retries webhook requests if your server doesn't respond with a 200 OK within a short timeframe. Ensure your webhook handlers execute quickly and return 200 OK. Process time-consuming tasks asynchronously using background jobs. Implement idempotency checks (e.g., using message_uuid) to prevent duplicate actions on retries.
ngrok creates secure tunnels from public URLs to your local machine, allowing Vonage to send webhooks to your development server. This is crucial for testing webhooks before deploying your application.
Store API keys, secrets, and private keys in environment variables (`.env` file locally) and never commit them to version control. Use secure configuration management systems in production.
Go to the Messages API Sandbox in your Vonage Dashboard. Scan the QR code with your WhatsApp app, or send the specified message from your phone's WhatsApp to the provided number. Follow the subsequent instructions to complete the whitelisting.
A frequent error is "Forbidden," indicating the recipient isn't whitelisted. Ensure the target number is correctly added to the Sandbox. Other errors might relate to incorrect `apiHost` settings or using a non-sandbox number as the 'from' address in WhatsApp messages.
A recommended schema includes fields for message_uuid, channel, direction, sender/recipient IDs, message content, status, timestamps, and error details. Consider indexing columns like sender/recipient, status, and channel for efficient querying.
Status updates provide real-time delivery information about your outbound messages (e.g., 'delivered', 'failed', 'read'). Vonage sends these updates to your status webhook URL, where your application can update the message status in its database accordingly.
Enable signature verification in your Vonage Dashboard settings to enhance security. Refer to the Vonage Node.js SDK documentation for the latest implementation details, which often involve middleware using your signature secret to validate the request's authenticity.