Pay with x402.
Get paid with x402.
This page is the tutorial. The server behind it is the demo — of both halves of the protocol. Fund a wallet and pay $0.01 USDC to unlock a skill that teaches your agent to spend money on the resources it needs, and teaches you to gate your own routes so other agents pay you. Base or Solana, settled on-chain — no account needed to pay; one free account needed to get paid.
Pay
How an agent autonomously settles a 402 challenge — including, if you want, one you build yourself in Part B, below.
Generate a wallet
A fresh keypair for Base (EVM) or Solana. Keep the private key in an env var — never send it anywhere, including here.
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
const privateKey = generatePrivateKey(); // 0x... keep secret
const account = privateKeyToAccount(privateKey);
console.log("Base address:", account.address);
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";
const keypair = Keypair.generate();
console.log("Solana address:", keypair.publicKey.toBase58());
console.log("Secret key:", bs58.encode(keypair.secretKey));
Fund it
Send a few cents of USDC to the address from step 1, plus a trace of native gas (ETH on Base, SOL on Solana) — fees on both are fractions of a cent.
// Base — buy USDC on an exchange and withdraw to Base,
// or bridge from Ethereum at bridge.base.org
// Solana — buy/swap USDC on a Solana-compatible wallet
// or exchange, send to your generated address
// $0.01 + gas is enough for many payments through this site.
Pay & fetch the skill
Wrap fetch with a payment-aware client. It handles the 402 challenge, signs, retries, and returns the skill markdown. No account or API key needed on this side — just a funded wallet.
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
const account = privateKeyToAccount(process.env.EVM_PRIVATE_KEY);
const fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {
schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(account) }],
});
const res = await fetchWithPayment("https://YOUR-DEPLOY-URL/skill/base");
const skill = await res.text(); // the full x402 setup skill
Get paid
Gate one of your own routes so other agents have to pay you to use it — this is exactly how /skill/base and /skill/solana on this site work. Unlike Part A, this side does need one account.
Install & pick a facilitator
The facilitator verifies and settles payments on-chain for you. For real mainnet money, that means a free Coinbase Developer Platform account and a Secret API Key — the one piece of account setup in this whole guide, and it's on the receiving side, not the paying side.
import { createCdpFacilitatorClient } from "@coinbase/cdp-sdk/x402";
// pnpm add @x402/core @x402/express @x402/evm @coinbase/cdp-sdk
// Reads CDP_API_KEY_ID / CDP_API_KEY_SECRET from the environment
// (create a Secret API Key at portal.cdp.coinbase.com/api-keys/secret)
const facilitatorClient = createCdpFacilitatorClient();
Gate a route
Register the network, set a price and a payTo address, wrap the route. That's the whole paywall.
import express from "express";
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
const NETWORK = "eip155:8453"; // Base mainnet
const resourceServer = new x402ResourceServer(facilitatorClient)
.register(NETWORK, new ExactEvmScheme());
const routes = {
"GET /report": {
accepts: {
scheme: "exact",
network: NETWORK,
price: "$0.10", // what you charge
payTo: "0xYourWalletAddress", // where it lands
},
},
};
const app = express();
app.use(paymentMiddleware(routes, resourceServer, undefined, undefined, false));
app.get("/report", (req, res) => res.json({ report: "..." }));
Verify you're actually charging
Hit it unpaid. A real 402 with your price and address in the challenge means it's live.
$ curl -i https://your-server/report
HTTP/1.1 402 Payment Required
payment-required: eyJ4NDAyVmVyc2lvbiI6Mi... # base64: your price + payTo
Endpoints
Everything an agent needs to discover and pay, machine-readable — and the worked example for Part B, since this table describes a real gated resource server.
| Route | Access | Returns |
|---|---|---|
| GET /llms.txt | free | Plain-text protocol reference for crawling agents. |
| GET /skill | free | Discovery JSON — price and links to the paid routes. |
| GET /skill/base | $0.01 USDC · Base |
The x402 setup skill (Markdown).
network: eip155:8453 — pay with an
exact EVM payment. |
| GET /skill/solana | $0.01 USDC · Solana |
The x402 setup skill (Markdown).
network: solana:5eykt4Us… — pay with an
exact SVM payment. |
Why this exists
x402 turns HTTP's unused 402 status into a real payment flow — for whichever side of the transaction you're on.
Symmetric by design
The same primitives — scheme, network, exact amount — gate a route and pay one. Learn it once, use it both ways.
No account to pay
A wallet address is the only identity a payer ever needs. Getting paid on mainnet is the one exception — that side needs a free CDP API key for the facilitator.
No checkout
The 402 challenge carries the price and destination. Your fetch client does the rest.
Real settlement
This is mainnet. Payments here are genuine USDC transfers, verified by a facilitator.