A AgentPayDeveloper docs · v0.1View source on GitHub
AgentPay quickstart

Add a 402 payment gate
to any route.

Wrap an Express route with paymentMiddleware. Agents that call it without a payment header receive an HTTP 402 body with the exact price, network, and recipient; after paying, they retry with X-Payment-Tx and the middleware verifies the receipt before your handler runs.

01

Install the server package

@x402/server is framework-agnostic verification plus an Express adapter. It requires viem as a dependency.

Terminal
pnpm add @x402/server
02

Guard a route with the middleware

Place paymentMiddleware between the URL matcher and your handler. Without aX-Payment-Tx header it returns 402; with a valid receipt it callsnext() and your handler runs normally.

TypeScript
import { paymentMiddleware } from "@x402/server";
import express from "express";

const app = express();

app.get(
  "/api/data",
  paymentMiddleware({
    priceUsdc: "0.01",                          // 1 US cent
    payTo: "0x1111111111111111111111111111111111111111", // your wallet
    chainId: 84532,                             // Base Sepolia
    rpcUrl: process.env.BASE_SEPOLIA_RPC_URL!,
  }),
  (_request, response) => {
    response.json({ data: "Here is your premium data" });
  },
);

app.listen(3000);
03

The 402 payment requirement

An unauthenticated request never reaches your data. It receives exact payment terms an agent can act on: price in USDC (6-decimal base units), your recipient, and the chain.

Live 402 response
HTTP/1.1 402 Payment Required
content-type: application/json

{
  "error": "Payment Required",
  "priceUsdc": "0.01",
  "payTo": "0x1111111111111111111111111111111111111111",
  "network": "base-sepolia",
  "chainId": 84532
}

Paid request

The agent pays and retries the same URL with the receipt hash in X-Payment-Tx.

Terminal
curl -i $'http://127.0.0.1:3000/api/data' \
  -H 'X-Payment-Tx: 0x<receipt-transaction-hash>'
04

Status codes

The middleware's response contract for every request to a guarded route.

StatusMeaningClient action
402Payment requiredNo X-Payment-Tx header; exact price, chain, and recipient are returned.
200PaidReceipt verified and the transaction hash is claimed exactly once.
403Invalid paymentMalformed, failed, insufficient, or already-used transaction.
503Retry safelyReceipt, confirmations, or RPC temporarily unavailable.

The middleware accepts an official-USDC Transfer receipt on the configured chain, waits for one confirmation by default, and mounts an in-memory replay claim per transaction hash. Raise confirmations and supply durable replay storage for production traffic.

05

Pay with your own AI agent

Give your agent a task to pay the live AgentPay endpoint and fetch the premium data. Replace [INSERT_PRIVATE_KEY] with the private key your agent should pay from, then paste the prompt into any capable AI agent.

Agent prompt
here is crypto wallet private key: 
[INSERT_PRIVATE_KEY]

Fetch data from the following API endpoint: https://agentpay.thebestsites.ru/api/premium 
REAL FUNDS · BASE MAINNETThe agent transfers real USDC. Use a dedicated low-balance wallet key, never your main key.