← Back to Blog

Handling Real-Time WhatsApp Webhooks in Express.js

By 5MinutesAPI Engineering

Why Webhooks Are Essential

When you send a message via 5MinutesAPI, our Golang engine dispatches it to Meta in milliseconds. But to know if the user read it, or if they replied, your server needs to listen for webhooks. Webhooks allow your system to react in real-time to inbound customer data.

Setting Up the Express Endpoint

In your Node.js application, you simply need to expose a POST endpoint. 5MinutesAPI will forward all Meta events directly to this URL.



const express = require('express');

const app = express();
app.use(express.json());

app.post('/webhook/whatsapp', (req, res) => {
const { type, data } = req.body;

if (type === 'message_received') {
console.log("New message from:", data.from, "Text:", data.text);
// Trigger chatbot or route to human agent
} else if (type === 'message_status') {
console.log("Message ID:", data.id, "Status:", data.status);
// Update DB (Sent, Delivered, Read, Failed)
}

// Always return 200 OK immediately
res.status(200).send('OK');
});

app.listen(3000, () => console.log('Webhook listening on port 3000'));

Performance Tip

Always return a 200 OK HTTP status *before* performing heavy database operations. If you block the response, our webhook dispatcher might assume your server is down and retry the request.

Ready to upgrade your infrastructure?

Start Building with 5MinutesAPI