Catalog npm
Back to packageRELEASE DETAILS

0.6.16

npm

@0xcipher0/x402-fetch

x402 Payment Protocol

Published26 Oct 2025
Direct dependencies18
Transitive dependenciesNot calculated

README

Current package documentation

x402-fetch

A utility package that extends the native fetch API to automatically handle 402 Payment Required responses using the x402 payment protocol. This package enables seamless integration of payment functionality into your applications when making HTTP requests.

Installation

npm install x402-fetch

Quick Start

import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment } from "x402-fetch";
import { baseSepolia } from "viem/chains";

// Create a wallet client
const account = privateKeyToAccount("0xYourPrivateKey");
const client = createWalletClient({
  account,
  transport: http(),
  chain: baseSepolia,
});

// Wrap the fetch function with payment handling
const fetchWithPay = wrapFetchWithPayment(fetch, client);

// Make a request that may require payment
const response = await fetchWithPay("https://api.example.com/paid-endpoint", {
  method: "GET",
});

const data = await response.json();

API

wrapFetchWithPayment(fetch, walletClient, maxValue?, paymentRequirementsSelector?)

Wraps the native fetch API to handle 402 Payment Required responses automatically.

Parameters

  • fetch: The fetch function to wrap (typically globalThis.fetch)
  • walletClient: The wallet client used to sign payment messages (must implement the x402 wallet interface)
  • maxValue: Optional maximum allowed payment amount in base units (defaults to 0.1 USDC)
  • paymentRequirementsSelector: Optional function to select payment requirements from the response (defaults to selectPaymentRequirements)

Returns

A wrapped fetch function that automatically handles 402 responses by:

  1. Making the initial request
  2. If a 402 response is received, parsing the payment requirements
  3. Verifying the payment amount is within the allowed maximum
  4. Creating a payment header using the provided wallet client
  5. Retrying the request with the payment header

Example

import { config } from "dotenv";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment } from "x402-fetch";
import { baseSepolia } from "viem/chains";

config();

const { PRIVATE_KEY, API_URL } = process.env;

const account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);
const client = createWalletClient({
  account,
  transport: http(),
  chain: baseSepolia,
});

const fetchWithPay = wrapFetchWithPayment(fetch, client);

// Make a request to a paid API endpoint
fetchWithPay(API_URL, {
  method: "GET",
})
  .then(async response => {
    const data = await response.json();
    console.log(data);
  })
  .catch(error => {
    console.error(error);
  });

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 18

Native requirements
18 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
viem
Environment
{
  "local_name": "viem",
  "alias": false,
  "peer_metadata": null
}
^2.21.26runtime
zod
Environment
{
  "local_name": "zod",
  "alias": false,
  "peer_metadata": null
}
^3.24.2runtime
@0xcipher0/x402
Environment
{
  "local_name": "@0xcipher0/x402",
  "alias": false,
  "peer_metadata": null
}
^0.6.14runtime
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^22.13.4development
@eslint/js
Environment
{
  "local_name": "@eslint/js",
  "alias": false,
  "peer_metadata": null
}
^9.24.0development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^9.24.0development
eslint-plugin-jsdoc
Environment
{
  "local_name": "eslint-plugin-jsdoc",
  "alias": false,
  "peer_metadata": null
}
^50.6.9development
eslint-plugin-prettier
Environment
{
  "local_name": "eslint-plugin-prettier",
  "alias": false,
  "peer_metadata": null
}
^5.2.6development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^8.29.1development
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^8.29.1development
eslint-plugin-import
Environment
{
  "local_name": "eslint-plugin-import",
  "alias": false,
  "peer_metadata": null
}
^2.31.0development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
3.5.2development
tsup
Environment
{
  "local_name": "tsup",
  "alias": false,
  "peer_metadata": null
}
^8.4.0development
tsx
Environment
{
  "local_name": "tsx",
  "alias": false,
  "peer_metadata": null
}
^4.19.2development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.7.3development
vite-tsconfig-paths
Environment
{
  "local_name": "vite-tsconfig-paths",
  "alias": false,
  "peer_metadata": null
}
^5.1.4development
vitest
Environment
{
  "local_name": "vitest",
  "alias": false,
  "peer_metadata": null
}
^3.0.5development
vite
Environment
{
  "local_name": "vite",
  "alias": false,
  "peer_metadata": null
}
^6.2.6development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-R4YREvdOZAIm+MSX4M3kkAZRfauu/iME9uV5PKnHJwVXgKyTXaPOT5Y6dAsvtKhgaqHX/4eEyosbgm9CgCSI2g==",
  "shasum": "df7abe01c86acea241c4dce51d58d08bf7820003",
  "tarball": "https://registry.npmjs.org/@0xcipher0/x402-fetch/-/x402-fetch-0.6.16.tgz",
  "fileCount": 8,
  "unpackedSize": 26092,
  "signatures": [
    {
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U",
      "sig": "MEYCIQCh1M+R/BpGhMEInZZsWCEwbwvxYfwqHIAQ2vpvhjT07QIhAJGOEMxaG+kbRw0dNsxDjz1GWYBBemAC2CS2o6byN2ux"
    }
  ]
}

Repository

PENDING
coinbase/x402

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 23:02 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:472c133d9982f9a5da063529cfc41943cb3ee116fdb9bc9b2f789f2ca850d714

Other collected versions

0.6.1525 Oct0.6.1425 Oct0.6.1325 Oct0.6.1225 Oct0.6.1125 Oct0.6.1025 OctView all collected versions →