NewWhatsApp, Instagram, Messenger, email, SMS and website chat, now in one inbox
CAT Engage 360

Verify signatures

Anyone can POST to a public address. The signature proves a delivery came from CAT Engage, wasn't changed on the way, and isn't an old one being replayed.

The header#

Header
X-Cat-Signature: t=1795775538,v1=5f2c9b1e0d7a…a3e41c
  • t is when the delivery was signed, in Unix seconds. Every attempt is signed afresh, so a retry has a new t.
  • v1 is the HMAC-SHA256, in hex, of t, a dot and the raw body, keyed with your endpoint's secret.

Check it in three steps#

  1. Read the raw body

    Use the exact bytes you received. Parsing the JSON and turning it back into text changes spacing and breaks the check.

  2. Recompute the signature

    HMAC-SHA256 of ${t}.${rawBody} with your secret, as lowercase hex, then compare it with v1 in constant time.

  3. Reject old deliveries

    Refuse it if t is more than 5 minutes from your clock. Keep your server's clock in sync.

Code#

import crypto from 'node:crypto';
import express from 'express';

const app = express();
const SECRET = process.env.CAT_WEBHOOK_SECRET;

// Keep the raw body: the signature is over the exact bytes we sent.
app.post('/cat-webhooks', express.raw({ type: 'application/json' }), (req, res) => {
  const raw = req.body.toString('utf8');
  if (!verify(req.get('X-Cat-Signature') ?? '', raw, SECRET)) return res.sendStatus(401);

  const event = JSON.parse(raw);
  res.sendStatus(200); // answer first, then do the work
  handle(event);
});

function verify(header, raw, secret, toleranceSeconds = 300) {
  const parts = Object.fromEntries(
    header.split(',').map((kv) => {
      const i = kv.indexOf('=');
      return [kv.slice(0, i).trim(), kv.slice(i + 1).trim()];
    }),
  );
  const t = Number(parts.t);
  if (!Number.isFinite(t) || !parts.v1) return false;
  if (Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false; // too old: replay

  const expected = crypto.createHmac('sha256', secret).update(`${t}.${raw}`).digest('hex');
  const a = Buffer.from(expected);
  const b = Buffer.from(parts.v1.toLowerCase());
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

Your secret#

Each endpoint has its own secret. It is shown once: when you add the endpoint in Integrations → Webhooks, or in the secret field when you create it through the API. We keep it encrypted and never show it again.

Lost the secret?

Delete the endpoint and add it again to get a new secret, then update your server.