A proof-of-concept demonstrating the x402 payment protocol developed by Coinbase. This project includes both a web interface and CLI tools to show how HTTP 402 "Payment Required" enables seamless micropayments for API access using stablecoins.
x402-demo is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 3 GitHub stars and 0 forks.
A proof-of-concept demonstrating the x402 payment protocol developed by Coinbase. This project includes both a web interface and CLI tools to show how HTTP 402 "Payment Required" enables seamless micropayments for API access using stablecoins.
x402 is an open payment protocol that enables instant, automatic stablecoin payments directly over HTTP. It uses the HTTP 402 "Payment Required" status code to request payment for resources.
Key Features:
Get started with x402 in 5 minutes!
# 1. Clone and install dependencies
npm install
# 2. Generate a test wallet
npm run generate-wallet
Save the wallet address and private key shown.
Perfect for presentations and demos!
# 1. Build the project
npm run build
# 2. Start the server (simulated payments - instant, no setup)
npm run dev:server
# 3. Open your browser
http://localhost:3402
You'll see a modern web interface with:
For real on-chain payments:
npm run start:real
(Requires testnet USDC - see Real Payments section below)
Start the Server:
npm run dev:server
Test Free Endpoint:
npm run dev:client test-public
Make Paid Request:
npm run dev:client request --endpoint /api-call --key YOUR_PRIVATE_KEY
View Available Endpoints:
npm run dev:client info
CLI-based-x402-Poc/
├── src/
│ ├── types.ts # x402 protocol type definitions
│ ├── facilitator.ts # Payment verification and settlement
│ ├── server.ts # Resource server requiring payments
│ ├── client.ts # CLI client for making paid requests
│ └── generate-wallet.ts # Utility to generate test wallets
├── public/
│ ├── index.html # Web demo interface
│ └── styles.css # Modern dark theme styling
├── package.json
├── tsconfig.json
└── README.md
X-PAYMENT header┌─────────┐ ┌─────────────┐ ┌──────────────┐
│ Client │───── GET ─────>│ Server │ │ Facilitator │
│ │ │ (402) │ │ Service │
└─────────┘ └─────────────┘ └──────────────┘
│ │ │
│ Payment Requirements │ │
│<───────────────────────────│ │
│ │ │
│ Sign Payment (EIP-712) │ │
│────────┐ │ │
│ │ │ │
│<───────┘ │ │
│ │ │
│ GET + X-PAYMENT Header │ │
│───────────────────────────>│ │
│ │ │
│ │ Verify Payment │
│ │────────────────────────────>│
│ │ │
│ │ Settle Payment (on-chain) │
│ │────────────────────────────>│
│ │ │
│ │ Settlement Result │
│ │<────────────────────────────│
│ │ │
│ Protected Resource │ │
│<───────────────────────────│ │
│ │ │
| Endpoint | Price | Description |
|---|---|---|
/public |
FREE | Public endpoint, no payment required |
/api-call |
0.1 USDC | Generic API call example |
/premium-data |
1.0 USDC | Premium market data and insights |
By default, the server runs in simulated mode (perfect for demos). To use real blockchain transactions:
Get Base Sepolia ETH (for gas, optional):
Get Testnet USDC:
npm run generate-wallet)Verify Balance:
# Build the project
npm run build
# Start server with real facilitator
npm run start:real
This uses the x402.org testnet facilitator which submits actual transactions to Base Sepolia blockchain.
# Web Interface
http://localhost:3402
# Or CLI
npm run dev:client request --endpoint /api-call --key YOUR_PRIVATE_KEY
After payment, you'll get a real transaction hash. View it on:
This PoC uses the exact payment scheme with EIP-3009 (transferWithAuthorization). This allows gasless transfers where:
{
domain: {
name: "USD Coin",
version: "2",
chainId: 84532, // Base Sepolia
verifyingContract: "0x036CbD..." // USDC contract
},
types: {
TransferWithAuthorization: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" },
{ name: "validBefore", type: "uint256" },
{ name: "nonce", type: "bytes32" }
]
}
}
The payment is sent as a base64-encoded JSON object:
{
"x402Version": 1,
"scheme": "exact",
"network": "base-sepolia",
"payload": {
"from": "0x...",
"to": "0x...",
"value": "1000000",
"validAfter": 1234567890,
"validBefore": 1234568190,
"nonce": "0x...",
"signature": "0x..."
}
}
npm run dev:server
Benefits:
Use for:
npm run start:real
Benefits:
Use for:
Step 1: Introduction (1 min)
"I'll show you x402 - a protocol that enables micropayments for APIs. It makes payments as simple as clicking a button."
Step 2: Connect Wallet (30 sec)
Step 3: Free Content (30 sec)
Step 4: Paid Content - The Magic! (2 min)
Step 5: Premium Content (1 min, optional)
Benefits:
Use Cases:
Q: How is this different from Stripe?
Stripe is great for traditional payments, but has minimums and fees that make micropayments impractical. x402 enables payments as low as a tenth of a cent with near-zero fees.
Q: What about gas fees?
x402 uses EIP-3009 for gasless transfers. The facilitator handles gas fees, so users just pay the content price.
Q: Which blockchains are supported?
Base, Solana, Polygon, Avalanche, Sei, and more. It's chain-agnostic by design.
Q: How long to integrate?
About 30 minutes for basic integration. SDKs available for Node.js, Python, Java, and Go.
Compile TypeScript to JavaScript:
npm run build
npm run server # Start server (simulated)
npm run server:real # Start server (real payments)
npm run client # Run CLI client
Change Brand Colors (edit public/styles.css):
:root {
--primary-color: #YOUR_COLOR;
--secondary-color: #YOUR_COLOR;
}
Update Pricing (edit src/server.ts):
// Change amounts (in smallest unit, 6 decimals for USDC)
sendPaymentRequired(res, '/api-call', '50000'); // 0.05 USDC
Customize Card Text (edit public/index.html):
<h3>Your Use Case</h3>
<p class="card-desc">Tailored description here</p>
Make sure you've:
npm run generate-walletCheck that:
npm run dev:server)Ensure:
Solution:
npm run dev:serverSolution:
0xnpm run generate-walletImportant: This is a PoC for educational purposes:
.env (never commit this file)This PoC demonstrates the protocol but has some simplifications:
This is a learning project! Feel free to:
After clicking "Pay & Access", you'll see a 5-step animation:
Plus transaction details with amount, network, and tx hash.
╔════════════════════════════════════════════════════════════╗
║ x402 Payment Flow - Starting ║
╚════════════════════════════════════════════════════════════╝
[1/5] Wallet initialized
Address: 0x1234...5678
[2/5] Making initial request to /premium-data
Status: 402 Payment Required ✓
[3/5] Payment requirement received:
Scheme: exact
Network: base-sepolia
Amount: 1.000000 USDC
Recipient: 0x742d...0bEb
Timeout: 300s
[4/5] Creating payment authorization...
[5/5] Submitting request with payment...
╔════════════════════════════════════════════════════════════╗
║ ✓ Payment Successful! ║
╚════════════════════════════════════════════════════════════╝
Response Data:
{
"message": "Payment successful! Here is your premium data.",
"data": {
"secret": "This is valuable premium content!",
...
},
"payment": {
"txHash": "0xabc123...",
"network": "base-sepolia"
}
}
Built with x402 by Coinbase
The agent-native LLM router for autonomous agents. 55+ models (8 free), <1ms local routing, USDC payments on Base & Solana via x402.
A payments protocol for the internet. Built on HTTP.
A local-first AI agent with persistent memory, emotional intelligence, and a peer-to-peer skills economy.
The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.
Self-healing infrastructure for AI agent payments. 90.3% auto-recovery.
The AI agent with a wallet — spends USDC autonomously to get real work done. Apache-2.0, TypeScript.