Skip to Content
Developer docs

Verify signatures

Check that an event really comes from Meishi and was not replayed.

Each event carries a Meishi-Signature header:

Meishi-Signature: t=1762773241,v1=5d2f8c0e…
  • t is when it was sent, in Unix seconds.
  • v1 is the hex HMAC-SHA256 of t, a full stop and the raw request body, made with your signing secret.

To check it:

  1. Read the raw body, before any JSON parsing (which changes the bytes).
  2. Compute HMAC-SHA256(secret, t + "." + rawBody) as hex.
  3. Compare it with v1 in constant time.
  4. Turn away events whose t is more than 5 minutes from now, so an old event cannot be replayed.
// Express. The signature covers the exact bytes Meishi sent, so read the raw body.
import crypto from 'node:crypto';
import express from 'express';

const SECRET = process.env.MEISHI_WEBHOOK_SECRET; // the signing secret
const app = express();

app.post('/your-meishi-webhook', express.raw({ type: 'application/json' }), (req, res) => {
  const header = req.get('Meishi-Signature') ?? '';
  const { t, v1 } = Object.fromEntries(header.split(',').map(part => part.split('=')));
  const expected = crypto.createHmac('sha256', SECRET).update(`${t}.${req.body}`).digest('hex');
  const fresh = Math.abs(Date.now() / 1000 - Number(t)) < 300;
  const matches =
    typeof v1 === 'string' &&
    v1.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
  if (!fresh || !matches) return res.status(400).send('bad signature');

  const event = JSON.parse(req.body);
  // A repeated event keeps its id: handle each id once.
  if (event.type === 'experience.ready') {
    // event.data is the experience: scanUrl, qrSvgUrl, printPdfUrl, orderReference…
  }
  res.sendStatus(200);
});

Replacing the signing secret in the portal stops the old one at once. Deploy the new secret to your server first, then replace it, to lose no events.