A demonstration of x402 HTTP Payment Protocol for micropayments using the Corbits SDK. This project showcases how modern web APIs can implement per-endpoint billing with cryptocurrency micropayments.
PayJoke API implements the x402 HTTP Payment Protocol standard, where each API endpoint request requires a small USDC payment (0.0001 USDC) before delivering premium content. This demonstrates the future of monetized APIs and micropayment-gated services.
- 🔒 x402 Payment Protocol: HTTP 402 "Payment Required" standard implementation
- 🛠️ Corbits SDK: Professional payment infrastructure integration
- 🎭 Premium Content: Bad jokes delivered after successful payment
- 📱 Modern UI: Clean interface
- 🔄 Per-Call Billing: Each joke requires a separate payment (true micropayments)
- Frontend: Next.js 16, React 19, TypeScript, Tailwind CSS
- Blockchain: Solana (Devnet), USDC SPL Token
- Payments: Corbits SDK, x402 Protocol
- Wallet: Phantom integration via Solana Web3.js
- Deployment: Vercel
- Node.js 18+
- npm or yarn
- Phantom wallet browser extension
- Solana Devnet USDC (for testing)
-
Clone the repository
git clone https://github.com/lalitcap23/pay-per-api.git cd pay-per-api/ppa -
Install dependencies
npm install
-
Start development server
npm run dev
-
Open in browser
http://localhost:3000
npm run build
npm start- Connect Wallet: Click "Connect Solana Wallet" and approve Phantom connection
- Request Joke: Click "Request Premium Joke" button
- Receive 402: Server responds with payment details (0.0001 USDC required)
- Make Payment: Click "Pay with Wallet" and approve USDC transaction
- Get Content: Joke is delivered after payment verification
- Repeat: Each new joke requires a new payment (proper micropayments)
- Purpose: Fetch premium joke content
- Auth: Bearer token (obtained after payment)
- Response:
200with joke or402Payment Required
- Purpose: Verify Solana transaction and generate auth token
- Body:
{ signature, paymentId, network } - Response:
{ verified: boolean, token: string }
# Solana Configuration
SOLANA_NETWORK=devnet
USDC_MINT=4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU
RECIPIENT_WALLET=455q3UD1KkfMP7zWrd2XcYoZW8LaVoiU969cmusengZ9
# Payment Configuration
PAYMENT_AMOUNT=100 # 0.0001 USDC in base unitsuseWallet: Any solana wallet connection managementusePayment: Solana USDC payment processingcorbitsMiddleware: x402 protocol implementation- Payment verification: Blockchain transaction validation
The x402 HTTP Payment Protocol is implemented as follows:
- Client Request: Standard HTTP request to protected endpoint
- 402 Response: Server returns payment details:
{ "error": "Payment Required", "price": { "amount": 100, "token": "USDC" }, "paymentId": "joke_1699123456789", "network": "devnet", "paymentDetails": { "recipient": "455q3UD1KkfMP7zWrd2XcYoZW8LaVoiU969cmusengZ9", "splToken": "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU" } } - Payment Processing: Client sends USDC via Solana
- Verification: Server validates blockchain transaction
- Content Delivery: Premium content delivered after successful payment
- Token Expiry: Auth token cleared after content delivery (ensures per-call billing)
ppa/
├── app/
│ ├── api/
│ │ ├── jokes/route.ts # Main API endpoint
│ │ └── verify-payment/route.ts # Payment verification
│ ├── hook/
│ │ ├── pay.ts # Payment processing logic
│ │ └── useWallet.ts # Wallet connection hook
│ ├── page.tsx # Main UI component
│ └── layout.tsx # App layout
├── package.json
└── README.md
- Per-Call Billing: Auth token cleared after each successful content delivery
- TypeScript Safety: Proper typing for all payment interfaces
- Error Handling: Comprehensive error states and user feedback
- Responsive Design: Mobile-friendly SaaS-level interface
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This project demonstrates the future of API monetization through micropayments and the x402 HTTP Payment Protocol standard.