Build a WhatsApp Messaging App with Fastify and Infobip - code-examples -

Frequently Asked Questions

Use the '/send' POST endpoint with a JSON body containing the recipient's number ('to') in E.164 format (e.g., +14155552671) and the message text ('text'). This endpoint leverages the Infobip API to deliver the message, simplifying WhatsApp integration within your Fastify app.
Infobip is a CPaaS (Communication Platform as a Service) that provides the necessary infrastructure and API for sending and receiving WhatsApp messages programmatically. The project uses Infobip's Node.js SDK to interact with their API, abstracting away the complexities of the WhatsApp Business API.
Fastify is a high-performance Node.js web framework known for its speed and developer-friendly features like built-in logging and validation. Its efficiency makes it well-suited for handling real-time messaging traffic and building scalable WhatsApp applications.
Ngrok is useful during local development to create a publicly accessible URL for receiving Infobip webhooks. In production, you'll need a properly deployed server with a public domain or IP and HTTPS.
Yes, besides text messages, Infobip supports various message types like images, documents, templates, and interactive buttons. Refer to the Infobip WhatsApp API documentation for details on structuring the payload for each type and adapting the webhook handler to process them.
Set up a '/infobip-webhook' POST route in your Fastify app. Configure this URL as a webhook in your Infobip account settings, and Infobip will forward incoming WhatsApp messages to this endpoint. Add security measures like secret verification to protect your webhook.
You'll need Node.js v14 or later, an active Infobip account (a free trial is sufficient for testing), your Infobip API Key and Base URL, a registered WhatsApp sender in your Infobip account, and a public URL for receiving webhooks.
Store your Infobip API key as an environment variable in a '.env' file, and add this file to your '.gitignore' to prevent it from being committed to version control. For production, consider using more secure secret management solutions.
A suggested schema includes fields for message ID, direction, sender/recipient numbers, message body, status, timestamps, and Infobip-specific status details (stored as JSON). Use indexing for optimal querying by numbers or status.
Validating the 'to' field ensures that only correctly formatted numbers (E.164 format) are sent to the Infobip API, minimizing errors and unnecessary API calls.
Implement try...catch blocks around API calls to handle network errors or issues returned by Infobip. Log errors with relevant context and return informative error messages to the client with appropriate HTTP status codes.
The webhook secret is used to verify that incoming requests to your '/infobip-webhook' endpoint are actually coming from Infobip. This adds a basic security layer to prevent unauthorized access. While a secret is helpful, prioritize signature verification if Infobip offers it.
Use the 'fastify-rate-limit' plugin to control the rate of incoming requests, protecting your application from abuse. Configure the maximum number of requests allowed within a specified time window. This is especially important for the /send endpoint.
Leverage Fastify's inherent performance, ensure asynchronous operations are non-blocking, optimize database interactions, and keep request/response payloads concise. For high-throughput, conduct load testing to identify potential bottlenecks.
Implement a '/health' endpoint that returns the application's status. Integrate logging and metrics collection using tools like Pino and Prometheus, and set up error tracking with services like Sentry or Bugsnag.