A simple demonstration of how to build a pay-to-play web application using the Coinbase Developer Platform (CDP) and X402 protocol.
This example shows how to integrate microtransactions into an Express.js app using x402-express and how to consume those APIs securely using x402-fetch.
This demo application demonstrates:
- CDP SDK Integration – Setting up a CDP account using the Coinbase SDK
- Payment Middleware – Using
x402-expressto monetize API endpoints - X402 Payments – Handling paid fetch requests via
x402-fetch - Game Logic – Building a simple Rock–Paper–Scissors game with payment enforcement
- Catalog Discovery – Listing available X402 APIs from the CDP catalog
- Node.js 18 or higher
- pnpm package manager
- A Coinbase Developer Platform (CDP) account
- Access to Base Sepolia testnet
- Install dependencies:
pnpm install- Create a
.envfile in the root directory and include your CDP credentials:
CDP_API_KEY_ID=your_api_key_id_here
CDP_API_KEY_SECRET=your_api_key_secret_here
CDP_WALLET_SECRET=your_wallet_secret_here- Visit the CDP Portal
- Create a new project or select an existing one
- Navigate to Settings > API Keys
- Click Create API Key
- Download the credentials and place them in your
.envfile
You can run both the seller (API server) and buyer (client) parts of the demo.
node index.jsThis server exposes a single paid endpoint:
-
POST /rps/play
- Price:
$0.001(Base Sepolia testnet) - Description: “Pay to play Rock–Paper–Scissors”
- Input:
{ "move": "rock" | "paper" | "scissors" } - Output:
{ "serverMove": "...", "userMove": "...", "outcome": "win|lose|draw" }
- Price:
Example console output:
Server listening at http://localhost:4021
node buyer.jsThe script uses:
@coinbase/cdp-sdkto load your CDP accountx402-fetchto make paid requestsdecodeXPaymentResponseto read payment receipts
Example output:
Game result: { serverMove: 'rock', userMove: 'paper', outcome: 'win' }
Payment receipt: {...}
node list-catalog.jsThis will fetch and print all X402 resources listed in the CDP discovery catalog.
- Never commit your
.envfile – Add it to.gitignore - Use testnets – Test on Base Sepolia before mainnet
- Rotate keys regularly – Ensure credential hygiene
- Validate payment receipts – Always verify payment headers before processing