Receiving a webhook is easy. Processing it safely, handling duplicates, and recovering from failures is where production systems get interesting.
When integrating a messaging API, it's tempting to build a single endpoint that receives an event and immediately processes it.
That can work in a local demo. Under real traffic, however, slow database queries, duplicate events, and temporary failures can make the system unreliable.
Here's a practical approach.
1. Create a basic Express endpoint
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/whatsapp", async (req, res) => {
const event = req.body;
// Add provider-specific verification here.
// Validate the event before processing it.
try {
// Enqueue the event for asynchronous processing.
// Replace this placeholder with your queue implementation.
res.sendStatus(200);
} catch (error) {
console.error("Webhook enqueue failed");
res.sendStatus(500);
}
});
app.listen(3000, () => {
console.log("Server listening on port 3000");
});
This is a structural example, not a complete production implementation. The endpoint must perform actual verification, validation, and queue insertion before acknowledging an event.
2. Avoid processing everything inside the request
If an event triggers several database operations or external API calls, processing all of them inside the HTTP request can increase latency.
A better architecture is:
- Receive the webhook.
- Verify the request according to the provider's documented security mechanism.
- Validate the event structure.
- Store or enqueue the event durably.
- Return the appropriate HTTP response.
- Process the event asynchronously.
A durable queue or database-backed inbox helps protect events when workers temporarily fail.
3. Handle duplicate events
Webhook providers may retry delivery. Your application should not assume that every incoming event is unique.
Use stable event identifiers where available and enforce idempotency at the processing layer.
4. Add controlled retries
Retry transient errors with exponential backoff and jitter. Avoid endless retries for permanent errors, and use a dead-letter mechanism for events that repeatedly fail.
5. Monitor the complete pipeline
Useful metrics include processing latency, retry counts, queue depth, failed events, and delivery status.
These metrics help you distinguish provider delivery problems from failures inside your own application.
Further resources
If you're evaluating WhatsApp messaging integrations, review CampaigningTool's WhatsApp Business API information and its Postman API documentation.
Verify the actual endpoint definitions and authentication requirements in the documentation before implementing an integration.
Developer question: Do you prefer processing webhooks through a queue such as BullMQ, or using a database-backed job system?
Top comments (0)