The First x402 Implementation on Polygon — Enabling AI agents to autonomously discover and pay for APIs using Coinbase's x402 protocol on Polygon's payment-optimized infrastructure.
"Stripe for AI Agents on Polygon — where autonomous agents pay per API call using x402 micropayments, settled in USDC on the world's leading payments blockchain."
- Executive Summary
- Why Polygon + x402?
- User Personas
- Core Features
- Technical Architecture
- System Flow
- Quick Start
- Project Structure
- Judging Criteria Alignment
- Demo
- Contributing
AI agents are the future of software, but they're stuck in Web2 payment rails:
- 🚫 Can't manage API keys or credit cards
- 💸 Fixed subscriptions waste money on variable usage
- 🏢 Centralized platforms take 30% cuts
- 🔍 No way to autonomously discover services
Market Gap: The AI agent economy needs decentralized, usage-based payment infrastructure.
StreamFlow implements Coinbase's x402 payment protocol on Polygon, enabling:
✅ Agent-to-Agent Commerce: Zero human intervention
✅ Pay-Per-Token Pricing: As low as $0.00001/token
✅ Decentralized Verification: Chainlink CRE prevents fraud
✅ Open Marketplace: Any API provider can monetize
✅ Polygon Settlement: Leverage $3.6B stablecoin liquidity
"StreamFlow proves x402 is a truly open protocol by implementing it on Polygon, demonstrating how Coinbase's payment standard can enable the AI agent economy across any EVM chain while leveraging Polygon's payment-optimized infrastructure."
Technical Achievement:
- First x402 marketplace implementation
- First x402 deployment on Polygon
- First AI agent payment system with CRE verification
| Requirement | Polygon Solution | x402 Solution |
|---|---|---|
| High Transaction Volume | 5,000 TPS (Rio upgrade) | HTTP-native (no blockchain bloat) |
| Low Costs | ~$0.005/tx | Micropayments viable |
| Stablecoin Liquidity | $3.6B USDC/USDT | Native USDC support |
| Agent-Friendly | Fast finality (5 sec) | No API keys required |
| Payment Leader | Stripe, Mastercard partners | Open payment standard |
Result: AI agents can make thousands of micro-payments daily at costs lower than traditional payment processors.
Profile: Founder building autonomous customer service agents
Current Pain:
- Agents need to consume multiple LLM APIs
- OpenAI charges $20/month even if agents use $2
- Can't switch providers without rewriting code
StreamFlow Solution:
# Agent automatically selects cheapest provider
cheapest_api = await marketplace.query(model_type="general")
response = await agent.call_api(cheapest_api, prompt="...")
# Pays $0.0045 (150 tokens × $0.00003) instead of $20/monthImpact: 98% cost reduction through usage-based pricing
Profile: ML engineer with fine-tuned legal analysis model
Current Pain:
- Can't compete with OpenAI's distribution
- Stripe takes 2.9% + $0.30 per transaction
- Model sits idle 80% of the time
StreamFlow Solution:
// Register API in marketplace
marketplace.registerService(
endpoint="https://legal-ai.com/api",
pricePerToken=0.00005 * 1e6 // $0.00005 USDC
);
// Earn $0.005 per request, settled instantly on PolygonImpact: Earn from 1,000 micro-transactions vs 5 enterprise customers
What: HTTP-native payment protocol adapted for Polygon's ecosystem
How It Works:
POST /api/v1/generate HTTP/1.1
Host: api.streamflow.xyz
Content-Type: application/json
X-Payment-Auth: polygon-0x742d35Cc...
HTTP/1.1 402 Payment Required
X-Payment-Required: 0.003 USDC
X-Payment-Address: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e
[Agent pays via CDP wallet]
HTTP/1.1 200 OK
Content-Type: application/json
X-Tokens-Used: 150
X-Cost-USDC: 0.0045
{"text": "Quantum computing uses..."}Innovation: First x402 implementation showing protocol extensibility beyond Base
What: Production-grade wallets with <200ms signing latency
Agent Onboarding:
import { Coinbase } from "@coinbase/coinbase-sdk";
// Create wallet on Polygon (zero human steps)
const wallet = await Coinbase.createWallet({
networkId: "polygon-amoy"
});
// Set spending policy
await wallet.setPolicy({
maxAmountPerTx: "10_000000", // 10 USDC
allowedRecipients: [providerAddress]
});Why Not Regular Wallets?
- No seed phrases to manage
- Sub-200ms transaction signing
- Built-in policy controls
- 99.9% uptime SLA
What: Off-chain computation that prevents usage fraud
The Problem: How do you trust API providers report accurate usage?
The Solution:
1. API logs usage: "Agent_123 used 150 tokens"
2. CRE polls logs from multiple nodes (decentralized)
3. CRE calculates: 150 × $0.00003 = $0.0045 USDC
4. CRE triggers Polygon settlement with proof
5. Only verified payments settle on-chain
Gas Savings: $0.001 (CRE) vs $0.05+ (on-chain verification)
What: Real-time dashboards querying Polygon events
Provider Dashboard:
query ProviderRevenue {
providerStats(id: "0x742d35Cc...") {
totalRevenue
totalTokensServed
uniqueAgents
revenueByDay {
date
amount
}
}
}Agent Dashboard:
- Spending by provider
- Cost per request trends
- Savings vs subscriptions
What: Permissionless registry on Polygon
Any Provider Can:
- Deploy their API endpoint
- Register in smart contract ($0.005 gas)
- Start earning USDC in minutes
Any Agent Can:
- Query The Graph for services
- Authorize spending once
- Pay per actual usage
Network Effect: More providers → Better prices → More agents → More providers
| Layer | Technology | Purpose |
|---|---|---|
| Settlement | Polygon PoS (Amoy) | All payments settle here |
| Payment Protocol | x402 (Custom) | HTTP-native authorization |
| Agent Wallets | CDP Server Wallets | Sub-200ms signing |
| Off-chain Compute | Chainlink CRE | Usage verification |
| Backend | Python FastAPI | Marketplace + routing |
| Mock Services | Python | 2 LLM endpoints (demo) |
| Indexing | The Graph | Real-time queries |
| Frontend | React + ethers.js | Provider/agent dashboards |
| Agent Framework | AgentKit (Python) | Autonomous logic |
┌─────────────────────────────────────────────────────────────┐
│ STREAMFLOW ON POLYGON │
└─────────────────────────────────────────────────────────────┘
1. AGENT CREATION (CDP Server Wallets)
↓
Wallet created on Polygon in <3 seconds
Pre-funded with USDC from faucet
2. SERVICE DISCOVERY (The Graph)
↓
Agent queries: "What LLMs are available?"
Graph returns: GeneralGPT ($0.00003/token), LegalAI ($0.00005/token)
3. AUTHORIZATION (x402 on Polygon)
↓
Agent approves $10 USDC spending to GeneralGPT provider
Transaction settles on Polygon (~5 seconds)
4. API CALL (HTTP + x402 Headers)
↓
POST /api/v1/generate
Headers: X-Payment-Auth, X-Agent-Address
Body: {"prompt": "Explain quantum computing"}
5. USAGE LOGGING (FastAPI Middleware)
↓
API processes request → logs 150 tokens used
Returns response + cost breakdown
6. VERIFICATION (Chainlink CRE)
↓
CRE polls usage logs (every 30s)
Multi-node consensus: "150 tokens confirmed"
Calculates: 150 × $0.00003 = $0.0045 USDC
7. SETTLEMENT (Polygon Smart Contract)
↓
CRE triggers: x402Contract.processPayment(agent, provider, 0.0045)
USDC transfers on Polygon
Event emitted: PaymentProcessed
8. ANALYTICS (The Graph)
↓
Subgraph indexes PaymentProcessed event
Dashboard updates in real-time
Provider sees: +$0.0045 revenue
Agent sees: -$0.0045 spent
9. WITHDRAWAL (Provider Action)
↓
Provider calls: x402Contract.withdraw()
USDC sent to provider's CDP wallet on Polygon
Every step happens on Polygon ✅
node --version # v18+
python --version # 3.10+
forge --version # Foundry for Solidity
gh --version # GitHub CLI# Clone repository
git clone https://github.com/yourusername/streamflow.git
cd streamflow
# Install dependencies
npm install
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Add your API keys:
# - CDP_API_KEY_NAME
# - CDP_API_KEY_SECRET
# - POLYGON_RPC_URL (https://rpc-amoy.polygon.technology)
# - GRAPH_API_KEYcd contracts
forge build
forge script script/Deploy.s.sol:DeployScript \
--rpc-url $POLYGON_RPC_URL \
--broadcast \
--verifycd backend
uvicorn main:app --reload --port 8000cd frontend
npm start# Terminal 1: Backend running
# Terminal 2: Run agent test
cd tests
python test_agent_flow.pystreamflow/
├── contracts/ # Solidity smart contracts
│ ├── src/
│ │ ├── X402Payment.sol # Main payment contract
│ │ ├── ServiceRegistry.sol # API marketplace
│ │ └── interfaces/
│ │ └── IX402.sol # x402 interface
│ ├── test/ # Foundry tests
│ └── script/ # Deployment scripts
│
├── cre-workflows/ # Chainlink CRE (TypeScript)
│ ├── src/
│ │ ├── usage-monitor.ts # Polls API usage
│ │ ├── billing-calculator.ts # Computes costs
│ │ └── settlement-trigger.ts # Triggers Polygon tx
│ └── test/
│
├── backend/ # Python FastAPI
│ ├── main.py # Marketplace API
│ ├── services/
│ │ ├── general_gpt.py # Mock LLM 1
│ │ └── legal_ai.py # Mock LLM 2
│ ├── middleware/
│ │ └── x402_auth.py # Payment validation
│ └── tests/
│
├── subgraph/ # The Graph indexing
│ ├── schema.graphql # Entity definitions
│ ├── subgraph.yaml # Configuration
│ └── src/
│ └── mapping.ts # Event handlers
│
├── frontend/ # React dashboard
│ ├── src/
│ │ ├── pages/
│ │ │ ├── ProviderDashboard.tsx
│ │ │ └── AgentDashboard.tsx
│ │ └── components/
│ │ ├── WalletConnect.tsx
│ │ └── PaymentHistory.tsx
│ └── public/
│
├── scripts/ # Utility scripts
│ ├── fund-wallets.ts # USDC faucet helper
│ └── deploy-all.sh # One-command deployment
│
└── docs/ # Documentation
├── ARCHITECTURE.md
├── API.md
└── DEPLOYMENT.md
Category 1: x402 + AI/Agentic Application
- ✅ x402 protocol deployed on Polygon
- ✅ AI agents autonomously consume APIs
- ✅ All settlements on Polygon network
- ✅ High transaction volume potential
Category 2: x402 Protocol Expansion
- ✅ Adapts x402 for Polygon ecosystem
- ✅ Shows protocol extensibility beyond Base
- ✅ Custom facilitator logic for Polygon's stablecoin infrastructure
Category 3: Payment Flows
- ✅ Merchant application (API providers)
- ✅ Payment processing middleware
- ✅ Brings transaction count up (agent-driven volume)
Why We Win: First x402 implementation on Polygon proving the protocol works across any EVM chain
Core Requirements:
- ✅ CDP Server Wallets (agents + providers)
- ✅ x402 protocol implementation
- ✅ AgentKit integration (Python SDK)
Bonus Points:
- ✅ Using 3+ CDP products
- ✅ Novel x402 use case (marketplace)
- ✅ High-quality implementation
- ✅ Demonstrates protocol extensibility
Why We Win: Proves x402 is a truly open standard by implementing on non-Base chain
Core Functionality:
- ✅ CRE workflows for off-chain computation
- ✅ Decentralized usage verification
- ✅ Novel integration (CRE + x402)
Why We Win: First project combining CRE with x402 payments
Requirements:
- ✅ Real-time analytics dashboards
- ✅ Complex queries (revenue aggregation)
- ✅ Essential to UX (service discovery)
Why We Win: Subgraph enables the entire marketplace discovery mechanism
1. Provider Registration (30s)
# Connect CDP wallet
# Register "LegalAI" at $0.00005/token
# Show Polygon transaction: polygonscan.com/tx/0x...2. Agent Discovery (30s)
# Query The Graph
{ apiServices { endpoint, pricePerToken } }
# Agent selects cheapest option3. Automated Payment (1 min)
# Agent authorizes $10 USDC
# Makes API call → 402 response → pays → 200 response
# CRE settles on Polygon
# Show transaction on PolygonScan4. Analytics Dashboard (1 min)
Provider view: +$0.0045 revenue (real-time)
Agent view: Cost comparison (saved 40%)
5. Impact Slide (30s)
- First x402 on Polygon
- 98% cheaper than Stripe
- Unlocks $X trillion agent economy
We welcome contributions! See CONTRIBUTING.md
git checkout -b feature/amazing-feature
git commit -m 'Add amazing feature'
git push origin feature/amazing-featureMIT License - see LICENSE
- Demo: streamflow-demo.vercel.app (coming soon)
- Contracts: Polygon Amoy Explorer
- Subgraph: The Graph Studio
- Twitter: @StreamFlowAI
Built with:
- Polygon - Payment-optimized blockchain
- Coinbase CDP - Server Wallets & x402 protocol
- Chainlink - CRE off-chain compute
- The Graph - Decentralized indexing
Special thanks to ETH Global and all DevRel teams!
The first x402 implementation on Polygon
Empowering the autonomous agent economy