> ## Documentation Index
> Fetch the complete documentation index at: https://docs.payai.network/llms.txt
> Use this file to discover all available pages before exploring further.

# Axios

## Getting started with Axios

Make x402 payments with an Axios client in 2 minutes.

<Note>You can find the full code for this example [here](https://github.com/x402-foundation/x402/tree/main/examples/typescript/clients/axios).</Note>

### Step 1: Create a project and install dependencies

Use your favorite package manager:

##### npm

```bash theme={null}
mkdir my-first-client && cd my-first-client
npm init -y && npm pkg set type=module
npm install axios dotenv viem @solana/kit @scure/base @x402/axios @x402/evm @x402/svm
npm install -D typescript tsx
```

##### pnpm

```bash theme={null}
mkdir my-first-client && cd my-first-client
pnpm init && pnpm pkg set type=module
pnpm add axios dotenv viem @solana/kit @scure/base @x402/axios @x402/evm @x402/svm
pnpm add -D typescript tsx
```

##### bun

```bash theme={null}
mkdir my-first-client && cd my-first-client
bun init -y
bun add axios dotenv viem @solana/kit @scure/base @x402/axios @x402/evm @x402/svm
bun add -d typescript
```

This is the same dependency set as the [upstream Axios example](https://github.com/x402-foundation/x402/tree/main/examples/typescript/clients/axios).

### Step 2: Set your environment variables

Create a `.env` file in the project root and set the following:

* `RESOURCE_SERVER_URL`: Base URL of the server to call (e.g. [http://localhost:4021](http://localhost:4021))
* `ENDPOINT_PATH`: Path to a paid endpoint (e.g. /weather)
* `EVM_PRIVATE_KEY`: Hex EVM private key of the paying account
* `SVM_PRIVATE_KEY`: Base58 Solana private key of the paying account

```env theme={null}
EVM_PRIVATE_KEY=
SVM_PRIVATE_KEY=
RESOURCE_SERVER_URL=http://localhost:4021
ENDPOINT_PATH=/weather
```

Optionally set `EVM_RPC_URL` to a JSON-RPC endpoint. When present, the client can perform
onchain reads, which enables gas-sponsoring extensions:

```env theme={null}
EVM_RPC_URL=
```

### Step 3: Preview the client code

Create `index.ts`: It loads your env, builds an `x402Client` and registers the exact and `upto` EVM schemes plus exact SVM, wraps Axios with `wrapAxiosWithPayment`, calls your endpoint, and prints the parsed payment result via `x402HTTPClient.parsePaymentResult`.

```ts theme={null}
import { config } from "dotenv";
import { x402Client, wrapAxiosWithPayment, x402HTTPClient } from "@x402/axios";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { UptoEvmScheme } from "@x402/evm/upto/client";
import { ExactSvmScheme } from "@x402/svm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";
import axios from "axios";

config();

const evmPrivateKey = process.env.EVM_PRIVATE_KEY as `0x${string}`;
const svmPrivateKey = process.env.SVM_PRIVATE_KEY as string;
const evmRpcUrl = process.env.EVM_RPC_URL;
const baseURL = process.env.RESOURCE_SERVER_URL || "http://localhost:4021";
const endpointPath = process.env.ENDPOINT_PATH || "/weather";
const url = `${baseURL}${endpointPath}`;

/**
 * Example demonstrating how to use @x402/axios to make requests to x402-protected endpoints.
 *
 * Uses the builder pattern to register payment schemes directly.
 *
 * Required environment variables:
 * - EVM_PRIVATE_KEY: The private key of the EVM signer
 * - SVM_PRIVATE_KEY: The private key of the SVM signer
 *
 * Optional environment variables:
 * - EVM_RPC_URL: JSON-RPC endpoint for on-chain reads (enables gas sponsoring extensions)
 */
async function main(): Promise<void> {
  const evmSigner = privateKeyToAccount(evmPrivateKey);
  const svmSigner = await createKeyPairSignerFromBytes(base58.decode(svmPrivateKey));
  const rpcOptions = evmRpcUrl ? { rpcUrl: evmRpcUrl } : undefined;

  const client = new x402Client();
  client.register("eip155:*", new ExactEvmScheme(evmSigner, rpcOptions));
  client.register("eip155:*", new UptoEvmScheme(evmSigner, rpcOptions));
  client.register("solana:*", new ExactSvmScheme(svmSigner));

  const api = wrapAxiosWithPayment(axios.create(), client);
  const httpClient = new x402HTTPClient(client);

  console.log(`Making request to: ${url}\n`);
  const response = await api.get(url);
  const result = httpClient.parsePaymentResult({
    status: response.status,
    getHeader: name => response.headers[name.toLowerCase()],
    body: response.data,
  });
  console.dir(result, { depth: null });
}

main().catch(error => {
  console.error(error?.response?.data?.error ?? error);
  process.exit(1);
});
```

### Step 4: Run the client

```bash theme={null}
npx tsx index.ts
```

<Check>
  Your client is now making x402 payments!
</Check>

### Step 5: Test the client

You can test your client against a local server by running the [Express example](/x402/servers/typescript/express), [Hono example](/x402/servers/typescript/hono), or [Next.js example](/x402/servers/typescript/nextjs).

You can also test your client against a <a href="https://x402.payai.network" target="_blank">live merchant</a> for free. You will receive a full refund of any tokens that you send, and PayAI will pay for the network fees.

## x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the [x402 Reference](/x402/reference).

## Need help?

<Card title="Join our Community" icon="discord" href="https://discord.gg/eWJRwMpebQ">
  Have questions or want to connect with other developers? Join our Discord server.
</Card>
