npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@b3dotfun/anyspend-x402-fetch

v0.2.5

Published

AnySpend x402 Fetch client - Extended x402 payment protocol integration for native fetch API with multi-token support

Readme

@b3dotfun/anyspend-x402-fetch

AnySpend-enhanced fetch client for the x402 Payment Protocol. This package extends the native fetch API to automatically handle 402 Payment Required responses with multi-token and cross-chain payment support through the AnySpend facilitator.

Installation

npm install @b3dotfun/anyspend-x402-fetch

Quick Start

import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment } from "@b3dotfun/anyspend-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();

What Makes AnySpend Different?

Unlike standard x402 implementations, AnySpend x402 enables:

  • Multi-token payments - Pay with various ERC-20 tokens, not just USDC
  • 🌉 Cross-chain payments - Pay on one network while the server receives on another
  • 🔄 Automatic conversion - Token swaps and bridging handled seamlessly by the facilitator
  • 🎯 Standard compatibility - Works with standard x402 servers (no custom server code needed)

API

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

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)
  • config: Optional X402 configuration (e.g., custom RPC URLs)
  • preferences: Optional payment preferences to specify preferred token and network

Returns

A wrapped fetch function that automatically handles 402 responses by:

  1. Making the initial request (with optional payment preferences)
  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

Payment Preferences

You can specify which token and network you want to pay with:

import { wrapFetchWithPayment, createSigner, type PaymentPreferences } from "@b3dotfun/anyspend-x402-fetch";

const signer = await createSigner("base-sepolia", privateKey);

// Pay with WETH instead of USDC
const preferences: PaymentPreferences = {
  preferredToken: "0x4200000000000000000000000000000000000006", // WETH on Base Sepolia
  preferredNetwork: "base-sepolia"
};

const fetchWithPayment = wrapFetchWithPayment(
  fetch,
  signer,
  undefined, // maxValue
  undefined, // paymentRequirementsSelector
  undefined, // config
  preferences
);

await fetchWithPayment('https://api.example.com/data');

Supported Networks

AnySpend facilitator supports multiple networks:

  • Base / Base Sepolia
  • Ethereum / Ethereum Sepolia
  • Arbitrum / Arbitrum Sepolia
  • Optimism / Optimism Sepolia
  • Polygon / Polygon Amoy

Primary Settlement Token: USDC across all supported networks

For the latest list of supported tokens and networks, query: https://mainnet.anyspend.com/x402/supported

Example

import { config } from "dotenv";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment } from "@b3dotfun/anyspend-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);
  });

Related Packages

About x402

The x402 protocol is an open standard for HTTP-native payments. It enables:

  • Low fees: No percentage-based fees, just network costs
  • Instant settlement: ~2 second finality on supported networks
  • Micro-payments: Accept payments as low as $0.001
  • Chain agnostic: Works across multiple blockchain networks
  • Easy integration: One line of code for servers, one function for clients

Learn more at x402.org

Resources

License

Apache-2.0

Contributing

Contributions are welcome! This is an extended version of the Coinbase x402-fetch client with AnySpend ecosystem integration.