Build a RedwoodJS App with Vonage WhatsApp Messaging - code-examples -

Frequently Asked Questions

Use the Vonage Messages API and Node.js SDK within a RedwoodJS service. Create a service function that initializes the Vonage client with your API credentials and uses the `vonage.messages.send` method with a `WhatsAppText` object to send messages.
The Vonage Messages API Sandbox is a testing environment provided by Vonage that allows developers to experiment with WhatsApp integration without a dedicated WhatsApp Business Account. It provides a sandbox number and allows you to whitelist personal WhatsApp numbers for testing.
RedwoodJS uses environment variables (`.env` files) to store sensitive information like API keys and secrets. This approach keeps credentials out of your codebase, improving security and portability across different environments.
The `apiHost` pointing to the sandbox should be removed from your Vonage client configuration when you deploy your RedwoodJS application to production and are ready to use a live WhatsApp Business Account and number.
While Vonage's primary interaction is through webhooks, you can create GraphQL mutations to trigger outbound WhatsApp messages manually from your RedwoodJS frontend. Define a mutation in your schema, implement a corresponding service function, and call `sendWhatsAppMessage` within it.
Configure webhook URLs in both your Vonage application settings and the Messages API Sandbox. Use your Ngrok URL combined with the Redwood function paths (e.g., `YOUR_NGROK_URL/.redwood/functions/whatsappInbound`). This allows Vonage to send incoming messages and status updates to your Redwood application.
Ngrok creates a public URL that tunnels to your local development server, which is essential for Vonage to send webhooks to your RedwoodJS application during development. Vonage webhooks require a publicly accessible URL to reach your local machine.
Create a Redwood function (`whatsappStatus`) to handle status update webhooks. Verify the JWT signature for security, parse the message status, and update your database accordingly using the `handleMessageStatusUpdate` service function.
You need Node.js 18+, Yarn, the RedwoodJS CLI, a Vonage API account (with API Key and Secret), Ngrok, a WhatsApp account for testing, basic Git knowledge, and a compatible operating system (macOS, Windows with WSL, or Linux).
Store Vonage credentials (including the private key *content*) as environment variables in a `.env` file. Ensure this file is added to your `.gitignore`. For production, use secure secret management provided by your hosting platform (like Render Environment Variables).
Create a Redwood function (`whatsappInbound`) as an API endpoint to handle incoming WhatsApp messages. This function will receive message data via webhooks from the Vonage Messages API after you've configured the webhook URL in your Vonage application settings.
RedwoodJS Services encapsulate the core logic for interacting with external APIs like Vonage. They provide a structured way to organize your Vonage API calls, handle authentication, send messages, and process incoming webhooks.
Navigate to the 'api' workspace in your RedwoodJS project and run `yarn add @vonage/server-sdk @vonage/messages @vonage/jwt`. These packages provide the necessary tools to interact with the Vonage APIs.
Common errors include incorrect environment variable setup, webhook URL misconfiguration, JWT signature verification failures, and issues with parsing JSON payloads. The guide provides troubleshooting tips and error handling suggestions for each step.