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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@aeon-ai-pay/x402-axios

v0.0.5

Published

x402 Payment Protocol

Downloads

22

Readme

x402-axios

A utility package that extends Axios 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 with Axios.

Installation

npm install @aeon-ai-pay/x402-axios

Quick Start

EVM (BSC) Example

import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";
import { bsc } from "viem/chains";

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

// Create an Axios instance with payment handling
const api = withPaymentInterceptor(
  axios.create({
    baseURL: "https://api.example.com",
  }),
  { evmClient }
);

// Make a request that may require payment
const response = await api.get("/paid-endpoint");
console.log(response.data);

Solana Example

import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";

// Create a Solana client (implementation depends on your Solana wallet setup)
const solanaClient = {
  // Your Solana client implementation
};

// Create an Axios instance with payment handling
const api = withPaymentInterceptor(
  axios.create({
    baseURL: "https://api.example.com",
  }),
  { solanaClient }
);

// Make a request that may require payment
const response = await api.get("/paid-endpoint");
console.log(response.data);

Multi-Chain Support

import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";

// Support both EVM and Solana payments
const api = withPaymentInterceptor(
  axios.create({
    baseURL: "https://api.example.com",
  }),
  { 
    evmClient: yourEvmClient,
    solanaClient: yourSolanaClient 
  }
);

// The interceptor will automatically choose the best payment method
// based on available options and stablecoin preferences
const response = await api.get("/paid-endpoint");

Features

  • Automatic handling of 402 Payment Required responses
  • Support for BSC (Binance Smart Chain) and Solana blockchains
  • Intelligent payment method selection with stablecoin prioritization
  • Automatic retry of requests with payment headers
  • Payment verification and header generation
  • Exposes payment response headers
  • Customizable payment requirements selection

API

withPaymentInterceptor(axiosClient, paymentClient, paymentRequirementsSelector?)

Adds a response interceptor to an Axios instance to handle 402 Payment Required responses automatically.

Parameters

  • axiosClient: The Axios instance to add the interceptor to
  • paymentClient: An object containing wallet clients for different blockchains:
    • evmClient?: EVM wallet client (viem WalletClient) for BSC
    • solanaClient?: Solana wallet client
  • paymentRequirementsSelector?: Optional function to customize payment requirements selection (defaults to selectPaymentRequirements)

Returns

The modified Axios instance with the payment interceptor that will:

  1. Intercept 402 responses
  2. Parse the payment requirements
  3. Select the best payment option based on available clients and preferences
  4. Create a payment header using the appropriate wallet client
  5. Retry the original request with the payment header
  6. Expose the X-PAYMENT-RESPONSE header in the final response

Payment Selection Logic

When multiple payment options are available, the interceptor prioritizes:

  1. Namespace compatibility: Matches available wallet clients (EVM vs Solana)
  2. Stablecoin preference: Prioritizes USDT/USDC over native tokens (BNB/SOL)
  3. Network compatibility: Ensures the payment network matches your wallet configuration (BSC for EVM)

Additional Exports

decodeXPaymentResponse

Utility function to decode payment response data from the X-PAYMENT-RESPONSE header.

import { decodeXPaymentResponse } from "x402-axios";

// After a successful payment, decode the response
const paymentData = decodeXPaymentResponse(response.headers['x-payment-response']);