Skip to content

Receive webhooks

View as Markdown

Your endpoint must do four things on every delivery: verify the signature over the raw body, reject old timestamps, skip events it already processed, and answer 2xx within 10 seconds. The examples below do exactly that.

The signature is sha256= followed by the hex HMAC-SHA256 of the timestamp, a dot and the body:

X-Goldmaster-Signature = "sha256=" + hex(HMAC_SHA256(secret, X-Goldmaster-Timestamp + "." + rawBody))
import crypto from 'node:crypto';
import express from 'express';
const app = express();
const secret = process.env.DUTY_WEBHOOK_SECRET;
const seen = new Set(); // use your database in production
app.post('/hooks/duty', express.raw({ type: 'application/json' }), (req, res) => {
const timestamp = req.get('X-Goldmaster-Timestamp') ?? '';
const signature = req.get('X-Goldmaster-Signature') ?? '';
const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(`${timestamp}.`).update(req.body).digest('hex');
const valid = signature.length === expected.length && crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) <= 300;
if (!valid || !fresh) return res.sendStatus(401);
const key = req.get('Idempotency-Key');
if (seen.has(key)) return res.sendStatus(200);
seen.add(key);
const event = JSON.parse(req.body.toString('utf8'));
queue.push(event); // process after answering
res.sendStatus(200);
});
  1. Expose your endpoint over HTTPS with a public address (in development, a tunnel such as cloudflared or ngrok).
  2. Create a webhook pointing to it and keep the secret.
  3. Open a request or comment on one in the portal. Within a few seconds the delivery reaches you.
  4. Check what happened with List deliveries: status, attempts, the response code you returned and the last error.