Dynamic pricing with quotes
Compute a price per request and still charge exactly what the payer was quoted.
app.post(
"/v1/translate",
tollstile(toll.price(async (context) => {
const words = await countWords(context.request);
return `$${(words * 0.0001).toFixed(4)}`;
})),
translate,
);- A request without payment gets
402with a signed quote for the computed price. - The payer retries the same request with a proof that carries the quote back.
- Tollstile charges the quoted price, even if the function would now return a different one.
Quotes expire after quoteTtlMs (5 minutes by default) and are never stored. Dynamic routes require rails that can carry quotes; routes with other rails are refused at startup.
A quote only pays for the request it priced
A quote commits to the request it was issued for: method, path and query, and a hash of the exact body bytes. On MCP it commits to the tool name and its arguments. If the retry differs, Tollstile answers 402 with reason: "quote_mismatch" and a fresh quote for the new request. Nothing is authorized or charged.
So this does not work:
POST /v1/translate {"text": "hello"} → 402, quote for $0.0001
POST /v1/translate {"text": "<a million words>"} → 402 quote_mismatch, quote for $100.00
Payment: …quote for $0.0001…Your price function and handler both read the body normally. Tollstile hashes a copy.
Clients that re-serialize the body
Binding to exact bytes means the retry must send the same bytes. If your clients may reorder keys or add fields that do not affect the price, bind only the fields that do:
toll.price(priceByWords, {
commit: async (context) => {
const { text, targetLanguage } = await context.request.json();
return JSON.stringify([text, targetLanguage]);
},
});Anything the commitment leaves out can change after quoting, so include every input your price depends on.
Reusable credentials
Credentials that pay for many requests (L402, KYAPay) are charged each request's own price against their limit. They are never locked to the first request's quote.
Tollstile does not decide what your service should cost — it evaluates the price you compute.