Guides
Leads

Verify the signature

How to validate X-Hub-Signature-256 on Meta Ads webhooks, the exact bytes the signature covers and the callback verification.

Every Meta Ads webhook (page/leadgen and ad_account) is a POST signed with the app secret. Verify the signature before trusting the body.

Headers

HeaderValue
Content-Typeapplication/json
User-Agentfacebookexternalua
X-Hub-Signature-256sha256= + hex HMAC-SHA256 of the raw body, with the app secret as the key
X-Hub-Signaturesha1= + hex HMAC-SHA1 (legacy)

The signed bytes

The signature covers the body exactly as it arrived. Meta's body escapes non-ASCII characters as \uXXXX and the slash as \/:

json
{"object":"page","entry":[{"id":"100539998049709","time":1794028115,"changes":[{"field":"leadgen","value":{"leadgen_id":"100539998049743","page_id":"100539998049709","form_id":"100539998049736","created_time":1794028115}}]}]}

So compute the HMAC over the raw body bytes, never over JSON you parsed and serialized again: your serialization won't escape \/ and ã the same way, and the signature won't match.

Node.js

javascript
import { createHmac, timingSafeEqual } from "node:crypto";
import express from "express";

const app = express();
app.post("/meta/webhook", express.raw({ type: "application/json" }), (req, res) => {
  const header = req.get("X-Hub-Signature-256") ?? "";
  const expected = "sha256=" + createHmac("sha256", process.env.META_APP_SECRET).update(req.body).digest("hex");
  const ok = header.length === expected.length && timingSafeEqual(Buffer.from(header), Buffer.from(expected));
  if (!ok) return res.sendStatus(401);
  const event = JSON.parse(req.body.toString("utf8"));
  // queue the processing and answer right away
  res.sendStatus(200);
});

Python

python
import hashlib, hmac, os
from flask import Flask, request, abort

app = Flask(__name__)

@app.post("/meta/webhook")
def webhook():
    raw = request.get_data()
    expected = "sha256=" + hmac.new(os.environ["META_APP_SECRET"].encode(), raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(request.headers.get("X-Hub-Signature-256", ""), expected):
        abort(401)
    event = request.get_json()
    return "", 200

Callback verification

When subscribing (POST /{app_id}/subscriptions), Mimicr calls the callback once with GET:

text
GET https://app.example.com/meta/webhook?hub.mode=subscribe&hub.challenge=1158201444&hub.verify_token=meu-token

Check that hub.verify_token is yours and answer 200 with the value of hub.challenge in the body, as plain text. Any other response makes the subscription fail with (#2200) Callback verification failed.

javascript
app.get("/meta/webhook", (req, res) => {
  if (req.query["hub.mode"] === "subscribe" && req.query["hub.verify_token"] === "meu-token") return res.send(req.query["hub.challenge"]);
  res.sendStatus(403);
});

Retries and duplicates

Answer 2xx within 10 s and process later. Without a 2xx, the delivery is retried 30 s, 2 min, 10 min, 30 min, 1 h, 3 h, 6 h, 12 h, 24 h and 36 h after the first attempt. The same notification may arrive more than once (webhook chaos simulates this): deduplicate by leadgen_id.

To inspect the deliveries, with the attempts and the signature that was sent: GET /v1/meta-ads/deliveries or npx mimicr get /v1/meta-ads/deliveries. To send one again: POST /v1/meta-ads/deliveries/{id}/resend or the webhook.resend action.

When you rotate the app secret (PUT /v1/meta-ads/app with rotate_secret: true), the following deliveries already use the new one, retries included.