This directory contains a collection of TypeScript examples demonstrating how to use the X402 protocol in various contexts. These examples are designed to work with the X402 npm packages and share a workspace with the main X402 packages.
Before running any examples, you need to install dependencies and build the packages:
# From the examples/typescript directory
pnpm install
pnpm buildThe examples are organized into several categories:
Examples of different client implementations for interacting with X402 services:
clients/axios/- Axios client with x402 payment interceptor fromx402-axios.clients/fetch/- Client using thex402-fetchwrapper around the native fetch API.clients/cdp-sdk/- Client that uses CDP Server Wallets as the signer withx402-axios.clients/chainlink-vrf-nft/- Example using Chainlink to mint a randomized NFT (see them on Opensea). Demonstrates verify/settle flow withx402-axios.
agent/- Anthropic agent that pays via a Go proxy usingx402-fetch.dynamic_agent/- Agent that discovers tools dynamically and pays per-request using x402.
discovery/- Uses the facilitator to list available x402-protected resources (Bazaar).
mcp/- MCP server that makes paid API requests viax402-axios(Claude Desktop compatible).mcp-embedded-wallet/- Electron-based MCP server with an embedded wallet that signs requests via IPC.
facilitator/- Example implementation of an x402 payment facilitator exposing/verifyand/settle.
fullstack/next/- Next.js app demonstrating route protection withx402-nextmiddleware.fullstack/mainnet/- Next.js app configured for Base mainnet using the Coinbase hosted facilitator.fullstack/next-advanced/- [WIP] Deep Next.js integration using a paywall + session cookie after verify/settle.fullstack/browser-wallet-example/- Browser wallet template: Hono server + React client with session and one-time payments.fullstack/farcaster-miniapp/- Farcaster Mini App template with x402-protected APIs using MiniKit.fullstack/auth_based_pricing/- SIWE + JWT with conditional pricing ($0.01 with JWT vs $0.10 without) using x402.
Examples of different server implementations:
servers/express/- Express.js server usingx402-expressmiddleware.servers/hono/- Hono server usingx402-honomiddleware.servers/advanced/- Express server without middleware: delayed settlement, dynamic pricing, multiple requirements.servers/mainnet/- Server example for accepting real USDC on Base mainnet using the Coinbase hosted facilitator.
Each example directory contains its own README with specific instructions for running that example. Navigate to the desired example directory and follow its instructions.
This workspace uses:
- pnpm for package management
- Turborepo for monorepo management
- TypeScript for type safety
The examples are designed to work with the main X402 packages, so they must be built before running any examples.
The examples in this folder commonly use private keys to sign messages. Never put a private key with mainnet funds in a .env file. This can result in keys getting checked into codebases and being drained.
There are many ways to generate a keypair to use exclusively for development, one way is via foundry:
# install foundry
curl -L https://foundry.paradigm.xyz | bash
# generate a new wallet
cast w new
You can fund your new wallet on most networks via the testnet CDP Faucet, simply provide the address generated by cast.