How to Verify Webhooks and Handle Retries¶
A webhook is the simplest way to keep systems in step: when an order is placed, the backend calls your server, and your server sends the confirmation email, updates the search index or tells the warehouse. Writing the endpoint takes ten minutes. Writing one that is safe and correct takes four more rules, and most receivers miss at least one.
This post walks through them with ObjectsGrid webhooks as the example; the same rules apply to any provider.
1. Check who is calling¶
Your endpoint is a public URL. Anyone who finds it can post a fake OrderCreated and have your warehouse ship
goods that were never paid for. So every delivery must prove where it came from.
ObjectsGrid signs each delivery: the request carries a JWT in Authorization: Bearer, signed with RS256. Verify four
things before you trust the body:
- The signature, with the public key from the published
webhook JWKS whose
kidmatches the token's header. - The issuer:
issishttps://objectsgrid.com. - The audience:
audis your Organization Account id, so a token meant for someone else's endpoint is refused at yours. - The expiry: tokens expire five minutes after they are issued, which limits how long a captured delivery can be replayed.
A library does all four in one call. In Node.js with jose:
import { createRemoteJWKSet, jwtVerify } from "jose";
const JWKS = createRemoteJWKSet(
new URL("https://objectsgrid.com/.well-known/objects-catalog/Webhook/jwks.json"),
);
async function verify(req) {
const token = (req.headers.authorization || "").replace(/^Bearer /, "");
await jwtVerify(token, JWKS, {
issuer: "https://objectsgrid.com",
audience: "org-123-4567", // your Organization Account id
algorithms: ["RS256"],
});
}
Fetch the keys from the JWKS rather than pasting one into your code: the JWKS always has the current keys, and the
kid picks the right one. Pin the algorithm, so a token can't choose a weaker one.
2. Answer first, work later¶
A delivery that isn't answered with a 2xx in time counts as failed and is sent again. If your handler sends an
email, calls a payment provider and writes to three databases before it answers, a slow day turns into timeouts,
the timeouts into redeliveries, and the redeliveries into more load.
So answer 204 as soon as the event is verified and safely stored, in a queue or a table, and do the work from
there.
3. Expect duplicates¶
Webhook delivery is at least once: when the answer to a delivery is lost, the sender can't know whether you got it, so it sends it again. Every receiver will see some events twice.
Make processing idempotent: each ObjectsGrid event has an id that is unique per event and the same on every
redelivery. Record the ids you have processed, and skip an event whose id you already have. A unique index on the id
column does it in one line of SQL.
4. Expect events out of order¶
Retries and parallel delivery mean ProductPatched can arrive before the ProductUpdated that happened earlier. If
you copy data into your own store as it arrives, an old version can overwrite a newer one.
Each ObjectsGrid event carries objectversion, which grows with every change of the object. Keep the last version
you applied per object, compare as numbers, and ignore an event with an older one:
UPDATE products
SET data = :data, version = :objectversion
WHERE id = :id AND version < :objectversion;
The whole receiver¶
app.post("/objectsgrid/products", async (req, res) => {
try {
await verify(req); // 1. who is calling
} catch {
return res.sendStatus(401);
}
await queue.add(req.body); // store it
res.sendStatus(204); // 2. answer first
});
queue.process(async (event) => {
if (await seen(event.id)) return; // 3. duplicates
await applyIfNewer(event.data.id, event.objectversion, event.data); // 4. order
await markSeen(event.id);
});
Two more things worth knowing¶
- Deliveries respect your access control. ObjectsGrid filters each delivery with the permissions of the account that last changed the webhook configuration, so an endpoint never receives objects or fields that account can't read. Set webhooks up with an account that can see what the endpoint needs, and nothing more.
- Not every event is about an object.
MonthlyQuotaApproachingandMonthlyQuotaReachedtell you when your spending cap is near, for example to alert your team or raise it automatically.
Events are CloudEvents 1.0 JSON, and new attributes may be added over time: ignore the ones you don't know, and your receiver keeps working. The webhooks guide has the full payload and configuration.