How do I verify a webhook signature?
Published December 5, 2026 · Updated December 5, 2026 · Mirage Browser
Recompute the HMAC of the request with your shared secret and compare it to the signature header in constant time. Use the raw body exactly as it arrived, not parsed and re-serialized JSON. Reject requests whose timestamp is too old, and ignore an event id you have already processed, because senders retry.
Why check a signature at all?
A webhook address is just a URL. Anyone who learns it can post to it. Without a check, a fake “profile crashed” or “scenario succeeded” event can trigger whatever your workflow does next. A signature proves the request came from someone who holds the secret and that the body was not changed on the way.
How does an HMAC signature work?
HMAC combines a secret key with a hash function, such as SHA-256, to produce a code for a message. The sender computes it over the message and sends it in a header. The receiver, holding the same secret, computes it again. If the two match, the message is authentic. The secret itself never travels with the request.
Most services sign more than the body. Mirage signs the timestamp, a dot and the raw body, and sends t= and v1= in the mirage-signature header. Including the time stops an attacker from replaying an old request later.
What are the steps?
- Read the raw request body as bytes, before any JSON parsing.
- Split the header into the timestamp and the signature.
- Reject the request if the timestamp is far from the current time, for example more than five minutes.
- Compute HMAC-SHA256 of
timestamp + "." + bodywith your secret. - Compare with the signature using a constant-time comparison.
- Only then parse the JSON and act on it.
What does it look like in code?
With the Mirage SDK the steps are one call:
import { verifyWebhook } from "@mirage-browser/sdk";
const event = verifyWebhook(process.env.MIRAGE_WEBHOOK_SECRET!, rawBody, req.headers["mirage-signature"]);from mirage_sdk import verify_webhook
event = verify_webhook(os.environ["MIRAGE_WEBHOOK_SECRET"], request.body, request.headers["mirage-signature"])Both throw if the signature is wrong or too old.
Why does my check keep failing?
| Mistake | Fix |
|---|---|
| Body parsed and serialized again | Spaces and key order change, so hash the raw bytes |
| Secret with a trailing newline | Trim the value when loading it |
Plain == comparison |
Works, but leaks timing; use a constant-time compare |
| Clock on the server is off | Sync the time with NTP |
In n8n, turn on Raw Body in the Webhook node, then compute the HMAC with a Crypto node and compare it in an IF node.
What about retries?
A sender that gets no 2xx answer sends the same event again. Mirage retries for about a day with the same id. Answer quickly, do the slow work afterwards, and keep a list of processed ids so a retry does not run your workflow twice.
Event list and setup: webhooks and Telegram bot.