Guides
API principlesErrors
MimicrSandbox control
Verify the signature
Validate the X-Hub-Signature-256 of every webhook with the app secret, over the raw request body.
Every webhook carries X-Hub-Signature-256: sha256=<hex>, the HMAC-SHA256 of the raw request body keyed with the sandbox's app secret. Compute the same HMAC over the bytes you received (before parsing the JSON) and compare in constant time. Reject what doesn't match with 401.
Node.js (Express)
javascript
import crypto from "node:crypto";
import express from "express";
const app = express();
app.post("/webhooks/whatsapp", express.raw({ type: "application/json" }), (req, res) => {
const header = req.get("X-Hub-Signature-256") ?? "";
const expected = "sha256=" + crypto.createHmac("sha256", process.env.WHATSAPP_APP_SECRET).update(req.body).digest("hex");
const ok = header.length === expected.length && crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected));
if (!ok) return res.sendStatus(401);
res.sendStatus(200);
const payload = JSON.parse(req.body.toString("utf8"));
// process payload.entry[].changes[] asynchronously
});Python (Flask)
python
import hashlib
import hmac
import os
from flask import Flask, abort, request
app = Flask(__name__)
@app.post("/webhooks/whatsapp")
def webhook():
body = request.get_data()
expected = "sha256=" + hmac.new(os.environ["WHATSAPP_APP_SECRET"].encode(), body, hashlib.sha256).hexdigest()
if not hmac.compare_digest(request.headers.get("X-Hub-Signature-256", ""), expected):
abort(401)
payload = request.get_json()
return "", 200Pitfalls
- Use the raw body. Re-serializing the JSON changes whitespace and escapes (such as
áinstead ofá) and the signature stops matching. X-Hub-Signature(SHA-1) is sent too, but prefer SHA-256.- After rotating the app secret (
POST /v1/whatsapp/{slug}/app-secret/rotate), new webhooks are signed with the new secret; update your app's variable. - If verification fails, check that your app uses the app secret of the right sandbox: each sandbox has its own.