Security
Signature Verification
Header
Description
How It Works
Node.js Example
const crypto = require('crypto');
function verifyWebhook(rawBody, signature, timestamp, secret, toleranceSeconds = 300) {
// Reject stale deliveries
const now = Math.floor(Date.now() / 1000);
if (Math.abs(now - Number(timestamp)) > toleranceSeconds) return false;
const expected = 'sha256=' + crypto
.createHmac('sha256', secret)
.update(`${timestamp}.${rawBody}`, 'utf8')
.digest('hex');
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
}
// Express middleware (note: raw body is required)
app.post('/webhooks', express.raw({ type: 'application/json' }), (req, res) => {
const ok = verifyWebhook(
req.body,
req.headers['x-tokenbot-signature'],
req.headers['x-tokenbot-timestamp'],
process.env.WEBHOOK_SECRET
);
if (!ok) return res.status(401).send('Invalid signature');
const event = JSON.parse(req.body);
// Process event...
res.status(200).send('OK');
});Python Example
Secrets
Best Practices
Last updated
Was this helpful?