# x402 integration: pay per request with USDC | Spider

Pay for Spider API calls with USDC using the x402 protocol. No account or API key needed. Autonomous agents pay per request over HTTP.

Canonical: https://spider.cloud/docs/integrations/x402/

Pay per request in USDC on Base. No account, no API key, no signup. Spider implements the [x402 protocol](https://x402.org): a request to a paid route without an API key gets a `402 Payment Required` carrying the price, and the x402 client SDK signs and retries it. That is one wrapper around `fetch`, so an agent never waits on a human to add credits.

## Payment flow

The protocol runs on plain HTTP headers.

1. 01 The client sends a request to a paid Spider endpoint without an API key.
2. 02 The server answers `402` with a `PAYMENT-REQUIRED` header that carries the price.
3. 03 The client signs a USDC payment and retries with a `PAYMENT-SIGNATURE` header.
4. 04 The server verifies the payment and settles it on Base through the x402 facilitator.
5. 05 The payment becomes credit on a balance tied to your wallet address. The request is metered against that balance, the same way as a request made with an API key.
6. 06 The response carries a `PAYMENT-RESPONSE` header with the settlement receipt.

## Install

Install the x402 client SDK and a wallet library.

Install x402 client with EVM support

```
npm install @x402/core @x402/evm @x402/fetch viem
```

You need an EVM wallet holding USDC on [Base](https://www.base.org/). Put its private key in `EVM_PRIVATE_KEY`.

## Usage

Call a paid Spider endpoint, here /search. The x402 SDK catches the 402, signs the payment and retries. A call with no Authorization header gets the 402 challenge on /scrape too. Only Authorization: Bearer free opts /scrape into the free tier, and that call skips x402.

Search with x402 in TypeScript

```
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { wrapFetchWithPayment } from "@x402/fetch";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);
const client = new x402Client();
registerExactEvmScheme(client, { signer });

const fetchWithPayment = wrapFetchWithPayment(fetch, client);

const response = await fetchWithPayment("https://api.spider.cloud/search", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    search: "rust web crawler",
    search_limit: 5
  })
});

const data = await response.json();
```

## Supported endpoints

Each route asks for a fixed deposit in the [credit system](https://spider.cloud/docs/core/rate-limits/). 10,000 credits cost $1 in USDC.

| Endpoint    | Deposit credits | Deposit USDC |
| ----------- | --------------- | ------------ |
| /scrape     | 5               | $0.0005      |
| /crawl      | 50              | $0.005       |
| /search     | 20              | $0.002       |
| /screenshot | 10              | $0.001       |
| /links      | 2               | $0.0002      |
| /unblocker  | 10              | $0.001       |
| /transform  | 5               | $0.0005      |

### AI endpoints

| Endpoint      | Deposit credits | Deposit USDC |
| ------------- | --------------- | ------------ |
| /ai/scrape    | 100             | $0.01        |
| /ai/crawl     | 500             | $0.05        |
| /ai/search    | 100             | $0.01        |
| /ai/browser   | 100             | $0.01        |
| /ai/links     | 50              | $0.005       |
| /ai/unblocker | 50              | $0.005       |

The deposit is credited to your wallet's balance, and the request is billed by what it uses. A crawl stops when the balance reaches zero. Credit left over stays with your wallet address for the next paid request.

## Protocol details

Spider implements x402 v2.

| Network              | Base mainnet, `eip155:8453`                                                                |
| -------------------- | ------------------------------------------------------------------------------------------ |
| Asset                | USDC                                                                                       |
| Scheme               | `exact`, a fixed deposit per route                                                         |
| Challenge header     | `PAYMENT-REQUIRED`, base64 JSON                                                            |
| Payment header       | `PAYMENT-SIGNATURE`, base64 signed payload                                                 |
| Settlement header    | `PAYMENT-RESPONSE`, base64 JSON with `success`, `transaction`, `network` and `payer`       |
| Facilitator          | [docs.x402.org/core-concepts/facilitator](https://docs.x402.org/core-concepts/facilitator) |
| Verification timeout | 30s                                                                                        |

## x402 or an API key

x402 fits an autonomous agent that pays per request with no account set up ahead of time. When you control the client, an [API key](https://spider.cloud/api-keys/) is simpler and needs no wallet.

### Resources

The protocol spec and the reference client.

[x402 protocol docs ](https://docs.x402.org)[GitHub repo](https://github.com/coinbase/x402)
