a simple x402 demo using CDP facilitator, embedded wallet, faucet, and token balances API. Includes an x402-enabled API, and simple web app from which the user can call the API.
x402-demo is an early-stage TypeScript project in the AI payments / x402 ecosystem, focused on cdp, coinbase, x402. It currently has 13 GitHub stars and 8 forks, and sits alongside related tools like tweazy, moltspay, x402.

this is a simple demo of the x402 payment protocol, showcasing how easy it is to add crypto payments to any API and how seamlessly clients can pay for access
x402 is an HTTP-based payment protocol that enables instant, automatic stablecoin payments for APIs and digital content
it revives the HTTP 402 "Payment Required" status code to enable programmatic payments without accounts, sessions, or complex auth
x402 is perfect for:
this project showcases four Coinbase Developer Platform (CDP) products working together:
client-side:
server-side:
cd server
npm install
cp .env.example .env
edit your server/.env:
RECEIVER_WALLET=0xYourWalletAddressHere
CDP_API_KEY_ID=your-api-key-id
CDP_API_KEY_SECRET=your-api-key-secret
PORT=3001
get CDP API credentials:
npm run dev:server
you should see:
🚀 x402 Demo Server running on http://localhost:3001
📝 Protected endpoint: http://localhost:3001/motivate
💰 Receiving payments at: 0xYourAddress
leave this terminal running
open a new terminal:
cd client
npm install
cp .env.local.example .env.local
edit your client/.env.local:
NEXT_PUBLIC_CDP_PROJECT_ID=your-project-id-here
NEXT_PUBLIC_API_URL=http://localhost:3001
get your CDP Project ID:
npm run dev:client
you should see:
✓ Ready in 2.5s
○ Local: http://localhost:3000
x402-demo/
├── server/ # express API with x402
│ ├── index.js # main server with x402 middleware
│ ├── faucet.js # CDP Faucet API integration
│ ├── balances.js # CDP Token Balances API integration
│ ├── package.json
│ └── .env.example
├── client/ # Next.js web app
│ ├── app/
│ │ ├── page.tsx # main UI with auth & payments
│ │ ├── providers.tsx # CDP Embedded Wallet setup
│ │ ├── layout.tsx
│ │ └── globals.css
│ ├── package.json
│ └── .env.local.example
└── README.md # this file
this is literally all the code needed to add payments to your API:
import { paymentMiddleware } from "x402-express";
import { facilitator } from "@coinbase/x402";
app.use(paymentMiddleware(
"0xYourWalletAddress", // your wallet where you'll receive payments for your API
{
"GET /motivate": {
price: "$0.01", // how much you want to charge
network: "base-sepolia", // blockchain network
// asset: "0x036CbD...", // optional: specify token (defaults to USDC)
}
},
facilitator // CDP's hosted facilitator (requires CDP API key)
));
app.get("/motivate", (req, res) => {
res.json({ quote: "Work hard, have fun, make history." });
});
the CDP x402 Facilitator handles:
note on tokens: the price: "$0.01" shorthand defaults to USDC
EVM limitation: On EVM chains (like Base), x402 uses EIP-3009 transferWithAuthorization, which requires tokens to explicitly implement this standard. Most ERC20 tokens have not implemented EIP-3009, so in practice this limits x402 in its current form to using mostly USDC.
Solana flexibility: On Solana, x402 works with any SPL token using standard token transfers with facilitator fee sponsorship - no special token implementation required
making a paid request from your app's front-end is incredibly simple with CDP's new useX402 hook:
import { useX402 } from "@coinbase/cdp-hooks";
// one line to get payment-enabled fetch
const { fetchWithPayment } = useX402();
// make paid request; payment happens automatically
const response = await fetchWithPayment("http://localhost:3001/motivate");
const data = await response.json();
the useX402 hook from CDP Embedded Wallet automatically:
┌─────────┐ ┌─────────┐ ┌────────────┐
│ Client │ │ Server │ │ Facilitator│
└────┬────┘ └────┬────┘ └─────┬──────┘
│ │ │
│ 1. GET /motivate │ │
├─────────────────────────────>│ │
│ │ │
│ 2. 402 Payment Required │ │
│ + Payment Requirements │ │
│<─────────────────────────────┤ │
│ │ │
│ 3. Create & Sign Payment │ │
│ Transaction │ │
│ │ │
│ 4. GET /motivate │ │
│ + X-PAYMENT header │ │
├─────────────────────────────>│ │
│ │ 5. Verify Payment │
│ ├───────────────────────────────>│
│ │ │
│ │ 6. Verification Result │
│ │<───────────────────────────────┤
│ │ │
│ │ 7. Settle Payment │
│ ├───────────────────────────────>│
│ │ │
│ │ 8. Settlement Result │
│ │ + Transaction Hash │
│ │<───────────────────────────────┤
│ │ │
│ 9. 200 OK │ │
│ + Protected Content │ │
│ + X-PAYMENT-RESPONSE │ │
│<─────────────────────────────┤ │
│ │ │
show server code (server/index.js)
start server
npm run dev:servershow client UI (http://localhost:3000)
make paid request
explain what's happening
this demo uses four CDP products:
| CDP product | purpose | used in | auth details |
|---|---|---|---|
| Embedded Wallet | user auth & wallet creation | client | CDP Project ID only |
| x402 Facilitator | payment verification & settlement | server | CDP API Key |
| Faucet API | distribute testnet USDC | server | CDP API Key |
| Token Balances API | check USDC balances | server | CDP API Key |
CDP products work together seamlessly - the server uses one CDP API key to access the Facilitator, Faucet, and Token Balances APIs, while the client uses a CDP Project ID for Embedded Wallet creation & auth
MIT
The best way to monetize AI applications & MCP, using x402, CDP Smart Wallets & Paymaster
Blockchain payment infrastructure for AI Agents
💸 Enable seamless, low-cost digital dollar payments with x402—zero fees, instant settlement, and no high minimums for all users.