anybrowse

x402 Payment Integration Guide Micropayments

Pay per request with USDC on Base. No subscription, no signup — just a crypto wallet.

What is x402?

x402 is an open HTTP payment protocol that lets API clients pay for individual requests using USDC on Base (Ethereum L2). When your free tier runs out, instead of signing up for a subscription you can pay $0.002 per scrape directly from your wallet — no account required. The server returns an HTTP 402 with cryptographic payment requirements, your wallet signs and submits the payment on-chain, and you resend the request with an X-PAYMENT header. The x402 client library handles all of this automatically.

When should I use x402? If your AI agent has a Coinbase wallet (e.g. via AgentKit), x402 is the easiest option — no signup needed. For high-volume use, the Pro subscription ($4.99/month for 10k scrapes) is cheaper. The MCP endpoint is free and unlimited for agents.

Quickstart — AI Agents (Coinbase AgentKit)

If you're building with Coinbase AgentKit, x402 is handled automatically.

1

Install AgentKit

npm install @coinbase/agentkit

2

Create a wallet and fund it with USDC on Base

Your agent needs a small amount of USDC on Base mainnet (a few cents is enough to start).

3

Make requests — AgentKit handles x402 automatically

AgentKit intercepts 402 responses, pays, and retries. No code changes needed beyond initializing AgentKit with your wallet.

Quickstart — Developers (x402 npm package)

The x402 npm package wraps the standard fetch function to automatically handle 402 payment challenges.

1

Install the package

npm install x402 @coinbase/agentkit viem

2

Create a wallet

Fund it with USDC on Base. Each scrape costs $0.002 USDC.

3

Wrap fetch and make requests

wrapFetch(fetch, wallet) — the library handles payment automatically when you hit a 402.

Working Code Examples

JavaScript / TypeScript

TypeScript
import { wrapFetch } from 'x402/client';
import { createWallet } from '@coinbase/agentkit';

// Create and fund a Coinbase wallet (USDC on Base)
const wallet = await createWallet();
const fetch402 = wrapFetch(fetch, wallet);

// This automatically handles 402 payment challenges
const response = await fetch402('https://anybrowse.dev/scrape', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ url: 'https://example.com' })
});

const { markdown } = await response.json();
console.log(markdown);

Python

Python
from x402.client import wrap_httpx
import httpx

# wallet must be a Coinbase CDP wallet or compatible signer
client = wrap_httpx(httpx.Client(), wallet)

response = client.post(
    'https://anybrowse.dev/scrape',
    json={'url': 'https://example.com'}
)

data = response.json()
print(data['markdown'])

With viem (custom wallet)

TypeScript
import { wrapFetch } from 'x402/client';
import { createWalletClient, http } from 'viem';
import { base } from 'viem/chains';
import { privateKeyToAccount } from 'viem/accounts';

const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const walletClient = createWalletClient({
  account,
  chain: base,
  transport: http()
});

const fetch402 = wrapFetch(fetch, walletClient);

// Crawl example — $0.01 per call
const response = await fetch402('https://anybrowse.dev/crawl', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: 'best web scraping tools 2025', limit: 5 })
});

const { results } = await response.json();

Pricing

Scrape
$0.002
POST /scrape
Convert URL → Markdown
Crawl
$0.01
POST /crawl
Google search + scrape
Search
$0.002
POST /serp/search
SERP results as JSON
Extract
$0.01
POST /extract
Structured JSON from URL

Payments are USDC on Base (mainnet). The x402 protocol uses the Coinbase facilitator. Gas is minimal — Base fees are fractions of a cent.

For high-volume use, compare: Pro subscription at $4.99/month gives 10,000 scrapes (≈$0.0005/call).

Payment Details

Troubleshooting

Error Cause Fix
paymentPayload is invalid Manually constructed X-PAYMENT header with wrong format or encoding Use the x402 npm package or Coinbase AgentKit. Do not manually construct the header.
Invalid X-PAYMENT header — expected base64 JSON X-PAYMENT value is not valid base64 or not valid JSON when decoded The header must be a base64-encoded JSON object. Always use the x402 client library to generate it.
Payment already used Replay attack — same payment submitted twice Each payment is single-use. The x402 library generates a fresh payment per request automatically.
Payment verification error — try again Temporary facilitator error Retry the request. If persistent, check Base network status.
Insufficient funds Wallet USDC balance is too low Fund your wallet with USDC on Base mainnet. A few cents is enough for many requests.
Daily free limit reached 50 free scrapes/day per IP exhausted Use x402 micropayments (this guide), subscribe to Pro ($4.99/month), or use the MCP endpoint (unlimited for agents).

Common Mistake: Manually Constructing X-PAYMENT

The most frequent error is attempting to manually construct the X-PAYMENT header. This requires EIP-712 signing, USDC permit2 encoding, and specific JSON structure — it's complex and error-prone. Always use the x402 client library.

❌ Don't do this
// Wrong — do not manually set X-PAYMENT
headers['X-PAYMENT'] = btoa(JSON.stringify({ amount: '2000', to: '0x...' }));
✅ Do this instead
import { wrapFetch } from 'x402/client';
// wrapFetch handles all payment signing and header construction
const fetch402 = wrapFetch(fetch, wallet);
const response = await fetch402('https://anybrowse.dev/scrape', { ... });

Resources