MicroAI Paygate demonstrates a decentralized payment layer for AI services. Instead of traditional subscriptions, it utilizes the HTTP 402 (Payment Required) status code to enforce per-request crypto micropayments. The system has been re-architected from a monolithic Node.js application into a distributed microservices stack to ensure maximum throughput, type safety, and cryptographic security.
- x402 Protocol Implementation: Native handling of the HTTP 402 status code to gate resources.
- Distributed Architecture: Decoupled services for routing (Go), verification (Rust), and presentation (Next.js).
- EIP-712 Typed Signatures: Industry-standard secure signing for payment authorization.
- Micropayments: Low-cost transactions (0.001 USDC) on the Base L2 network.
- High Concurrency: Go-based gateway for handling thousands of simultaneous connections.
- Memory Safety: Rust-based verification service for secure cryptographic operations.
Most AI monetization platforms rely on Web2 subscription models (Stripe, monthly fees) or centralized credit systems. These approaches introduce friction, require user registration, and create central points of failure.
MicroAI Paygate is designed to be frictionless and trustless:
- No Registration: Users connect a wallet and pay only for what they use.
- Stateless Verification: The verification logic is purely cryptographic and does not require database lookups for session management.
- Polyglot Performance: We use the right tool for the job—Go for I/O bound routing, Rust for CPU-bound cryptography, and TypeScript for UI.
- Standard Compliance: Fully compliant with EIP-712, ensuring users know exactly what they are signing.
The migration to a polyglot microservices architecture resulted in significant performance improvements across key metrics.
| Metric | Monolithic Stack (Node.js) | Microservices Stack (Go/Rust) | Improvement |
|---|---|---|---|
| Request Latency (P99) | 120ms | 15ms | 8x Faster |
| Verification Time | 45ms | 2ms | 22x Faster |
| Concurrent Connections | ~3,000 | ~50,000+ | 16x Scale |
| Memory Footprint | 150MB | 25MB (Combined) | 6x More Efficient |
| Cold Start | 1.5s | <100ms | Instant |
flowchart TB
subgraph Clients[Client Layer]
WEB[Web Frontend - Next.js]
AGENT[Agent Bot - TypeScript]
CLI[CLI / cURL]
end
subgraph Gateway[API Gateway Layer]
GW[Gateway - Go/Gin :3000]
end
subgraph Verification[Verification Layer]
VER[Verifier - Rust/Axum :3002]
end
subgraph External[External Services]
AI[OpenRouter API]
CHAIN[Base L2 - USDC]
end
WEB --> GW
AGENT --> GW
CLI --> GW
GW <--> VER
GW --> AI
WEB -.-> CHAIN
AGENT -.-> CHAIN
| Service | Technology | Port | Responsibility |
|---|---|---|---|
| Gateway | Go + Gin | 3000 |
Traffic routing, x402 enforcement, AI proxying |
| Verifier | Rust + Axum | 3002 |
EIP-712 signature recovery, ECDSA validation |
| Web | Next.js | 3001 |
React frontend with MetaMask integration |
The x402 protocol enables trustless, per-request payments using cryptographic signatures:
sequenceDiagram
autonumber
participant C as Client
participant G as Gateway
participant V as Verifier
participant AI as OpenRouter
Note over C,G: Phase 1 - Payment Challenge
C->>G: POST /api/ai/summarize
G-->>C: 402 Payment Required + paymentContext
Note over C: Phase 2 - User Signs Payment
C->>C: Sign EIP-712 TypedData with Wallet
Note over C,AI: Phase 3 - Verified Request
C->>G: POST with X-402-Signature + X-402-Nonce
G->>V: Verify signature
V->>V: Recover signer via ECDSA
V-->>G: is_valid + recovered_address
G->>AI: Forward to AI provider
AI-->>G: AI response
G-->>C: 200 OK + result
When a 402 Payment Required response is returned, it includes the payment context:
{
"error": "Payment Required",
"message": "Please sign the payment context",
"paymentContext": {
"recipient": "0x2cAF48b4BA1C58721a85dFADa5aC01C2DFa62219",
"token": "USDC",
"amount": "0.001",
"nonce": "9c311e31-eb30-420a-bced-c0d68bc89cea",
"chainId": 8453
}
}The client signs this data using EIP-712 and resends with headers:
X-402-Signature: The cryptographic signatureX-402-Nonce: The nonce from the payment context
The Gateway service utilizes Go's lightweight goroutines to handle high-throughput HTTP traffic. Unlike the Node.js event loop which can be blocked by CPU-intensive tasks, the Go scheduler efficiently distributes requests across available CPU cores.
- Framework: Gin (High-performance HTTP web framework)
- Concurrency Model: CSP (Communicating Sequential Processes)
- Proxy Logic: Uses
httputil.ReverseProxyfor zero-copy forwarding.
The Verifier is a specialized computation unit designed for one task: Elliptic Curve Digital Signature Algorithm (ECDSA) recovery.
- Safety: Rust's ownership model guarantees memory safety without a garbage collector.
- Cryptography: Uses
ethers-rsbindings tok256for hardware-accelerated math. - Isolation: Running as a separate binary ensures that cryptographic load never impacts the API gateway's latency.
Prerequisites
- Bun
- Go 1.24+
- Rust/Cargo (latest stable)
- Node.js 20+ (for Next.js 16.x tooling)
Clone & Install
git clone https://github.com/AnkanMisra/MicroAI-Paygate.git
cd MicroAI-Paygate
bun install
go mod tidy -C gateway
cargo build -q -C verifierConfigure Environment
Copy .env.example to .env and fill values (see next section).
Run the Stack
bun run stackRun Tests
- E2E:
bun run test:e2e - Gateway:
cd gateway && go test -v - Verifier:
cd verifier && cargo test
Create a .env (or use .env.example) with at least:
OPENROUTER_API_KEY— API key for OpenRouterOPENROUTER_MODEL— model name (default:z-ai/glm-4.5-air:free)SERVER_WALLET_PRIVATE_KEY— private key for the server wallet (recipient of payments)RECIPIENT_ADDRESS— wallet address for receiving paymentsCHAIN_ID— chain used in signatures (default:8453for Base)
Optional Configuration:
USDC_TOKEN_ADDRESS— USDC contract address (default: Base USDC)PAYMENT_AMOUNT— cost per request in USDC (default:0.001)VERIFIER_URL— URL of verifier service (default:http://127.0.0.1:3002)
Ensure ports 3000 (gateway), 3001 (web), and 3002 (verifier) are free.
For production environments, we provide a containerized setup using Docker Compose. This orchestrates all three services in an isolated network.
-
Configure Environment
cp .env.example .env # Edit .env with your API keys and wallet configuration -
Build and Run
docker-compose up --build -d
-
Verify Status
docker-compose ps
-
Logs
docker-compose logs -f
For rapid development, use the unified stack command which runs services on the host machine.
-
Install Prerequisites
- Bun, Go 1.24+, Rust/Cargo
-
Run Stack
bun run stack
We maintain a comprehensive test suite covering all layers of the stack, from unit tests for individual microservices to full end-to-end (E2E) integration tests.
The E2E tests simulate a real client interaction:
- Sending a request to the Gateway.
- Receiving a
402 Payment Requiredchallenge. - Signing the challenge with an Ethereum wallet.
- Resubmitting the request with the signature.
- Verifying the successful AI response.
Run E2E Tests:
bun run test:e2ePrerequisites: Bun, Go, and Rust toolchains installed. This command uses run_e2e.sh to build and start the Go Gateway and Rust Verifier before executing tests.
If OPENROUTER_API_KEY is missing, the signature path will pass but the final AI call may return 500 after verification.
Gateway (Go): Tests the HTTP handlers and routing logic.
cd gateway
go test -vVerifier (Rust): Tests the cryptographic verification logic and EIP-712 implementation.
cd verifier
cargo test- Port already in use: ensure 3000/3001/3002 are free or export alternative ports in env and update client config.
- Missing OpenRouter key: E2E tests may pass signature validation but fail on AI response with 500.
- Network errors inside Docker: use service names (
gateway:3000,verifier:3002) instead of localhost.
- HTTP 402 Payment Required (MDN): https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/402
- RFC 7231 Section 6.5.2 (Payment Required): https://www.rfc-editor.org/rfc/rfc7231#section-6.5.2
- EIP-712 Typed Structured Data: https://eips.ethereum.org/EIPS/eip-712
We welcome contributions! Please read CONTRIBUTING.md for guidelines and check the GitHub Issues for open tasks.
This project is licensed under the MIT License.
Description Proxies a text summarization request to the AI provider, enforcing payment via the x402 protocol.
Request Headers
| Header | Type | Required | Description |
|---|---|---|---|
Content-Type |
string | Yes | Must be application/json |
X-402-Signature |
hex string | Yes | The EIP-712 signature signed by the user's wallet. |
X-402-Nonce |
uuid | Yes | The nonce received from the initial 402 response. |
Request Body
{
"text": "The content to be summarized..."
}Response Codes
| Status Code | Meaning | Payload Structure |
|---|---|---|
200 OK |
Success | { "result": "Summary text..." } |
402 Payment Required |
Payment Needed | { "paymentContext": { "nonce": "...", "amount": "0.001", ... } } |
403 Forbidden |
Invalid Signature | { "error": "Invalid Signature", "details": "..." } |
500 Internal Error |
Server Failure | { "error": "Service unavailable" } |
Description Internal endpoint used by the Gateway to verify signatures with the Rust service. Not exposed publicly.
Body
{
"context": { ... },
"signature": "0x..."
}