Skip to content

wrapFetch

wrapFetch(params): X402Fetch

Defined in: src/x402/fetch.ts:180

Returns an x402-aware fetch that pays for 402 responses on the caller’s behalf using the provided signer.

Parameters

params

WrapFetchParams

Returns

X402Fetch

Example

import { privateKeyToAccount } from "viem/accounts";
import { createX402PaymentSigner, wrapFetch } from "kawasekit";
const signer = createX402PaymentSigner({
account: privateKeyToAccount(process.env.PAYER_PK as `0x${string}`),
});
let spent = 0n;
const MAX_SPEND = 100_000n; // 100 JPYC (6 decimals)
const fetch402 = wrapFetch({
signer,
onPayment: (req) => {
const next = spent + BigInt(req.amount);
if (next > MAX_SPEND) return false; // budget exhausted
spent = next;
return true;
},
});
const res = await fetch402("https://api.example.com/weather?city=Tokyo");
console.log(await res.json());