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

Setup

  1. Install and build all packages from the typescript examples root:
cd ../../
pnpm install && pnpm build
cd clients/axios
  1. Copy .env-local to .env and add your private keys:
cp .env-local .env

Required environment variables:

  • EVM_PRIVATE_KEY - Ethereum private key for EVM payments
  • SVM_PRIVATE_KEY - Solana private key for SVM payments
  1. 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.