Frequently Asked Questions
Integrate Infobip WhatsApp with RedwoodJS by setting up a new RedwoodJS project, configuring Infobip credentials as environment variables, implementing a service to handle sending messages, creating a GraphQL mutation to expose the sending functionality, setting up a webhook function to receive incoming messages and delivery reports, and optionally setting up a database schema to log message interactions. This guide uses TypeScript, GraphQL, Prisma, and Node.js to achieve the integration.
`ngrok` creates a secure tunnel to your local RedwoodJS development server, making it publicly accessible for receiving webhooks from Infobip. This is necessary because Infobip needs a public URL to send real-time message and delivery report notifications to during development.
Create a RedwoodJS service function that constructs the message payload, including recipient number, template name, placeholders, and language. This function then calls the Infobip API using an HTTP client like `fetch` or `axios`. You'll need your Infobip API key, base URL, and WhatsApp sender number, all configured as environment variables.
Infobip is an official Business Solution Provider (BSP) for the WhatsApp Business API. They provide the necessary infrastructure and API access to connect your application with the WhatsApp network, enabling you to send and receive messages and manage templates.
RedwoodJS offers a robust, full-stack, serverless framework with built-in features like GraphQL and Prisma, simplifying development and deployment. Its integrated frontend/backend structure streamlines the process of connecting user interfaces with WhatsApp communication flows.
Incoming messages are forwarded from WhatsApp through the Infobip platform to a RedwoodJS function acting as a webhook. This function processes the message, potentially interacts with a database to log the message, and acknowledges receipt to Infobip. Security measures like shared secrets or signature validation protect the webhook.
The guide provides a basic security example using a shared secret passed in an HTTP header. For production, you must implement robust signature validation (e.g., HMAC) according to Infobip's documentation to prevent unauthorized access to your webhook.
You will need Node.js, Yarn, the RedwoodJS CLI, an Infobip account, a registered WhatsApp Sender number, and basic understanding of RedwoodJS, GraphQL, and TypeScript. `ngrok` is necessary for local webhook development. An active and approved template on Infobip is required to send templated messages.
`InfobipTemplatePlaceholder` is a TypeScript interface defined to enforce the structure of data required by WhatsApp message templates. It ensures that the message sent to the Infobip API contains the correct placeholders for variables like body text, buttons, and headers, which get populated with dynamic values when sending the message.
Yes, the provided code can be extended to send free-form messages. Ensure the free-form messages comply with WhatsApp Business API guidelines and consider that initiating a conversation usually requires a pre-approved message template first, followed by a 24-hour window.
Infobip sends delivery reports (sent, delivered, read, failed) to the same webhook as inbound messages. The webhook handler must distinguish between message types by analyzing the payload structure. You can then update message status in your database based on the delivery reports received.
The guide recommends defining a Prisma schema with a `MessageLog` model to store message details (sender, recipient, status, content preview). Update your service and webhook functions to create and update `MessageLog` entries when sending and receiving messages and delivery reports. Be mindful of PII when logging content.
While the example uses Node's native `fetch`, a dedicated HTTP client like `axios` might be beneficial for more complex API interactions, features like interceptors for managing auth tokens or retries, or if you're more familiar with its API.
Use the language code that aligns with your approved WhatsApp message template and your target audience's language. The guide uses 'en' for English, but change this to match your template's language, e.g., 'es' for Spanish, 'fr' for French.
Developer Guide: Integrating Infobip WhatsApp with RedwoodJS
This guide provides a step-by-step walkthrough for integrating the Infobip WhatsApp Business API into your RedwoodJS application. We'll cover sending messages, handling incoming messages via webhooks, managing templates, and setting up a robust foundation for production use.
By the end of this tutorial, you will have a RedwoodJS application capable of:
This guide solves the common challenge of connecting a modern full-stack framework like RedwoodJS with a powerful communication channel like WhatsApp, leveraging Infobip as the official Business Solution Provider (BSP).
Technologies Used:
System Architecture Overview:
The system involves the user's WhatsApp application interacting with the WhatsApp network. Messages sent by the user are forwarded by WhatsApp through the Infobip platform to a RedwoodJS Function (webhook). This function processes the message, potentially interacts with a service layer and database (Prisma), and acknowledges receipt. To send messages, the RedwoodJS frontend triggers a GraphQL mutation in the RedwoodJS API. This API calls a service layer function, which interacts with the database and then calls the Infobip WhatsApp API. Infobip delivers the message via the WhatsApp network to the user's device. Delivery reports follow a similar path back from Infobip to the RedwoodJS webhook.
Prerequisites:
yarn global add @redwoodjs/cli
)ngrok
or a similar tunneling service for local webhook development.1. Setting up the RedwoodJS Project
Let's initialize a new RedwoodJS project and configure the necessary environment variables for Infobip.
Create RedwoodJS App: Open your terminal and run:
Follow the prompts (choose TypeScript).
Infobip Account Setup & Credentials:
https://<your-unique-id>.api.infobip.com
.Configure Environment Variables: RedwoodJS uses a
.env
file for environment variables. Create this file in the project root:Add your Infobip credentials and sender number:
INFOBIP_WEBHOOK_SECRET
is a simple shared secret we'll use later to add a basic layer of security to our incoming webhook. Generate a strong random string for this..env
to your.gitignore
file to prevent committing secrets. RedwoodJS does this by default, but always double-check.Install Dependencies (Optional - HTTP Client): While Node's native
fetch
is often sufficient, you might prefer a dedicated HTTP client likeaxios
for more complex scenarios or features like interceptors. For this guide, we'll stick withfetch
. If you wantedaxios
:2. Implementing Core Functionality: Sending Messages
We'll create a RedwoodJS service on the API side to encapsulate the logic for interacting with the Infobip API.
Generate Infobip Service:
This creates
api/src/services/infobip/infobip.ts
and related test files.Implement
sendTemplateMessage
Function: Sending pre-approved template messages is often the starting point, especially for initiating conversations outside the 24-hour customer service window.Open
api/src/services/infobip/infobip.ts
and add the following code:InfobipTemplatePlaceholder
,InfobipSendMessagePayload
,InfobipSendResponse
) for type safety, based on Infobip's API documentation. AdjustInfobipTemplatePlaceholder
based on the actual placeholders your specific templates require (body text, button parameters, header variables, etc.).process.env
.callInfobipApi
helper function handles constructing the request (URL, headers, authorization, body) and basic error checking/logging. Crucially, it uses theAuthorization: App YOUR_API_KEY
header format specified by Infobip.sendTemplateMessage
constructs the specific payload for the/whatsapp/1/message/template
endpoint and calls the helper function.src/lib/logger
) for debugging.3. Building the API Layer (GraphQL Mutation)
Now, let's expose the
sendTemplateMessage
functionality through a GraphQL mutation so our frontend (or other services) can trigger it.Define GraphQL Schema: Open
api/src/graphql/infobip.sdl.ts
(create it if it doesn't exist) and define the mutation:InfobipTemplateDataInput
, etc.) to structure the data passed into the mutation. Make these specific to match theInfobipTemplatePlaceholder
interface in your service and the requirements of your actual templates.InfobipSendResponse
, etc.) to match the expected structure returned by thesendTemplateMessage
service function.sendWhatsAppTemplate
mutation takes the necessary arguments.@requireAuth
(or a more specific roles-based directive) should be added to protect this mutation. We won't implement the auth logic itself in this guide, but it's crucial for production.Implement Mutation Resolver: RedwoodJS automatically maps the
sendWhatsAppTemplate
mutation to thesendTemplateMessage
function in theapi/src/services/infobip/infobip.ts
service because the names match. No extra resolver code is needed for this mapping.Testing the Mutation:
yarn rw dev
http://localhost:8911/graphql
).templateName
used below ("welcome_multiple_languages"
) is just an example. You must replace this with the exact name of an active, approved template associated with your Infobip sender number. Also, ensure thetemplateData
matches the placeholders defined in your specific template.4. Integrating with Infobip (Webhook for Inbound Messages/Reports)
To receive messages sent to your WhatsApp number or get delivery status updates, Infobip needs to send HTTP requests (webhooks) to your application. We'll use a RedwoodJS Function for this.
Generate Webhook Function:
This creates
api/src/functions/inboundWebhook.ts
.Implement Webhook Handler: Open
api/src/functions/inboundWebhook.ts
and add the handler logic:x-webhook-secret
). This is minimal security. For production, you must investigate Infobip's documentation for signature validation (e.g., HMAC) and implement that verification instead.results
array structure.from
,receivedAt
vs.sentAt
,status
). You MUST inspect actual payloads from Infobip and consult their documentation to adjust this logic accurately.// TODO:
) indicate where you'd add your application-specific logic (database interaction, notifications, etc.).200 OK
status code to Infobip upon successful processing, or appropriate error codes (401, 400, 500). Infobip generally expects a 2xx response to consider the webhook delivered.Expose Webhook Locally with ngrok: Infobip needs a publicly accessible URL to send webhooks to. During development,
ngrok
creates a secure tunnel to your local machine.yarn rw dev
(Note the API server port, usually 8911).ngrok
will display forwarding URLs (e.g.,https://<random-string>.ngrok.io
). Copy thehttps
URL.Configure Webhook in Infobip:
ngrok
HTTPS URL, appending the path to your Redwood function:/inboundWebhook
. So the full URL will be something like:https://<random-string>.ngrok.io/api/functions/inboundWebhook
.x-webhook-secret
(or your chosen name) with the value set to yourINFOBIP_WEBHOOK_SECRET
from the.env
file. If Infobip supports signature validation (HMAC), configure that instead/additionally following their documentation.Testing the Webhook:
yarn rw dev
and the terminal runningngrok
. You should see log output from yourinboundWebhook
function indicating it received and processed the message.5. Error Handling, Logging, and Retry Mechanisms
callInfobipApi
helper includes basictry...catch
blocks and checks the HTTP response status. Expand this to handle specific Infobip error codes or messages if needed (refer to Infobip API documentation for error details).try...catch
and returns appropriate HTTP status codes (4xx for client errors/bad data, 500 for server errors). Infobip might retry webhooks on receiving 5xx errors or timeouts.src/lib/logger
) is used extensively. Use different log levels (debug
,info
,warn
,error
) appropriately.callInfobipApi
function using libraries likeasync-retry
or a manual loop with exponential backoff. Be cautious not to retry excessively for errors that are unlikely to resolve (e.g., invalid API key, malformed request).6. Database Schema and Data Layer (Optional but Recommended)
Storing message logs helps with tracking, debugging, and analytics.
Define Prisma Schema: Open
api/db/schema.prisma
and add a model:Run Migration:
Update Services/Functions to Use DB:
api/src/services/infobip/infobip.ts
(sendTemplateMessage
), after a successful API call, create aMessageLog
entry:api/src/functions/inboundWebhook.ts
, inside the loop processing results: