Skip to content

Function: createX402Fetch() ​

ts
function createX402Fetch(payers, opts?): FetchLike;

Defined in: packages/x402-server-sdk/src/fetch.ts:49

Wrap fetch so x402 payments are transparent: call the resource, and on a 402 read the accepted requirements, pay via a matching payer, and retry with the payment in the PAYMENT-SIGNATURE header, where the x402 v2 HTTP transport carries it. Non-402 responses — and 402s no payer can satisfy — pass through unchanged.

Accepts one payer or several; with several, each advertised requirement routes to the first payer that supports() it (e.g. a Canton payer + an EVM payer). Depends only on the X402Payer contract, so it's scheme-agnostic.

Works for any HTTP method. The paid request is retried, so a request body must be re-readable (a value like a string / JSON / Buffer / URLSearchParams, not a ReadableStream, and don't pass a Request object whose body the first call consumes).

Parameters ​

payers ​

| X402Payer | X402Payer[]

opts? ​

X402FetchOptions = {}

Returns ​

FetchLike

Example ​

ts
const f = createX402Fetch(payer);                 // one payer
const f = createX402Fetch([cantonPayer, evmPayer]); // several
const res = await f("https://api.example/paid");