Skip to content

Signing events

Every POST /v1/events carries two proofs: the API key in Authorization says whose request it is, and the signature says the body was not changed on the way. The signature follows the open Standard Webhooks scheme, the same one we use to sign webhooks.

Header What it holds
webhook-id the sending ID; passing event_id is convenient
webhook-timestamp sending time, Unix seconds
webhook-signature v1, and the base64 signature

The signature is HMAC-SHA256 of the string {webhook-id}.{webhook-timestamp}.{body}:

  • Key: the event signing secret (whsec_…), the part after whsec_, decoded from base64.
  • Body: exactly the bytes sent in the request. Serialize JSON once, sign that string and send the same string. If your HTTP client serializes the object itself, the signature will not match.
  • Time: the sending time, not occurred_at. Sign a retry an hour later anew, with a new timestamp.
// app/lib/aapp.ts: only on the server, never in a client component
import { createHmac } from 'node:crypto';
const API = 'https://test-api.zirva.xyz';
export async function sendEvent(event: Record<string, unknown> & { event_id: string }) {
const body = JSON.stringify(event);
const timestamp = Math.floor(Date.now() / 1000).toString();
const key = Buffer.from(process.env.AAPP_SIGNING_SECRET!.slice('whsec_'.length), 'base64');
const signature = createHmac('sha256', key)
.update(`${event.event_id}.${timestamp}.${body}`)
.digest('base64');
const res = await fetch(`${API}/v1/events`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.AAPP_API_KEY}`,
'Content-Type': 'application/json',
'webhook-id': event.event_id,
'webhook-timestamp': timestamp,
'webhook-signature': `v1,${signature}`,
},
body,
signal: AbortSignal.timeout(10_000),
});
return { status: res.status, body: await res.json() };
}
await sendEvent({
event_id: 'evt_90114',
type: 'order.paid',
user_ref: '4f9a',
order_id: 'ord_78123',
occurred_at: new Date().toISOString(),
amount: 25.0,
currency: 'AZN',
items: [{ sku: 'game-123', category: 'rpg', price: 25.0, qty: 1 }],
});

All four examples were checked against the official Standard Webhooks library. You can use the library too: Webhook.sign(id, timestamp, body) produces the same signature.

Response What it means What to do
401 invalid_api_key the API key is missing, wrong or revoked check the Authorization: Bearer aapp_… header and the key’s mode
401 invalid_signature the signature did not match check that you signed the same bytes you sent, with the secret of the same mode as the key
401 timestamp_out_of_tolerance the timestamp is more than 5 minutes from our time sync the server clock (NTP) and sign right before sending

An event with a signature error does not enter the log. After fixing it, send it again with the same event_id.

You can rotate the secret without downtime. We issue a new secret, and for a while both are valid. During that time, send two signatures separated by a space: v1,<signed with old> v1,<signed with new>. Any valid one is enough. Once the new secret is on all your servers, ask us to revoke the old one.