Checkout hooks
A checkout hook is an HTTPS address of yours that Webround calls during checkout to change the cart, apply a discount, change shipping or block the purchase.
Webround is always the one that calculates the totals. A hook never says "the total is X": it says what to do (operations), and Webround applies them to the net amount.
Kinds
A hook has one kind, which never changes. A store can have several hooks.
| Kind | When it runs | What it receives (data) | What it can do (operations) |
|---|---|---|---|
cart-transform | In a chain, first | items | add_line, remove_line, set_quantity, set_unit_price, set_data |
discount | In parallel, after promotions | items, shipping.options, appliedPromotions | percentage, fixed_amount, free_shipping, set_data |
shipping | In parallel | items, deliveryMethods, standardOptions | rename_option, set_option_amount, set_data |
validation | In parallel | items, totals | block, set_data |
An operation of another kind makes the whole response invalid.
Every time the checkout is calculated, Webround works in this order: the lines from the catalog with their checks (stock, quantity, currency, delivery), the VAT of each line, the cart-transform hooks one after the other, standard shipping, Webround's promotion codes, then the discount, shipping and validation hooks together, all on the same state. It applies their answers (shipping, then discounts, then validations) and finally calculates the totals. Webround's own logic always runs first: a discount hook already sees the promotions applied.
Hooks run in order of position, from the lowest; those without one come after, by creation date. cart-transform hooks run one at a time, each on the cart the previous one changed.
Create a hook
Hooks are managed with the Commerce API (/checkout-hooks), with the checkout-hook permission (read, create, update, delete).
| Field | Type | Rule |
|---|---|---|
kind | cart-transform, discount, shipping, validation | Required. It cannot be changed afterwards. |
name | string | 1 to 100 characters. |
url | string | HTTPS, up to 2048 characters. |
timeout | integer, ms | 500 to 8000. Default 3000. |
onError | passthrough, abort | What happens if the hook fails. Default passthrough. |
position | integer or null | 0 to 100000. null puts it last. |
active | boolean | Default true. |
appId | UUID or null | The app the hook belongs to. |
Webround generates a secret when you create the hook and shows it once: keep it, it signs every call. It cannot be rotated: to change it, delete the hook and create a new one. A change to a hook (new, changed URL, deactivated) reaches the checkout within 30 seconds.
How many hooks a store can have depends on the plan:
| Plan | In total | For each kind |
|---|---|---|
free | 2 | 1 |
start | 6 | 3 |
grow, scale | 12 | 5 |
Above the limit the answer is 429 DATA_LIMIT_REACHED.
The request
Webround sends POST <url> with Content-Type: application/json and X-WR-Checkout-Hook-Version: 2:
{
"version": 2,
"kind": "discount",
"phase": "quote", // "quote" (read only) or "checkout" (payment)
"storeId": "…",
"timestamp": 1760000000000, // ms since the Unix epoch
"currencyCode": "EUR",
"langCode": "en",
"customer": { "authenticated": true, "id": "…", "email": "…", "vatCode": "…", "vatExempt": false, "metadata": {} },
"addresses": { "shipping": {}, "billing": {} },
"data": {}, // depends on the kind (table above)
"signature": "…"
}
The customer is verified by Webround and their token never leaves. Every line arrives already priced: unitNet, unitVat, unitGross, totalNet, totalVat, totalGross and vatRate. Every line also carries additionalData: the free data the customer gave that line (an engraving, a colour, a name) as pairs of text, or null if it has none. Webround does not know what is in it: you read it, for example to add a surcharge or to validate it. A line is its variant and its data: the same variant can be in several lines, one for each set of data. Hooks that do not look at additionalData keep working as before.
The signature
signature is an HMAC-SHA256 (hexadecimal) with the hook's secret, over:
"2" + "." + storeId + "." + timestamp + "." + sha256( the JSON of the body WITHOUT the signature field )
The same algorithm signs your response. Verify the signature of every request and refuse (401) the ones that are not signed or whose timestamp is older than 30 seconds: the address is public, the signature is the security.
The response
Answer 200 with JSON, up to 256 KB, with no redirects (a 3xx is an error):
{
"version": 2,
"storeId": "…", // the same as the request
"timestamp": 1760000000123, // now, within 30 seconds
"operations": [], // only operations of the hook's kind
"signature": "…" // same algorithm and secret
}
Webround checks, in order: the signature, the time window, the strict schema (an extra field or an operation of another kind is an error), the storeId, and the meaning of the operations (for example a variant that does not exist). If a check fails, the whole response is invalid and none of the hook's operations apply.
Operations
Amounts are always net (without VAT), in the currency's main unit (12.34 is 12 euros and 34 cents). Webround calculates the VAT from the tax zone.
cart-transform
| Operation | Effect |
|---|---|
add_line { productId, variantId, quantity, unitPrice?, additionalData? } | Adds the line. If the variant with the same additionalData is already there, it adds the quantity (up to 10000); otherwise it creates a new line with that data. unitPrice is the net price; without it, the list price. |
remove_line { productId, variantId, additionalData? } | Removes the line. |
set_quantity { productId, variantId, quantity, additionalData? } | Sets the quantity (1 to 10000). |
set_unit_price { productId, variantId, unitPrice, additionalData? } | Sets the net unit price (also 0). It is the new list price: it does not count as a discount. |
Which line. additionalData (optional, the same format you receive on the line) says which line the operation is about. Without it, the operation is about the only line of the variant; if the variant has several lines, the one without data; if it has several and none is without data, the response is invalid (with an error that says so): a line is never picked at random. A hook written before line data, on a cart without data, does not change: the variant has one line.
The response is invalid if a variant is not in this store, does not belong to the productId, a line to change is not in the cart, or the variant has several lines with different data and the operation does not say which. After each operation the cart must stay valid: stock, maximum quantity, one currency and cadence, at most 99 lines (19 if recurring) and not empty.
discount
| Operation | Effect |
|---|---|
percentage { value, target, label? } | A percentage discount (above 0, up to 100) on the net amount. |
fixed_amount { amount, target, label? } | A net amount off the total of the target, split in proportion to the value of each line. It never goes past the total. |
free_shipping { target?, label? } | Sets the shipping cost to zero. target is { shipping: "all" } if missing. |
target is { variantIds: [...] } (those lines), "order" (all lines) or { shipping: "all" | "<option id>" } (shipping). Discounts never touch shipping unless it is their target.
shipping
| Operation | Effect |
|---|---|
rename_option { id, label } | Renames a standard shipping option. |
set_option_amount { id, amount } | Sets the net cost of a standard option. |
A hook does not add, hide or reorder options: the ones a customer can choose are the shipping methods of the store. The hook changes only their name and cost.
validation
| Operation | Effect |
|---|---|
block { code, target } | Stops the checkout with 400 CHECKOUT_BLOCKED. code (1 to 64 characters) and target ("cart", "address" or { variantId }) are for your site to translate. |
set_data (every kind)
set_data { key, value } writes into order.additionalData. key is 1 to 64 characters, and the wr_ prefix is reserved. The keys sent by the client come first, then those of the hooks in order of execution; if a key is already there, key-1, key-2… is used.
How discounts are applied
- The amount is always
(net + VAT) × quantity. Discounts come off the net unit price, then VAT is added and only then it rounds to the currency's digits, and finally multiplies by the quantity. - Discounts apply one after the other: 100 net with −5%, −10% and −10 is
((100 − 5%) − 10%) − 10. Values never go negative. - The discount lines add up exactly to the discount on the items, to the cent.
- The free shipping threshold of Webround looks at the gross amount before discounts.
Errors and timeouts
Each call has the hook's timeout, and the whole calculation has a total budget for all hooks together (8 seconds, the same for every store). The call takes the shorter of the two.
| What happens | Status in the log | With passthrough | With abort |
|---|---|---|---|
| Time ran out | timeout | The hook is skipped. | 424 CHECKOUT_HOOK_FAILED |
Not 2xx, a redirect, network down, not JSON, response too big | error | The hook is skipped. | 424 CHECKOUT_HOOK_FAILED |
Wrong or missing signature, time out of the window, wrong storeId, schema not respected, an operation that cannot apply | validation_failed | The hook is skipped. | 424 CHECKOUT_HOOK_FAILED |
"Skipped" means none of its operations apply and the checkout goes on as if it did not exist. Use abort for hooks without which you must not sell, such as a fraud check.
Your site receives errors as { message, code, meta }, ready to translate: CHECKOUT_BLOCKED (400), CHECKOUT_HOOK_FAILED (424, with hookId and reason in meta) and SHIPPING_METHOD_UNAVAILABLE (400).
Quote and payment
quoteis read only and can be called at every change of the cart. A hook must be pure: no side effects, the same answer for the same data. It is logged only on error.checkoutis the payment, once for each attempt. It is always logged. The total Stripe charges is exactly the one calculated.
GET /checkout-hooks/logs lists the calls, with the phase, the status, the duration, whether the hook was skipped, and the whole request and response. They contain customer data: treat the logs as personal data.
Example: free shipping
A hook of kind discount that makes shipping free from a total (Node 20, no dependencies). Create it with:
curl -X POST https://commerce-api.webround.com/checkout-hooks \
-H "Authorization: Bearer <API key>" -H "store-id: <storeId>" -H "user-id: <userId>" \
-H "Content-Type: application/json" \
-d '{"kind":"discount","name":"Free shipping","url":"https://hooks.example.com/free-shipping","timeout":3000,"onError":"passthrough"}'
The answer has the secret: it goes in WR_HOOK_SECRET.
import http from 'node:http';
import { createHash, createHmac, timingSafeEqual } from 'node:crypto';
const secret = process.env.WR_HOOK_SECRET;
const freeFromGross = Number(process.env.FREE_FROM_GROSS ?? 0); // free shipping from this gross total
// HMAC-SHA256 with the secret over `2.storeId.timestamp.sha256(body without the signature)`
const sign = (body) => {
const { signature, ...rest } = body;
const bodyHash = createHash('sha256').update(JSON.stringify(rest)).digest('hex');
return createHmac('sha256', secret).update(`2.${body.storeId}.${body.timestamp}.${bodyHash}`).digest('hex');
};
const fromWebround = (body) => {
try {
return Math.abs(Date.now() - body.timestamp) <= 30_000
&& timingSafeEqual(Buffer.from(sign(body)), Buffer.from(String(body.signature)));
} catch { return false; }
};
http.createServer(async (req, res) => {
if (req.method !== 'POST') return res.writeHead(405).end();
let text = '';
for await (const chunk of req) text += chunk;
let body;
try { body = JSON.parse(text); } catch { return res.writeHead(400).end(); }
if (!fromWebround(body)) return res.writeHead(401).end(); // never trust an unsigned request
const itemsGross = body.data.items.reduce((sum, item) => sum + item.totalGross, 0);
const free = body.kind === 'discount' && itemsGross >= freeFromGross;
const operations = free ? [{ type: 'free_shipping', label: 'Free shipping', target: { shipping: 'all' } }] : [];
const response = { version: 2, storeId: body.storeId, timestamp: Date.now(), operations };
response.signature = sign(response);
res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify(response));
}).listen(Number(process.env.PORT ?? 3000));
At checkout the hook answers [{ "type": "free_shipping", "target": { "shipping": "all" } }]: Webround sets the shipping cost (and so its VAT) to zero and adds a discount line for the amount that was there. If there is nothing to ship, nothing changes. If the hook does not answer, with passthrough shipping stays paid. To receive calls from Webround, your server needs an HTTPS address that can be reached from outside.