A Solana-powered multi-LLM gateway with pay-per-request micropayments using the x402 protocol. Allows any app to access GPT, Claude and other LLM models through a trustless 402 Payment Required flow with on-chain verification.
x402-ai is an early-stage TypeScript project in the AI payments / x402 ecosystem, focused on ai, micropayments, solana, x402. It currently has 5 GitHub stars and 0 forks, and sits alongside related tools like dna-x402, vibecheck-x402, x402-sdk.
A production-ready micropayment gateway that implements HTTP 402 (Payment Required) to monetize access to AI inference services. Users pay micro-amounts in SOL to query multiple LLM providers (OpenAI, Google, Anthropic, Perplexity) with automatic on-chain receipt recording.
https://github.com/user-attachments/assets/c12506dc-06a6-43ca-8e8d-faa9914017ed
x402 AI Gateway enables trustless, pay-per-request AI inference with secure Solana micropayments. Each AI request requires a small SOL payment verified on-chain before processing. Payment receipts are automatically recorded on Solana using an Anchor program for full auditability and transparency.
User Request → HTTP 402 Payment Required → User Pays SOL → Transaction Verified → AI Response
For installation, setup, and development guidelines, see CONTRIBUTING.md.
POST /api/ai
Content-Type: application/json
{
"prompt": "Explain quantum computing",
"modelId": "gpt-4o"
}
Response (402 Payment Required):
{
"paymentRequirements": {
"recipient": "MERCHANT_WALLET_ADDRESS",
"amountLamports": "50000000",
"price": {
"tokenSymbol": "SOL",
"tokenDecimals": 9,
"amountTokens": "0.050000000",
"amountUsd": 0.05
},
"reference": "1731523200000-abc123"
}
}
POST /api/ai
Content-Type: application/json
X-PAYMENT: {"txSignature":"5xK3...","reference":"1731523200000-abc123"}
{
"prompt": "Explain quantum computing",
"modelId": "gpt-4o"
}
Response (200 Success):
{
"output": "Quantum computing uses quantum mechanical phenomena...",
"modelId": "gpt-4o",
"usage": {
"inputTokens": 10,
"outputTokens": 150
}
}
The x402 AI Gateway follows a three-tier architecture: Frontend (Client), Backend (API Gateway), and Blockchain (Solana). The system implements the HTTP 402 Payment Required protocol to enforce payment before AI inference.
┌─────────────────┐
│ Frontend │
│ (Next.js App) │
│ │
│ - Chat UI │
│ - Wallet Conn │
│ - Tx Builder │
└────────┬────────┘
│
│ HTTP 402 Protocol
│
┌────────▼─────────────────────────┐
│ Backend API Gateway │
│ (/api/ai) │
│ │
│ - Payment Verification │
│ - Price Oracle │
│ - LLM Router │
│ - Refund Handler │
└────────┬─────────────────────────┘
│
┌────┴────┐
│ │
┌───▼───┐ ┌──▼──────────┐
│Solana │ │ LLM APIs │
│Block- │ │ │
│chain │ │ - OpenAI │
│ │ │ - Google │
│ │ │ - Anthropic │
│ │ │ - Perplexity│
└───────┘ └─────────────┘
Location: src/features/dashboard/ and src/components/
Chat Interface (chat-interface.tsx): Main user interface for AI interactions
Wallet Integration (solana/): Universal wallet support
UI Components (components/ui/): Shadcn-based design system
Location: src/app/api/ai/route.ts
API Gateway (/api/ai): Main entry point for AI requests
Payment Verification (verify.ts logic):
Price Oracle:
LLM Router:
modelIdRefund System:
Location: x402_receipts/programs/x402_receipts/
x402_receipts):12wpFdqZm2bwCUNSiqB8UJTwRJFkevU5vUuE8XxhpHE1record_receipt1. User Request
│
├─► Frontend sends POST /api/ai
│
2. Payment Required (402)
│
├─► Backend calculates SOL amount from USD price
├─► Returns payment requirements with reference
│
3. Transaction Building
│
├─► Frontend builds Solana transaction:
│ ├─► SystemProgram.transfer (payment)
│ └─► record_receipt instruction (on-chain receipt)
│
4. Transaction Signing
│
├─► User approves in wallet
├─► Transaction signed and broadcast
│
5. Payment Verification
│
├─► Backend verifies transaction on-chain
├─► Checks reference for replay protection
├─► Validates payment amount
│
6. AI Processing
│
├─► Backend routes to LLM provider
├─► Returns AI response
│
7. Error Handling (if AI fails)
│
├─► Backend detects failure
├─► Builds refund transaction
├─► Processes automatic refund
└─► Returns error with refund status
/v1/chat/completions endpointRequest Flow:
Payment Flow:
Receipt Flow:
Refund Flow (on failure):
Security is a top priority. Please review our Security Policy before reporting vulnerabilities.
Important Security Notes:
MERCHANT_PRIVATE_KEY secure and never expose to client-side codeThis project is licensed under the MIT License - see the LICENSE file for details.
12wpFdqZm2bwCUNSiqB8UJTwRJFkevU5vUuE8XxhpHE1Built with: Next.js, React, Solana, Anchor, TypeScript, Tailwind CSS
DNA — Payment rails for AI agents. x402 micropayment protocol on Solana. Netting, transfer, stream settlement. Receipt anchoring on-chain.
AI-powered vibe check for online dating profiles. Pay once, know who’s real. Built with Solana x402 for safer, ad-free digital trust.
x402 SDK for AI agent payments - Base & Solana USDC micropayments