Browse and search developers
Webhooks
Verify webhook signatures
Check the X-Formit-Signature HMAC-SHA256 header against the raw request body and reject stale timestamps, with Node.js, Python and PHP code.
Anyone who knows your endpoint URL can send it a request. Verify the signature on every delivery before you trust the body. Formit signs each delivery attempt with the endpoint’s signing secret, a string that starts with whsec_.
The signature scheme
| Item | Value |
|---|---|
| Header | X-Formit-Signature |
| Header format | t=<unix_seconds>,v1=<hex> |
| Algorithm | HMAC-SHA256, hex-encoded (lowercase) |
| Signed payload | The timestamp, a literal ., then the raw request body: <t>.<rawBody> |
| Key | The full signing secret, including the whsec_ prefix |
| Timestamp header | X-Formit-Timestamp repeats the value of t |
| Tolerance | Reject requests whose t is more than 300 seconds from your server clock |
Verification steps
- Read the raw request body as bytes before any JSON parsing. A body that was parsed and re-serialised will not match.
- Split
X-Formit-Signatureon commas. Readtas an integer and collect everyv1value. - Reject the request if the absolute difference between
tand the current Unix time is greater than 300 seconds. - Compute HMAC-SHA256 of
t + "." + rawBodywith your secret and hex-encode it. - Compare the result with each
v1value using a constant-time comparison. Accept if any matches. - Return a 2xx status only after verification succeeds, and an error status otherwise.
server.js
// server.js (Express). Mount the raw parser on this route only:
// the signature covers the exact bytes Formit sent, so never verify a re-serialised object.
const crypto = require('node:crypto');
const express = require('express');
const app = express();
const SECRET = process.env.FORMIT_WEBHOOK_SECRET; // the full whsec_... value
const TOLERANCE_SECONDS = 300;
function verifyFormitSignature(rawBody, header, secret) {
if (!header) return false;
let timestamp = null;
const candidates = [];
for (const part of header.split(',')) {
const eq = part.indexOf('=');
if (eq < 0) continue;
const key = part.slice(0, eq).trim();
const value = part.slice(eq + 1).trim();
if (key === 't') timestamp = Number(value);
if (key === 'v1' && value) candidates.push(value);
}
if (!Number.isFinite(timestamp) || candidates.length === 0) return false;
const now = Math.floor(Date.now() / 1000);
if (Math.abs(now - timestamp) > TOLERANCE_SECONDS) return false;
const expected = crypto
.createHmac('sha256', secret)
.update(timestamp + '.' + rawBody, 'utf8')
.digest('hex');
const expectedBuf = Buffer.from(expected, 'utf8');
return candidates.some((candidate) => {
const candidateBuf = Buffer.from(candidate, 'utf8');
return (
candidateBuf.length === expectedBuf.length &&
crypto.timingSafeEqual(candidateBuf, expectedBuf)
);
});
}
app.post('/webhooks/formit', express.raw({ type: 'application/json' }), (req, res) => {
const rawBody = req.body.toString('utf8');
const ok = verifyFormitSignature(rawBody, req.get('X-Formit-Signature'), SECRET);
if (!ok) return res.status(400).send('invalid signature');
const event = JSON.parse(rawBody);
// Acknowledge fast, then do the slow work asynchronously.
res.status(200).json({ received: true });
handleEvent(event).catch(console.error);
});
app.listen(3000);Replay protection
The timestamp is part of the signed payload, so an attacker cannot change it without breaking the signature. Rejecting anything older than 300 seconds means a captured request cannot be replayed later. Formit signs each attempt at send time, so retries carry a fresh timestamp and still pass the window.
For stronger protection, record each X-Formit-Delivery-Id you have processed and skip repeats. This also makes retries idempotent, because a retry reuses the delivery id.
- Keep your server clock synced with NTP. Clock drift beyond 300 seconds causes valid requests to fail.
- Parse the header by key instead of by position, and accept several
v1values, which the Formit verifier also allows. - Never log the secret, and keep it out of source control. Load it from an environment variable or a secret manager.
- Use a separate endpoint and secret for each environment.
Rotating the secret
Rotate secret on the endpoint page replaces the secret immediately, and the old secret stops working at once. Deliveries signed after the rotation, including retries of older events, use the new secret. Update your receiver right after rotating, or expect failed attempts until you do.
Related
Can't find what you need?