Skip to main content
Faremeter API
Faremeter API / fetch/src

fetch/src

Namespaces

Classes

WrappedFetchError

Defined in: packages/fetch/src/fetch.ts:14 Error thrown when payment fails after exhausting all retry attempts. Contains the final 402 response for inspection.

Extends

  • Error

Constructors

Constructor
new WrappedFetchError(message, response): WrappedFetchError
Defined in: packages/fetch/src/fetch.ts:15
Parameters
message
string
response
Response The final 402 response after all retry attempts failed.
Returns
WrappedFetchError
Overrides
Error.constructor

Properties

response
response: Response
Defined in: packages/fetch/src/fetch.ts:18 The final 402 response after all retry attempts failed.

Type Aliases

WrapOpts

WrapOpts = ProcessPaymentRequiredResponseOpts & object
Defined in: packages/fetch/src/fetch.ts:27 Configuration options for wrapping a fetch function with x402 payment handling.

Type declaration

initialRetryDelay?
optional initialRetryDelay: number
Initial delay between retries in milliseconds. Doubles after each attempt. Defaults to 100.
phase1Fetch?
optional phase1Fetch: typeof fetch
Optional fetch function for the initial request (phase 1). Defaults to phase2Fetch.
retryCount?
optional retryCount: number
Number of retry attempts after initial failure. Defaults to 2.
returnPaymentFailure?
optional returnPaymentFailure: boolean
If true, returns the 402 response instead of throwing on payment failure.

Functions

wrap()

wrap(phase2Fetch, options): (input, init) => Promise<Response>
Defined in: packages/fetch/src/fetch.ts:49 Wraps a fetch function with automatic x402 payment handling. When a 402 Payment Required response is received, the wrapper automatically processes the payment requirements, executes payment via the configured handlers, and retries the request with the payment header attached.

Parameters

phase2Fetch
{(input, init?): Promise<Response>; (input, init?): Promise<Response>; } The fetch function to use for the paid request (phase 2)
options
WrapOpts Configuration including payment handlers and retry settings

Returns

A wrapped fetch function with the same signature as native fetch
(input, init): Promise<Response>
Parameters
input
RequestInfo | URL
init
RequestInit = {}
Returns
Promise<Response>

References

chooseFirstAvailable

Re-exports chooseFirstAvailable