Skip to Content
Grail Network

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:

HeaderValue
webhook-idThe id of the message. It stays the same when Grail Network sends the message again
webhook-timestampThe time of this try, in seconds since 1970
webhook-signaturev1, 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:

  1. Read the raw body as bytes, before any JSON parser changes it.
  2. Compute the signature with your secret, and compare it with the header in constant time.
  3. Refuse a message whose webhook-timestamp is more than 5 minutes from your clock.
  4. Answer 2xx at once. Do the work after the answer.
check-webhook.js
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-id of 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

Set it with PUT /partner/webhook, read it with GET /partner/webhook, and make a new secret with POST /partner/webhook/secret. See Getting started. With no address, Grail Network sends you no webhook.

The types

typedataWhenWhat to read
partner-item.changed{ itemId, reference }A card changed: its intake, its price, its state, a take-back request or its endGET /partner/items/{itemId}, or GET /partner/items?changedAfter=
intake.ended{ intakeId, reference }An intake of your slip endedGET /partner/intakes?reference=, then GET /partner/items?reference=
pool.created{ poolDay }, such as "2026-11-23"A pool gave you at least one shareGET /partner/report-lines?after=

Accept a type that you do not know, and answer it with 2xx.

A webhook is a nudge

A webhook only tells your system to read the API now. Your system never depends on it: after an outage, it reads GET /partner/report-lines?after= and GET /partner/items?changedAfter= from its last cursors, and gets everything that it missed. No route sends a webhook again.

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 card that your system cannot place

Sometimes a webhook names a card or a reference that your system does not know yet, for example a card of a parcel that a consignor sent with a wrong submission. Answer 2xx after the signature check anyway. Keep the card in a place of your own for unknown cards, and read the API. Any other answer makes Grail Network try the message again for about 27.6 hours, and the next tries cannot place it either.

On this page