$ npm install @space-meridian/x402-axiosA 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.
npm install x402-axios
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";
import { baseSepolia } from "viem/chains";
// Create a wallet client
const account = privateKeyToAccount("0xYourPrivateKey");
const client = createWalletClient({
account,
transport: http(),
chain: baseSepolia,
});
// Create an Axios instance with payment handling
const api = withPaymentInterceptor(
axios.create({
baseURL: "https://api.example.com",
}),
client
);
// Make a request that may require payment
const response = await api.get("/paid-endpoint");
console.log(response.data);
withPaymentInterceptor(axiosClient, walletClient)Adds a response interceptor to an Axios instance to handle 402 Payment Required responses automatically.
axiosClient: The Axios instance to add the interceptor towalletClient: The wallet client used to sign payment messages (must implement the x402 wallet interface)The modified Axios instance with the payment interceptor that will: