Axios Client Example
Use x402 with Axios HTTP client
exampletypescriptaxiosclientbeginner
Files
2 files in this example
@x402/axios Example Client
Example client demonstrating how to use @x402/axios to make HTTP requests to endpoints protected by the x402 payment protocol.
import { x402Client, wrapAxiosWithPayment } from "@x402/axios";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import axios from "axios";
const client = new x402Client();
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.EVM_PRIVATE_KEY) });
const api = wrapAxiosWithPayment(axios.create(), client);
const response = await api.get("http://localhost:4021/weather");
console.log(response.data);
Prerequisites
- Node.js v20+ (install via nvm)
- pnpm v10 (install via pnpm.io/installation)
- A running x402 server (see express server example)
- Valid EVM and/or SVM private keys for making payments
Setup
- Install and build all packages from the typescript examples root:
cd ../../
pnpm install && pnpm build
cd clients/axios
- Copy
.env-localto.envand add your private keys:
cp .env-local .env
Required environment variables:
EVM_PRIVATE_KEY- Ethereum private key for EVM paymentsSVM_PRIVATE_KEY- Solana private key for SVM payments
- Run the client:
pnpm start
Next Steps
See Advanced Examples for builder pattern registration, payment lifecycle hooks, and network preferences.
Related Content
Looking for more? Check out our other typescript examples or browse by client content.