@faremeter/fetch
@faremeter/fetch wraps the native fetch API so your application can negotiate Coinbase x402 payments automatically. It understands 402 Payment Required responses, consults local payment handlers, signs payloads, and retries with an X-PAYMENT header.
Source: GitHub › packages/fetchThe package is framework agnostic—use it in browsers, Workers, Node.js, or anywhere
fetch is available.
Quick start
wrap will:
- Parse the x402 payment requirements (
@faremeter/types/x402). - Ask each handler for compatible
PaymentExecers. - Pick one (default: first available) and execute it to produce a payload.
- Base64-encode the payload into the
X-PAYMENTheader. - Retry the request with the header and return the new response.
returnPaymentFailure is false, a WrappedFetchError is thrown.
API surface
wrap(phase2Fetch, options)
Wraps a fetch implementation. Returns a function with the same signature.
phase2Fetch: fetch used for the retry that carries the payment header. Passfetchdirectly for most cases; override when you need different runtimes for the retry.options.handlers: required array ofPaymentHandlers. See@faremeter/types/client.options.payerChooser: async function that selects aPaymentExecerwhen multiple handlers return candidates. Defaults tochooseFirstAvailable.options.phase1Fetch: alternative fetch for the first attempt (before payment). Useful when hitting a public edge that must see the original request.options.retryCount: number of additional attempts if the second phase keeps returning 402 (default2).options.initialRetryDelay: backoff starting delay in milliseconds (default100). Doubles on each retry.options.returnPaymentFailure: whentrue, returns the final 402 response instead of throwing.- Additional properties from
ProcessPaymentRequiredResponseOpts(e.g.,payerChooser) are forwarded to the underlying processor.
WrappedFetchError
Subclass of Error thrown when retries are exhausted and returnPaymentFailure is false. The original Response is attached as .response.
internal.processPaymentRequiredResponse(context, responseBody, options)
Low-level helper that powers wrap. It:
- Validates the merchant’s JSON using
@faremeter/types/x402. - Invokes each
PaymentHandler, collecting possiblePaymentExecers. - Lets you supply a
payerChooserto decide which payer executes. - Executes the chosen payer, returning
{ payer, payerResult, paymentPayload, paymentHeader }.
internal.chooseFirstAvailable(execers)
Default chooser: throws if the array is empty or contains undefined, otherwise returns execers[0].
mock.responseFeeder(responses)
Testing helper that returns a mock fetch function. Provide a queue of Response objects or functions returning Response; each invocation consumes the next item.
Usage patterns
- Multiple networks: Combine Solana/EVM handlers in the
handlersarray. The payer chooser can inspectrequirements.schemeorrequirements.networkto prefer a specific chain. - UX retries: Set
returnPaymentFailure: trueto bubble the failing response back to UI layers instead of throwing. Great for showing “Payment still pending…” states. - Custom telemetry: Wrap the returned fetch to capture metrics (e.g., time spent negotiating payments).
Related references
@faremeter/types: definitions for x402 payloads,PaymentHandler, andPaymentExecer.@faremeter/payment-solana,@faremeter/payment-evm: ready-made handlers for blockchain settlements.@faremeter/middleware: server-side companion that issues the 402 Payment Required responses.