Why payments need webhooks
A payment does not end when the API call returns. A live provider accepts a payout and confirms it later. A settled bank transfer can come back days afterwards as a return. A person finishes identity verification whenever they get round to it. Webhooks are how Rey tells your system that something changed, without you polling for it.
They are also an open door into your system. Anyone who knows the URL can POST to it, so the design has to answer two questions. Did this really come from Rey? And what happens when delivery fails?
Proving it came from Rey
Every endpoint gets its own signing secret (whsec_…, shown once). Every delivery carries a Rey-Signature header with a timestamp and a signature:
Rey-Signature: t=1764072000,v1=5f2b…c9e1v1 is the hex HMAC-SHA256 of "<t>.<raw body>", keyed with the endpoint secret. Three details make the difference between a check that works and one that only looks like it does:
- Sign the raw bytes. Verify against the request body exactly as it arrived, before JSON parsing. Re-serialising a parsed object changes whitespace and key order, and the signature stops matching.
- Compare in constant time. Use
timingSafeEqualor your language's equivalent. An ordinary string comparison can leak, through timing, how many leading characters matched. - Check the timestamp. The timestamp is inside the signed string, so it cannot be altered. Reject anything older than five minutes and a captured request cannot be replayed later.
import { createHmac, timingSafeEqual } from "node:crypto";
export function verifyReySignature(secret, rawBody, header, toleranceSeconds = 300) {
const parts = Object.fromEntries(
header.split(",").map((part) => {
const [key, ...rest] = part.split("=");
return [key.trim(), rest.join("=")];
}),
);
const timestamp = Number(parts.t);
if (!Number.isFinite(timestamp) || !parts.v1) return false;
if (Math.abs(Date.now() / 1000 - timestamp) > toleranceSeconds) return false;
const expected = createHmac("sha256", secret).update(`${timestamp}.${rawBody}`).digest();
const received = Buffer.from(parts.v1, "hex");
return received.length === expected.length && timingSafeEqual(received, expected);
}When delivery fails
Endpoints go down. Deploys restart servers. Rey treats a delivery as done only when your endpoint answers with a 2xx within ten seconds. Network errors, timeouts, 429 and 5xx are retried on a schedule that front-loads the quick wins and then backs off:
| Attempt | When |
|---|---|
| 1 | Immediately |
| 2 | 2 seconds later |
| 3 | 8 seconds after that |
| 4 | 10 minutes later |
| 5 | 1 hour later |
| 6 | 6 hours later |
| 7 | 24 hours later |
The first three run straight after the event, which covers most blips: a cold start, a dropped connection. The later four are scheduled in the database and sent by a background job, so they survive restarts and deploys on our side. Each attempt is signed afresh with a new timestamp, so a retry a day later still passes the five-minute window.
Any other non-2xx answer, such as a 400 or 401, is treated as a deliberate rejection and not retried. Redirects are not followed. Every attempt and its response status is logged in the console under Developers, so you can see exactly what Rey sent and what came back.
Receiving well
- Expect duplicates. Retries mean the same event can arrive after you already handled it. Store processed event
ids and skip repeats. This is the same idea as idempotency keys, pointed the other way. - Acknowledge fast, work later. Verify, record the event and return
204. Do the slow work in a queue, well inside the ten seconds. - Do not trust order. A retried
payment.succeededcan land after a newer event. Act on the state indata, or re-read the payment. - Reconcile. If you ever suspect a gap,
GET /payments/:idandGET /identities/:idare the source of truth. - Rotate secrets by adding a second endpoint with a new secret, switching over, then deleting the old one.
The events you will see
- payment.processing
- A live provider accepted the payout; the result follows.
- payment.succeeded
- The payment paid out.
- payment.failed
- Every attempt failed, or the rail rejected an accepted payout.
- payment.returned
- A settled payout came back from the beneficiary's bank.
- payment.canceled
- Canceled before the provider sent it; the full amount is back in your balance.
- identity.*
created,approved,declinedandrevoked, covered in Verify once.
The full payload shapes are in the webhooks reference.



