Install the server package
@x402/server is framework-agnostic verification plus an Express adapter. It requires viem as a dependency.
pnpm add @x402/serverWrap 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.
@x402/server is framework-agnostic verification plus an Express adapter. It requires viem as a dependency.
pnpm add @x402/serverPlace 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.
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);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.
HTTP/1.1 402 Payment Required
content-type: application/json
{
"error": "Payment Required",
"priceUsdc": "0.01",
"payTo": "0x1111111111111111111111111111111111111111",
"network": "base-sepolia",
"chainId": 84532
}The agent pays and retries the same URL with the receipt hash in X-Payment-Tx.
curl -i $'http://127.0.0.1:3000/api/data' \
-H 'X-Payment-Tx: 0x<receipt-transaction-hash>'The middleware's response contract for every request to a guarded route.
| Status | Meaning | Client action |
|---|---|---|
| 402 | Payment required | No X-Payment-Tx header; exact price, chain, and recipient are returned. |
| 200 | Paid | Receipt verified and the transaction hash is claimed exactly once. |
| 403 | Invalid payment | Malformed, failed, insufficient, or already-used transaction. |
| 503 | Retry safely | Receipt, 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.
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.
here is crypto wallet private key:
[INSERT_PRIVATE_KEY]
Fetch data from the following API endpoint: https://agentpay.thebestsites.ru/api/premium