Building a Production-Ready Sinch WhatsApp Integration with Fastify and Node.js - code-examples -

Frequently Asked Questions

Use the `sendWhatsAppTextMessage` function in the provided `sinch.ts` service file. This function utilizes the Sinch Conversation API to send text messages via WhatsApp. It requires the recipient's phone number in E.164 format and the message content as input. The function handles the interaction with the Sinch SDK and returns the Sinch message ID upon successful delivery.
The Sinch Conversation API is a unified messaging API that allows developers to integrate various messaging channels, including WhatsApp, into their applications. It simplifies the process of sending and receiving messages, managing contacts, and handling other communication tasks, providing a more streamlined experience than directly integrating with each messaging platform's API.
Fastify is a high-performance, low-overhead web framework for Node.js. Its speed and efficiency make it well-suited for handling real-time communication, like receiving webhook notifications from Sinch. It offers a robust routing system for managing incoming messages and a flexible plugin system for extensibility.
ngrok is primarily used during development to create a publicly accessible URL for your local Fastify server. This allows Sinch to send webhook notifications to your application even while it's running locally. Consider alternatives or paid tiers for production for more stable URLs.
Yes, the Sinch Conversation API supports various message types, including text messages, images, and interactive templates. The provided example focuses on text messages, but you can extend the `sinch.ts` service file to include functions for sending other message types using the appropriate Sinch SDK methods.
In your Sinch app dashboard, go to "Conversation API" -> "Webhook Settings". Enter your public URL (e.g., ngrok URL) followed by `/webhooks/inbound` for incoming messages and `/webhooks/status` for delivery reports. Select the relevant event types (`MESSAGE_INBOUND`, `MESSAGE_DELIVERY`) for each endpoint.
The `waid:` prefix is a specific identifier used by the Sinch Conversation API to denote WhatsApp contacts. It's followed by the recipient's phone number in E.164 format (e.g., waid:12223334444). It ensures that the message is routed correctly through the Sinch system to the intended WhatsApp user.
Webhook security is paramount. Use the `verifySinchSignature` function in the example code. This function compares the signature provided by Sinch in the `x-sinch-signature` header with your calculated signature using the raw request body, timestamp, and your Sinch "Application Secret." This prevents unauthorized requests.
The `x-sinch-signature` header contains a cryptographic signature generated by Sinch for each webhook request. This signature, along with the `x-sinch-timestamp` header, allows your application to verify the authenticity of the webhook and ensure it originated from Sinch, preventing malicious actors from sending fake requests.
The provided code includes a global error handler in `server.ts` to catch unhandled exceptions within routes. Specific error handling within the webhook route and the `sendWhatsAppTextMessage` function provides contextual error logging and responses. Always respond with a 200 OK to Sinch webhooks even if internal processing fails to prevent retries.
The Sinch webhook signature verification process typically uses the *raw*, unparsed request body as part of the signed data. Accessing this raw body is crucial for verifying the integrity of the request. The provided Fastify server setup includes instructions and examples to ensure `request.rawBody` is populated correctly for use in signature verification. This can be accomplished using middleware or adjusting content parser settings
`SINCH_PROJECT_ID`, `SINCH_KEY_ID`, `SINCH_KEY_SECRET`, `SINCH_APP_ID`, `SINCH_APPLICATION_SECRET` (for webhooks), and `WHATSAPP_SENDER_ID` (your provisioned number). These are loaded from the `.env` file in the provided example, keeping credentials secure.
The article recommends a modular structure. Create a dedicated service file (`sinch.ts`) to encapsulate interactions with the Sinch SDK. Define separate route handlers in Fastify (`routes/webhooks.ts`) to manage incoming webhooks and outgoing message requests. Use environment variables (`.env`) for configuration.