Guides
Webhooks

Verify the signature

Confirm that each webhook came from the simulator by recomputing the HMAC-SHA256 of messageId + "+" + clientId (with a literal +).

Every delivery carries the X-Message-Signature header, an HMAC-SHA256 you recompute with your clientSecret. If it does not match, reject the request.

What is signed

text
signature = hex(HMAC-SHA256(key = clientSecret, data = messageId + "+" + clientId))
  • messageId comes from the X-Message-Id header.
  • clientId and clientSecret are your sandbox credentials (the same ones used for the token, see Authentication).

The signature covers the message id, not the body. Body integrity comes from TLS, which is why the endpoint must be HTTPS.

Code

python
import hmac
import hashlib

def verify_signature(message_id: str, client_id: str, client_secret: str, signature_header: str) -> bool:
    expected = hmac.new(
        key=client_secret.encode("utf-8"),
        msg=f"{message_id}+{client_id}".encode("utf-8"),
        digestmod=hashlib.sha256,
    ).hexdigest()
    return hmac.compare_digest(expected, signature_header)
javascript
import { createHmac, timingSafeEqual } from "node:crypto";

function verifySignature(messageId, clientId, clientSecret, signatureHeader) {
  const expected = createHmac("sha256", clientSecret)
    .update(`${messageId}+${clientId}`)
    .digest("hex");
  const a = Buffer.from(expected, "hex");
  const b = Buffer.from(signatureHeader ?? "", "hex");
  return a.length === b.length && timingSafeEqual(a, b);
}

Use a constant-time comparison (hmac.compare_digest, crypto.timingSafeEqual).

Handler example

javascript
app.post("/trace-webhooks", express.json(), async (req, res) => {
  const ok = verifySignature(
    req.get("X-Message-Id"),
    process.env.TRACE_CLIENT_ID,
    process.env.TRACE_CLIENT_SECRET,
    req.get("X-Message-Signature"),
  );
  if (!ok) return res.status(401).end();
  res.status(204).end();
  await queue.push({ messageId: req.get("X-Message-Id"), event: req.get("X-Event-Type"), body: req.body });
});

Separate environments

The signature depends on the clientSecret. Mimicr's credentials differ from Trace's, so keep one pair per environment: a Mimicr delivery does not validate with Trace's secret, and vice versa.

Check by hand

Every recorded delivery carries its messageId (in List execution logs or in the control API). To recompute a signature in a terminal:

bash
printf '%s+%s' "<message-id>" "<client-id>" | openssl dgst -sha256 -hmac "<client-secret>"