@crifine/x402
A fetch wrapper for 402 Payment Required, with spend limits that are not optional.
npm install @crifine/x402Works with any API that speaks x402 — it has no dependency on the rest of Crifine.
import { x402Fetch, usdcSettler } from "@crifine/x402";
const pay = x402Fetch({
maxPerCall: 0.01, // required
maxTotal: 5, // lifetime budget for this wrapper
allowHosts: ["api.crifine.app"],
onPayment: (event) => audit(event),
settle: usdcSettler({ walletClient }),
});
pay.spent; // running total
pay.remaining; // budget left, or Infinity
pay.receipts; // every settlement, in order — an audit trail with no wiringWhy payments are serialised by default
With parallel calls, two 402s can both read the budget before either has spent, and the pair sails past a limit each of them individually respected. An unattended agent fanning out ten requests would blow through maxTotal without a single check failing. concurrency defaults to 1; raise it only if you can tolerate that.
Errors
| Error | Meaning | What an agent should do |
|---|---|---|
SpendLimitError | Quote above maxPerCall | Skip, or widen the limit deliberately |
BudgetExhaustedError | maxTotal reached | Stop — a budget is a decision, not a hiccup |
HostNotAllowedError | 402 from an unlisted host | Treat as hostile; something redirected you |
MalformedTermsError | 402 without readable terms | The server is broken; do not pay |
Nothing is spent when any of these throw — the ledger only moves on a receipt, and it moves by the *receipt* amount, not the quoted one.