A Next.js application demonstrating the x402 Protocol for blockchain-based micropayments on Base Network. This project showcases payment-gated content with dual access methods: beautiful browser UIs and JSON API endpoints for AI agents.
x402 is a protocol that enables HTTP 402 (Payment Required) status for the modern web, allowing content creators to monetize their APIs and digital content using cryptocurrency micropayments. This demo shows how to integrate x402 payments into a Next.js application with proper blockchain settlement.
This project demonstrates three payment-gated content offerings with dual access methods:
- Beautiful presentation pages with OnchainKit wallet integration
- Seamless payment flow with USDC on Base Network
- User-friendly payment success pages with direct download links
- JSON endpoints for AI agents and programmatic access
- Same payment verification through x402 middleware
- Perfect for integrating with AI tools and automated systems
Routes: /message β /message/user-payment (Browser) | /api/message (API)
Simple message endpoint demonstrating the basic x402 flow. Perfect for testing and understanding the protocol.
What you get:
- Access to protected message content
- Test x402 micropayment protocol
- Instant blockchain settlement
Routes: /ui-ux-book β /ui-ux-book/user-payment (Browser) | /api/ui-ux-book (API)
Comprehensive UI/UX design book with practical resources.
What's included:
- 322 Pages of insights and practical examples
- Figma Files - Design templates ready to use
- AI Prompts - Leverage AI for faster design workflows
- Practical Examples - Real-world case studies and applications
Browser access shows a beautiful payment success page with download links. API access returns JSON with the same download links for programmatic access.
Routes: /shadcn-blocks β /shadcn-blocks/user-payment (Browser) | /api/shadcn-block (API)
Premium UI component blocks from Creative Tim built with shadcn/ui.
What you get:
- Creative Tim x shadcn/ui CRUD form block
- Complete component and page code
- Ready to copy and use in your project
Browser Users:
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ
β Presentation βββββββΆβ User Payment βββββββΆβ Beautiful UI β
β Page β β Page (Protected)β β with Downloads β
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ
β
βΌ
ββββββββββββββββ
β x402 β
β Middleware β
ββββββββββββββββ
API/AI Agents:
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ
β API Request βββββββΆβ x402 βββββββΆβ JSON Response β
β β β Middleware β β with Data β
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ
- User visits presentation page (e.g.,
/ui-ux-book) - User clicks "Get Access" button
- Redirected to payment page (
/ui-ux-book/user-payment) - x402 middleware intercepts and shows OnchainKit payment UI
- User connects wallet (MetaMask, Coinbase Wallet, etc.)
- User completes USDC payment on Base Network
- Payment settled on blockchain via @coinbase/x402 facilitator
- User redirected to success page with download links
- Client makes API request to endpoint (e.g.,
/api/ui-ux-book) - x402 middleware intercepts and returns 402 Payment Required
- Client receives payment details (amount, wallet, chain)
- Client completes payment with Web3 wallet
- Client retries request with payment proof headers
- Server verifies and settles payment
- Server returns JSON with content/download links
- Node.js 18+ and npm/pnpm
- A cryptocurrency wallet (MetaMask recommended)
- For testnet: Free testnet USDC from Base Sepolia faucet
- For mainnet: Real USDC on Base + Coinbase CDP API keys
- Clone the repository
git clone https://github.com/creativetimofficial/x402-creative-tim
cd x402-creative-tim- Install dependencies
npm install
# or
pnpm install- Configure environment variables
Copy .env.example to .env:
cp .env.example .envEdit .env with your configuration:
# Network Configuration
NETWORK=mainnet
# Wallet Addresses for receiving payments
NEXT_PUBLIC_MAINNET_WALLET_ADDRESS=0xYourMainnetWallet
NEXT_PUBLIC_TESTNET_WALLET_ADDRESS=0xYourTestnetWallet
# CDP API Keys (required for mainnet)
CDP_API_KEY_ID=your-cdp-api-key-id
CDP_API_KEY_SECRET=your-cdp-api-key-secret
# OnchainKit API key (optional but recommended)
NEXT_PUBLIC_ONCHAINKIT_API_KEY=your-onchainkit-api-key
# App URL
NEXT_PUBLIC_APP_URL=http://localhost:3001
# Book Download URLs (for UI/UX book content)
BOOK_URL_PAID=https://your-dropbox-url
NEXT_PUBLIC_BOOK_URL_PREVIEW=https://your-preview-pdf-url- Run the development server
npm run dev- Open your browser
Visit http://localhost:3001 and start exploring!
The application supports both Base Mainnet (production) and Base Sepolia (testnet).
Use the network toggle in the navbar to switch between:
- Testnet (Base Sepolia) - Free testnet USDC
- Mainnet (Base) - Real USDC payments
- Network: Base
- Chain ID: 8453
- USDC:
0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 - Settlement: @coinbase/x402 facilitator with CDP API keys
- Requires: Real USDC and CDP API credentials
- Network: Base Sepolia
- Chain ID: 84532
- USDC:
0x036CbD53842c5426634e7929541eC2318f3dCF7e - Settlement: Free x402.org facilitator
- Faucet: https://www.alchemy.com/faucets/base-sepolia
export const ENDPOINT_PRICING = {
'message': 0.10, // $0.10 USDC
'ui-ux-book': 10.00, // $10.00 USDC
'shadcn-block': 0.01, // $0.01 USDC
}Prices are centrally configured in lib/x402-config.ts and automatically apply to:
- Presentation page badges
- Purchase buttons
- Middleware payment verification
- API route protection
- Success page displays
/- Homepage with all offerings/message- Message content presentation/ui-ux-book- UI/UX book presentation/shadcn-blocks- Component blocks presentation
/message/user-payment- Access message after $0.10 payment/ui-ux-book/user-payment- Download book after $10.00 payment/shadcn-blocks/user-payment- View component code after $0.01 payment
/api/message- Returns JSON message content/api/ui-ux-book- Returns JSON with download links/api/shadcn-block- Returns JSON with component code
- Next.js 14 with App Router
- TypeScript for type safety
- Tailwind CSS for styling
- shadcn/ui for UI components
- OnchainKit for wallet integration and payment UI
- x402 Protocol via
x402-nextpackage - @coinbase/x402 facilitator for mainnet settlement
- Base Network (mainnet and Sepolia testnet)
- USDC stablecoin for all payments
- Coinbase CDP for mainnet transaction settlement
- Next.js Middleware for route protection
- Server Components for payment-protected pages
- Official x402 middleware with proper blockchain settlement
x402-creative-tim/
βββ app/
β βββ api/ # API routes (return JSON)
β β βββ message/route.ts # Message API ($0.10)
β β βββ ui-ux-book/route.ts # Book API ($10.00)
β β βββ shadcn-block/route.ts # Component API ($0.01)
β βββ message/
β β βββ page.tsx # Message presentation page
β β βββ user-payment/page.tsx # Payment success page
β βββ ui-ux-book/
β β βββ page.tsx # Book presentation page
β β βββ user-payment/page.tsx # Payment success page
β βββ shadcn-blocks/
β β βββ page.tsx # Blocks presentation page
β β βββ user-payment/page.tsx # Payment success page
β βββ layout.tsx # Root layout
β βββ page.tsx # Homepage
β βββ providers.tsx # OnchainKit providers
βββ lib/
β βββ x402-config.ts # Pricing & network config
β βββ x402-middleware-official.ts # Official middleware config
β βββ network-context.tsx # Network switching context
βββ components/
β βββ ui/ # shadcn/ui components
β βββ navbar.tsx # Navigation with network toggle
β βββ client-layout.tsx # Client wrapper
β βββ analytics.tsx # Google Tag Manager
βββ middleware.ts # Route protection
βββ .env.example # Environment template
- All code in this repository
.env.examplefile- Public wallet addresses (for receiving payments)
- Frontend code and UI components
.envfiles (already in .gitignore)- CDP API keys (sensitive credentials)
- Book download URLs (paid content)
- Private keys or seed phrases
- BOOK_URL_PAID - Server-only, not exposed to client
- NEXT_PUBLIC_BOOK_URL_PREVIEW - Public, safe to expose (free preview)
- CDP_API_KEY_SECRET - Server-only, never exposed
Edit lib/x402-config.ts:
export const ENDPOINT_PRICING = {
'message': 0.10, // Change to your price
'ui-ux-book': 10.00, // Change to your price
'shadcn-block': 0.01, // Change to your price
}All pages automatically update to reflect new pricing.
- Add pricing to
lib/x402-config.ts - Add route config to
lib/x402-middleware-official.ts - Add matcher to
middleware.ts - Create presentation page:
app/your-content/page.tsx - Create payment page:
app/your-content/user-payment/page.tsx - Create API route (optional):
app/api/your-content/route.ts
- Global styles:
app/globals.css - Components:
components/ui/(shadcn/ui) - Navbar:
components/navbar.tsx - Layout:
components/client-layout.tsx
- Switch to testnet using the navbar toggle
- Get free testnet USDC from the faucet
- Test all payment flows with $0 cost
- Verify blockchain settlement on Base Sepolia explorer
- Switch to mainnet using the navbar toggle
- Use real USDC for payments
- Verify transactions on Base mainnet explorer
- Monitor CDP API usage
- Push code to GitHub
- Import project in Vercel
- Add environment variables
- Deploy
NETWORK=mainnet
NEXT_PUBLIC_MAINNET_WALLET_ADDRESS=0x...
NEXT_PUBLIC_TESTNET_WALLET_ADDRESS=0x...
CDP_API_KEY_ID=...
CDP_API_KEY_SECRET=...
NEXT_PUBLIC_ONCHAINKIT_API_KEY=...
NEXT_PUBLIC_APP_URL=https://your-domain.com
BOOK_URL_PAID=https://your-dropbox-url
NEXT_PUBLIC_BOOK_URL_PREVIEW=https://your-preview-urlThis project uses specific wallet addresses for receiving payments:
- Mainnet:
0x45ac00db8bdd4b837abbbf75888cbdfe6b6d8943 - Testnet:
0x92a0f8ac8b8c2ef60d6d46e0f768067fa379f7f3
Configure your own addresses in the .env file.
- x402 Protocol - Official protocol documentation
- x402-next Package - Next.js middleware
- Base Network - Layer 2 documentation
- OnchainKit - Coinbase Web3 toolkit
- shadcn/ui - UI component library
- Coinbase Developer Platform - Get API keys
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Test thoroughly on testnet
- Submit a pull request
Apache-2.0 License
- x402 Protocol for the payment infrastructure
- Creative Tim UI for the UI components and design
- Coinbase for Base Network and OnchainKit
- shadcn for the beautiful UI component system
Built with β€οΈ by Creative Tim UI using Next.js, x402 Protocol, and Base Network
For questions or issues, please open an issue on GitHub.