@faremeter/fetch

中文文档
0.22.0发布于 2026/05/27LGPL-3.0-onlyTypeScriptESM + CJS近 30 天下载 3976

HTTP fetch wrapper that automatically handles 402 Payment Required responses with x402 protocol

原始使用文档
文档目录21 节
  1. Installation
  2. Features
  3. API Reference
  4. Functions
  5. chooseFirstAvailable
  6. processPaymentRequiredResponse
  7. processPaymentRequiredResponseMPP
  8. wrap
  9. responseFeeder
  10. WrappedFetchError
  11. Types
  12. DetectedVersion
  13. ProcessPaymentRequiredResponseOpts
  14. ProcessPaymentRequiredResponseResult
  15. ProcessMPPOpts
  16. WrapOpts
  17. MockFetchType
  18. MockResponse
  19. Examples
  20. Related Packages
  21. License

@faremeter/fetch

HTTP fetch wrapper that automatically handles 402 Payment Required responses with the x402 protocol.

Installation

pnpm install @faremeter/fetch

Features

  • Automatic 402 handling - Transparently pays and retries
  • Pluggable payment handlers - Support any blockchain
  • Multi-chain support - Use multiple handlers simultaneously
  • Smart payer selection - Choose based on balance, cost, etc.
  • Retry logic - Configurable exponential backoff
  • Type-safe - Full TypeScript support

API Reference

Functions

chooseFirstAvailable

Default payer chooser that selects the first available payment execer.

FunctionType
chooseFirstAvailable(possiblePayers: PaymentExecer[]) => PaymentExecer

Parameters:

  • possiblePayers: - Array of payment execers that can handle the requirements

Returns:

The first execer in the array

processPaymentRequiredResponse

Process a 402 Payment Required response, auto-detecting v1 or v2 protocol.

FunctionType
processPaymentRequiredResponse(ctx: RequestContext, response: Response, options: ProcessPaymentRequiredResponseOpts) => Promise<ProcessPaymentRequiredResponseResult>

Parameters:

  • ctx: - Request context
  • response: - The 402 Response object (must not have been consumed)
  • options: - Processing options including payment handlers

Returns:

Payment information including header and detected version

processPaymentRequiredResponseMPP

FunctionType
processPaymentRequiredResponseMPP(response: Response, handlers: MPPPaymentHandler[], opts?: ProcessMPPOpts or undefined) => Promise<string or undefined>

wrap

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.

FunctionType
wrap(phase2Fetch: (input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<Response>, options: WrapOpts) => (input: RequestInfo or URL, init?: RequestInit) => Promise<...>

Parameters:

  • phase2Fetch: - The fetch function to use for the paid request (phase 2)
  • options: - Configuration including payment handlers and retry settings

Returns:

A wrapped fetch function with the same signature as native fetch

responseFeeder

Creates a mock fetch that returns responses from a queue in order.

Each call to the returned fetch function shifts the next response from the array. Responses can be either Response objects or fetch functions for dynamic behavior.

FunctionType
responseFeeder(responses: (Response or ((input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<Response>))[]) => (input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<...>

Parameters:

  • responses: - Array of responses or fetch functions to return in order

Returns:

A fetch function that serves responses from the queue

WrappedFetchError

Error thrown when payment fails after exhausting all retry attempts. Contains the final 402 response for inspection.

Types

DetectedVersion

TypeType
DetectedVersion1 or 2

ProcessPaymentRequiredResponseOpts

Options for processing a 402 Payment Required response.

TypeType
ProcessPaymentRequiredResponseOpts{ /** Payment handlers that produce execers for payment requirements. */ handlers: PaymentHandler[]; /** Optional function to select among multiple possible payers. Defaults to chooseFirstAvailable. */ payerChooser?: (execer: PaymentExecer[]) => Promise<PaymentExecer>; }

ProcessPaymentRequiredResponseResult

Result of processing a 402 Payment Required response.

TypeType
ProcessPaymentRequiredResponseResult{ /** The selected payment execer. */ payer: PaymentExecer; /** The result from executing the payment. */ payerResult: { payload: object }; /** The payment payload in the detected protocol version format. */ paymentPayload: x402PaymentPayload or x402PaymentPayloadV1; /** Base64-encoded payment header ready to attach to the retry request. */ paymentHeader: string; /** The detected protocol version (1 or 2). */ detectedVersion: DetectedVersion; }

ProcessMPPOpts

Attempts to process a 402 response as an MPP challenge.

Checks for a WWW-Authenticate header with Payment challenges, then iterates handlers to find one that matches. Returns the Authorization header value on success, or undefined if no MPP challenges are present or no handler matches (allowing fallthrough to x402).

Does not consume the response body.

TypeType
ProcessMPPOpts{ bodyDigest?: string; }

WrapOpts

Configuration options for wrapping a fetch function with x402 payment handling.

TypeType
WrapOptsProcessPaymentRequiredResponseOpts and { /** MPP payment handlers for Authorization: Payment flow. */ mppHandlers?: MPPPaymentHandler[]; /** Optional fetch function for the initial request (phase 1). Defaults to phase2Fetch. */ phase1Fetch?: typeof fetch; /** Number of retry attempts after initial failure. Defaults to 2. */ retryCount?: number; /** Initial delay between retries in milliseconds. Doubles after each attempt. Defaults to 100. */ initialRetryDelay?: number; /** If true, returns the 402 response instead of throwing on payment failure. */ returnPaymentFailure?: boolean; }

MockFetchType

TypeType
MockFetchTypetypeof fetch

MockResponse

TypeType
MockResponseResponse

Examples

See working examples in the faremeter repository:

License

LGPL-3.0-only

版本与发布记录

@faremeter/fetch 共发布 33 个版本,首次发布于 2025/07/16;过去 12 个月发布了 12 个版本。

版本发布日期距今
0.22.0latest2026/05/275 个月前
0.21.02026/04/216 个月前
0.20.12026/04/147 个月前
0.20.02026/04/147 个月前
0.19.02026/03/217 个月前
0.18.02026/03/207 个月前

关于 @faremeter/fetch 的常见问题

@faremeter/fetch 是什么?

HTTP fetch wrapper that automatically handles 402 Payment Required responses with x402 protocol

如何安装 @faremeter/fetch?

在项目目录运行 npm install @faremeter/fetch,或使用 pnpm add / yarn add / bun add。

@faremeter/fetch 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@faremeter/fetch 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@faremeter/fetch 可以商用吗?

@faremeter/fetch 使用 LGPL-3.0-only 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@faremeter/fetch 的下载量有多少?

根据 npm 官方下载统计,@faremeter/fetch 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,976 次,日均约 133 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@faremeter/fetch 还在维护吗?

最新版本 0.22.0 发布于 2026/05/27(5 个月前);至今共发布 33 个版本,其中过去 12 个月发布了 12 个;首次发布于 2025/07/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。