Complete x402 (Solana) payment protocol implementation for API monetization
Built for the Solana X402 Hackathon π
Features β’ Architecture β’ Quick Start β’ Documentation β’ Contributing
- Overview
- Features
- Tech Stack
- Architecture
- Quick Start
- Services
- Documentation
- Development
- Hackathon Submission
- Contributing
- License
MicroAPI Hub is a complete end-to-end implementation of the x402 payment protocol for Solana, enabling API providers to monetize their endpoints with blockchain payments. This project demonstrates how to build a production-ready payment system that integrates seamlessly with Solana's blockchain.
x402 is an open payment standard that enables clients to pay for external resources (APIs, content, data) using blockchain payments. It follows HTTP status code 402 (Payment Required) semantics and provides a standardized way to implement pay-per-use APIs with on-chain settlement and verifiable receipts.
- β Full Protocol Compliance: Complete x402 specification implementation
- β Solana Integration: Native SOL and SPL token support
- β Production-Ready: Error handling, validation, logging, testing
- β Developer-Friendly: Comprehensive docs, code examples, easy integration
- β On-Chain Registry: Decentralized API discovery
- β Multi-Language Support: TypeScript, Python, Go examples
- Payment Verification: Cryptographically secure signature verification
- On-Chain Settlement: Automatic blockchain transaction processing
- Multi-Token Support: Native SOL and SPL tokens (USDC, etc.)
- Nonce Replay Protection: Prevents double-spending attacks
- Time Window Validation: Ensures payment freshness
- Wallet Integration: Connect with Phantom, Solflare, and other Solana wallets
- Payment Modal: User-friendly payment flow with real-time status
- Transaction History: View and verify all payment receipts
- Resource Discovery: Browse available paid APIs
- Receipt Viewer: Verify transactions on Solscan
- RESTful API: Standard HTTP endpoints
- Discovery Endpoint:
.well-known/x402protocol compliance - Code Examples: TypeScript, Python, Go implementations
- Comprehensive Docs: API reference, developer guide, tutorials
- E2E Testing: Complete test suite
- Decentralized Discovery: Query APIs from Solana blockchain
- Provider Registration: Register your API on-chain
- Category Organization: Organize APIs by type
- Active/Inactive Management: Control API availability
Node.js 18+ - Runtime environment
TypeScript 5.6 - Type-safe development
Express.js - Web framework
Zod - Schema validation
Solana - Blockchain platform
Anchor Framework - Solana program framework
@solana/web3.js - Solana JavaScript SDK
@solana/spl-token - Token operations
Next.js 14 - React framework
React 18 - UI library
Tailwind CSS - Styling
@solana/wallet-adapter - Wallet integration
The following Mermaid diagram shows the end-to-end x402 payment flow across swimlanes for Client, Provider, Facilitator, and Solana:
sequenceDiagram
autonumber
box Client
participant C as Client (Web UI / Agent)
end
box Provider API
participant P as Provider API (x402 Guard)
end
box Facilitator
participant F as Facilitator (Verify & Settle)
end
box Solana
participant S as Solana Network
end
C->>P: 1) GET /api/data (no X-PAYMENT)
P-->>C: 2) 402 Payment Required + PaymentRequirements
note over C: Create x402 Payment Header<br/>- Secure nonce<br/>- Time window<br/>- Authorization payload
C->>P: 3) GET /api/data with X-PAYMENT
P->>F: 4) POST /verify { paymentHeader, requirements }
F-->>P: 5) { isValid: true }
P->>F: 6) POST /settle { paymentHeader, requirements }
F->>S: 7) Submit transaction (native/SPL)
S-->>F: 8) Confirmed (tx hash)
F-->>P: 9) { success: true, txHash }
P-->>C: 10) 200 OK + X-PAYMENT-RESPONSE (txHash)
Alternatively, here is a swimlane-style activity view using Mermaid flowchart subgraphs (lanes):
flowchart LR
subgraph L1[Client]
A[Request /api/data] --> B[Receive 402 + Requirements]
B --> C[Create X-PAYMENT header]
C --> D[Call /api/data with header]
end
subgraph L2[Provider]
D --> E[Verify with Facilitator]
E --> G[Settle with Facilitator]
G --> H[Return 200 + X-PAYMENT-RESPONSE]
end
subgraph L3[Facilitator]
E --> F[Verify Authorization]
G --> I[Build & Submit Transaction]
end
subgraph L4[Solana]
I --> J[Confirm Transaction]
end
J --> H
βββββββββββ ββββββββββββ βββββββββββββββ ββββββββββββ
β Client β β Provider β β Facilitator β β Solana β
ββββββ¬βββββ ββββββ¬ββββββ ββββββββ¬βββββββ ββββββ¬ββββββ
β β β β
β 1. GET /api/data β β β
ββββββββββββββββββββΊβ β β
β β β β
β 2. 402 Payment β β β
β Required β β β
β + Requirements β β β
βββββββββββββββββββββ€ β β
β β β β
β 3. Create Payment β β β
β Authorization β β β
β (with nonce) β β β
β β β β
β 4. POST /api/data β β β
β + X-PAYMENT β β β
β header β β β
ββββββββββββββββββββΊβ β β
β β β β
β β 5. POST /verify β β
β ββββββββββββββββββββββββΊβ β
β β β β
β β 6. Verify Response β β
β β {isValid: true} β β
β βββββββββββββββββββββββββ€ β
β β β β
β β 7. POST /settle β β
β ββββββββββββββββββββββββΊβ β
β β β β
β β β 8. Create Transactionβ
β β βββββββββββββββββββββββΊβ
β β β β
β β β 9. Transaction Hash β
β β ββββββββββββββββββββββββ€
β β β β
β β 10. Settlement Resp β β
β β {success: true, β β
β β txHash: "..."} β β
β βββββββββββββββββββββββββ€ β
β β β β
β 11. 200 OK β β β
β + X-PAYMENT- β β β
β RESPONSE β β β
βββββββββββββββββββββ€ β β
β β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Web UI (Next.js) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Wallet β β Payment β β Resource β β
β β Provider β β Modal β β Card β β
β β β β β β β β
β β β’ Connect β β β’ Show β β β’ Display β β
β β β’ Sign β β Amount β β Info β β
β β β’ State β β β’ Execute β β β’ Trigger β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β HTTP
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Provider API Service β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β x402 Guard Middleware β β
β β β β
β β 1. Check X-PAYMENT header β β
β β 2. If missing β Return 402 β β
β β 3. If present β Verify with Facilitator β β
β β 4. If valid β Settle with Facilitator β β
β β 5. If settled β Allow request to proceed β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β β POST /verify, /settle β
β βΌ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Facilitator Service β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Verification Engine β β
β β β β
β β β’ Parse payment header β β
β β β’ Validate signature β β
β β β’ Check nonce (replay protection) β β
β β β’ Validate time window β β
β β β’ Verify amount & recipient β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Settlement Engine β β
β β β β
β β β’ Create Solana transaction β β
β β β’ Sign with fee payer β β
β β β’ Submit to blockchain β β
β β β’ Wait for confirmation β β
β β β’ Return transaction hash β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β β RPC β
β βΌ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β
βΌ
ββββββββββββββββ
β Solana β
β Blockchain β
ββββββββββββββββ
Node.js 18+ and npm/pnpm
Solana CLI (optional, for contract deployment)
Docker (optional, for containerized deployment)
# Clone the repository
git clone https://github.com/yourusername/microapi-hub.git
cd microapi-hub
# Install dependencies
npm install --workspacescd services/facilitator
# Create .env file
cat > .env << EOF
PORT=8787
NETWORK=devnet
RPC_URL=https://api.devnet.solana.com
FEE_PAYER_SECRET= # Optional: base58-encoded keypair
SETTLEMENT_MODE=native
DEMO_MODE=true
EOF
# Start facilitator
npm run devcd services/provider-api
# Create .env file
cat > .env << EOF
PORT=8080
PAY_TO_PUBKEY=YOUR_SOLANA_PUBKEY
USDC_MINT=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
FACILITATOR_URL=http://localhost:8787
EOF
# Start provider API
npm run devcd clients/web
# Create .env.local file
cat > .env.local << EOF
NEXT_PUBLIC_PROVIDER_DISCOVERY_URL=http://localhost:8080/.well-known/x402
EOF
# Start web UI
npm run dev- π Web UI: http://localhost:3000
- π Provider API: http://localhost:8080
- βοΈ Facilitator: http://localhost:8787
Payment verification and on-chain settlement service.
Endpoints:
POST /verify- Verify payment authorizationPOST /settle- Settle payment on-chainGET /health- Health checkGET /supported- Supported payment schemes
Key Features:
- β Signature verification
- β Nonce replay protection
- β Time window validation
- β Native SOL and SPL token settlement
- β Auto-funding on devnet
- β Redis/file-based storage
API server with x402 payment protection.
Endpoints:
GET /api/data- Protected endpoint (example)GET /.well-known/x402- Discovery endpointGET /health- Health check
Key Features:
- β 402 Payment Required responses
- β Payment verification integration
- β Automatic settlement
- β Discovery endpoint
- β Resource protection middleware
Next.js application with wallet integration.
Features:
- β Wallet connection (Phantom, Solflare)
- β Payment modal with real-time status
- β Resource discovery
- β Transaction history
- β Receipt viewer
- β Code examples
- β Comprehensive documentation
- π API Documentation - Complete API reference with examples
- π¨βπ» Developer Guide - Integration guide and best practices
- π Hackathon Submission - Submission details and demo script
- π Examples: http://localhost:3000/examples - Code samples in multiple languages
- β FAQ: http://localhost:3000/faq - Frequently asked questions
- π API Docs: http://localhost:3000/api - Interactive API documentation
microapi-hub/
βββ services/
β βββ facilitator/ # Payment verification & settlement
β β βββ src/
β β β βββ index.ts # Main facilitator service
β β β βββ config.ts # Configuration management
β β β βββ errors.ts # Error handling
β β β βββ registry.ts # Registry client
β β βββ package.json
β βββ provider-api/ # Protected API server
β βββ src/
β β βββ index.ts # Main provider service
β β βββ config.ts # Configuration
β βββ package.json
βββ clients/
β βββ web/ # Next.js web UI
β β βββ app/ # Next.js app router
β β βββ components/ # React components
β β βββ lib/ # Utilities
β β βββ package.json
β βββ agent-demo/ # Example client
βββ contracts/
β βββ registry/ # Solana Anchor program
β βββ programs/
β βββ registry/
β βββ src/
β βββ lib.rs # Registry contract
βββ shared/
β βββ types/ # Shared TypeScript types
β βββ x402.ts # x402 protocol types
β βββ errors.ts # Error types
β βββ registry.ts # Registry types
βββ docs/ # Documentation
β βββ API.md
β βββ DEVELOPER.md
βββ infra/ # Infrastructure
β βββ docker-compose.yml
βββ x402/ # x402 protocol library
# Run all tests
npm test
# Run E2E tests
npm run e2e# Build all services
npm run build# Start all services with Docker Compose
cd infra
docker-compose upβ
x402 Protocol Integration: Full specification implementation
β
Solana Integration: Deployed to devnet, mainnet-ready
β
Open Source: All code on GitHub
β
Demo Video: 3-minute demonstration
β
Documentation: Comprehensive setup and usage guides
See HACKATHON.md for demo video script and submission details.
- Complete Stack: End-to-end implementation from facilitator to UI
- Production-Ready: Error handling, validation, logging, testing
- Developer-Friendly: Comprehensive docs, code examples, easy integration
- On-Chain Registry: Decentralized API discovery
- Multi-Language: Support for TypeScript, Python, Go
- Wallet Integration: Seamless payment UX with wallet adapters
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Follow TypeScript best practices
- Write tests for new features
- Update documentation
- Follow the existing code style
- Ensure all tests pass
This project is open source. See LICENSE file for details.
- Coinbase for the x402 protocol specification
- Solana Foundation for the hackathon
- All open-source contributors
- π Documentation: See
/docsdirectory - π» Examples: http://localhost:3000/examples
- β FAQ: http://localhost:3000/faq
- π GitHub Issues: [Repository URL]/issues
- Mainnet deployment
- Multi-chain support (EVM)
- Advanced payment schemes
- Analytics dashboard
- Webhook notifications
- Client SDK package
- Rate limiting per client
- Usage analytics
Built with β€οΈ for the Solana X402 Hackathon