Webhooks
The messages that Grail Network sends to your server, and how to check them.
The message
A webhook is a POST request with a JSON body. It is thin: it says what changed, and your system reads the API for
the facts.
{
"id": "019a8c20-11aa-7bcd-8e00-3f4a5b6c7d8e",
"type": "partner-item.changed",
"createdAt": "2026-11-25T10:00:01.250Z",
"data": { "itemId": "019a8b90-0c1d-7e2f-8a3b-4c5d6e7f8091" }
}The signature
Grail Network signs each webhook by the Standard Webhooks specification (the one of Svix). Each request has three headers:
| Header | Value |
|---|---|
webhook-id | The id of the message. It stays the same when Grail Network sends the message again |
webhook-timestamp | The time of this try, in seconds since 1970 |
webhook-signature | v1, and the base64 of an HMAC-SHA256 |
Your webhook secret looks like whsec_ and base64 text. The key of the HMAC is the bytes of the base64 after
whsec_. The signed text is <webhook-id>.<webhook-timestamp>.<raw body>.
To check a webhook:
- Read the raw body as bytes, before any JSON parser changes it.
- Compute the signature with your secret, and compare it with the header in constant time.
- Refuse a message whose
webhook-timestampis more than 5 minutes from your clock. - Answer
2xxat once. Do the work after the answer.
import { createHmac, timingSafeEqual } from 'node:crypto';
export function isValidWebhook(secret, headers, rawBody) {
const id = headers['webhook-id'];
const timestamp = headers['webhook-timestamp'];
const signatures = headers['webhook-signature'];
if (!id || !timestamp || !signatures) return false;
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 5 * 60) return false;
const key = Buffer.from(secret.replace(/^whsec_/, ''), 'base64');
const expected = createHmac('sha256', key).update(`${id}.${timestamp}.`).update(rawBody).digest();
// The header may hold several signatures, separated by spaces.
return signatures.split(' ').some((entry) => {
const [version, value] = entry.split(',');
const given = Buffer.from(value ?? '', 'base64');
return version === 'v1' && given.length === expected.length && timingSafeEqual(given, expected);
});
}Any library of Standard Webhooks works too.
Order and copies
- The same message can arrive twice. Keep the
webhook-idof each message that you handled, and skip a copy. - Messages can arrive in another order than they happened. Read the API for the current state.
Your webhook address
A user of your shop sets the address and makes the secret in the shop back office. The secret is shown once. Your server's API key cannot read or change them. With no address, Grail Network sends your shop no webhook.
The types
type | data | When | What to read |
|---|---|---|---|
shipment.left | { orderId, shipmentId } | A shipment was handed to FedEx | GET /shop/orders/{orderId} |
shipment.item-canceled | { orderId, shipmentId } | Grail Network's staff canceled one card of a shipment | GET /shop/orders/{orderId} |
Accept a type that you do not know, and answer it with 2xx.
A pull, a sale back and an order send no webhook: the answer of the request is the result.
The tries
- A try fails on any answer other than
2xx, or on no answer within 10 seconds. - After a failed try, Grail Network waits 1 minute, 5 minutes, 30 minutes, 2 hours, 5 hours, 10 hours and 10 hours. The last of 8 tries is about 27.6 hours after the first. Then Grail Network stops trying this message.
- Every try of one message has the same
webhook-id.
A webhook only tells your server to read the API. After an outage, read your orders with GET /shop/orders.