Monetize Your API with Stablecoin Micropayments
Earn USDC from every API call using x402, session keys, and spending policies. AI agents pay per request automatically. No subscriptions, no billing dashboards, no invoices.
Why Stablecoin Micropayments?
Traditional API monetization (API keys, subscriptions, Stripe billing) adds friction and overhead. With x402 + Chipi:
- - No signups — clients pay with USDC per request
- - No billing infrastructure — the HTTP protocol handles payment inline
- - No invoices — every payment settles on StarkNet in seconds
- - AI-native — agents pay autonomously via session keys
- - Gasless — Chipi sponsors gas, you receive pure USDC
Revenue math: At $0.003 per AI inference call, 10,000 calls/day = $30/day = $900/month from a single endpoint.
How It Works
Client (AI Agent) Your API Server Chipi Paymaster
| | |
| GET /api/inference | |
|---------------------------------->| |
| 402 + PAYMENT-REQUIRED header | |
|<----------------------------------| |
| | |
| [session key signs USDC payment] | |
| | |
| GET /api/inference + X-PAYMENT | |
|---------------------------------->| |
| | verify + settle (gasless) |
| |------------------------------->|
| | { txHash } |
| |<-------------------------------|
| 200 + inference result | |
|<----------------------------------| |The client never needs to think about payments. The session key signs automatically. Spending policies cap the total spend.
Server Setup (TypeScript)
Add the x402 middleware to any Express or Next.js API route:
typescript// Express
import express from "express";
import { x402Middleware } from "@chipi-stack/backend";
const app = express();
app.use("/api/inference", x402Middleware({
amount: "0.003", // USDC per request
recipient: process.env.MERCHANT_WALLET!, // your StarkNet wallet
facilitatorUrl: "https://x402.chipipay.com",
network: "starknet-mainnet",
asset: "USDC",
}));
app.post("/api/inference", async (req, res) => {
// This only runs after payment is verified
const result = await runInference(req.body.prompt);
res.json(result);
});typescript// Next.js API Route
import { x402Middleware } from "@chipi-stack/backend";
const paywall = x402Middleware({
amount: "0.003",
recipient: process.env.MERCHANT_WALLET!,
facilitatorUrl: "https://x402.chipipay.com",
network: "starknet-mainnet",
asset: "USDC",
});
export async function POST(req: Request) {
// Check payment
const paymentResult = await paywall(req);
if (paymentResult.status === 402) return paymentResult;
// Payment verified — serve the request
const result = await runInference(await req.json());
return Response.json(result);
}Server Setup (Python / FastAPI)
pythonfrom fastapi import FastAPI, Depends
from chipi_sdk import x402_middleware
app = FastAPI()
# Protect endpoint with x402
paywall = x402_middleware(
amount="0.003",
recipient="0xYOUR_WALLET",
facilitator_url="https://x402.chipipay.com",
network="starknet-mainnet",
asset="USDC",
)
@app.post("/api/inference", dependencies=[Depends(paywall)])
async def inference(prompt: str):
# Only runs after payment is verified
result = await run_inference(prompt)
return {"result": result}Client: AI Agent with Budget Control
The AI agent uses a session key to pay automatically, with spending policies to cap the budget:
typescriptimport { ChipiServerSDK } from "@chipi-stack/backend";
const sdk = new ChipiServerSDK({
apiPublicKey: process.env.CHIPI_PUBLIC_KEY!,
apiSecretKey: process.env.CHIPI_SECRET_KEY!,
});
const USDC = "0x033068f6539f8e6e6b131e6b2b814e6c34a5224bc66947c47dab9dfee93b35fb";
// 1. Create session key for the agent
const session = sdk.sessions.createSessionKey({
encryptKey: process.env.AGENT_ENCRYPT_KEY!,
durationSeconds: 86400, // 24 hours
});
// 2. Register session on-chain
await sdk.sessions.addSessionKeyToContract({
encryptKey: process.env.AGENT_ENCRYPT_KEY!,
wallet: agentWallet,
sessionConfig: {
sessionPublicKey: session.publicKey,
validUntil: session.validUntil,
maxCalls: 10000,
allowedEntrypoints: ["transfer"],
},
}, bearerToken);
// 3. Set spending cap: max $0.01 per call, $50 per day
await sdk.sessions.setSpendingPolicy({
encryptKey: process.env.AGENT_ENCRYPT_KEY!,
wallet: agentWallet,
spendingPolicyConfig: {
sessionPublicKey: session.publicKey,
token: USDC,
maxPerCall: 10_000n, // $0.01 (6 decimals)
maxPerWindow: 50_000_000n, // $50
windowSeconds: 86400, // 24h rolling window
},
}, bearerToken);
// 4. Agent makes API calls — payments are automatic
const response = await fetch("https://your-api.com/inference", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-PAYMENT": signedPayment, // session key signs this
},
body: JSON.stringify({ prompt: "analyze this..." }),
});Three layers of protection:
- - Session key expires after 24 hours
- - maxCalls limits total transactions (10,000)
- - Spending policy caps USDC amount ($50/day, enforced on-chain)
Pricing Strategies
| Model | Price | Use Case | |-------|-------|----------| | Per-request | $0.001 - $0.01 | Data APIs, price feeds, simple queries | | Per-inference | $0.003 - $0.05 | AI models, LLM calls, image generation | | Per-compute | $0.01 - $0.10 | Heavy computation, batch processing | | Free tier | $0 | Price endpoints, health checks, discovery |
Set your price in the middleware amount parameter. The facilitator handles verification and settlement.
Full Documentation
- - x402 Protocol Guide — how x402 works
- - x402 Server Setup — middleware configuration
- - x402 + Session Keys — automated payments
- - Spending Policies — on-chain budget control
- - x402 Facilitator — live on StarkNet mainnet