I just built and deployed three fully autonomous APIs that AI agents discover, pay USDC micropayments on Base, and get data from — zero human involvement after setup. Crypto prices, stock data, forex rates, web scraping, AI summaries — all paywalled with real micropayments.
Here's how I did it, the tech stack, challenges, and how you can build your own.
The Problem With Traditional APIs
Traditional APIs need subscriptions, API keys, credit cards, and billing dashboards. For human developers, that's manageable. But AI agents can't sign up for accounts. They can't enter credit card numbers. They can't manage monthly invoices.
If agents are going to consume APIs autonomously, they need a way to just pay and go.
The Solution: x402 Protocol
HTTP status code 402 — "Payment Required" — has existed since the early web but was never implemented. Coinbase built the x402 protocol to finally give it meaning:
Agent sends a request to my API
Server responds with HTTP 402 + payment instructions (amount, wallet, network)
Agent signs a USDC transfer on Base
Agent retries the request with a payment signature header
Server verifies payment via the CDP facilitator
Server returns the data
USDC lands in my wallet
The whole thing takes about 200ms. No accounts. No API keys. No subscriptions.
What I Built: 3 APIs, 22 Endpoints
API 1: Crypto Data Enrichment — 4 Endpoints
Live crypto market intelligence for AI agents and trading bots.
EndpointPriceWhat It ReturnsGET /api/v1/price/:symbol$0.01Real-time price, 24h change, volume, market capGET /api/v1/signal/:symbol$0.05Trading signal (BUY/HOLD/SELL) with confidence scoreGET /api/v1/deep-analysis/:symbol$0.10On-chain metrics, sentiment, risk assessmentGET /api/v1/llm-report/:symbol$0.25Claude AI-written investment analysis
Supports 53 symbols including BTC, ETH, SOL, PEPE, ARB, SUI, TON, and more. Data from CoinGecko free API.
Live: https://crypto-enrichment-api-production.up.railway.app
API 2: Web Extract — 10 Endpoints
Web scraping and content extraction for AI agents that need to read the web.
EndpointPriceWhat It ReturnsPOST /extract/text$0.01Clean text from any URLPOST /extract/metadata$0.02Title, author, images, links, headingsPOST /extract/contacts$0.03Emails, phones, social media linksPOST /extract/pdf$0.03Text extraction from PDF URLsPOST /extract/structured$0.05Full structured data with tables and JSON-LDPOST /extract/product$0.05Product price, reviews, ratingsPOST /extract/batch$0.08Extract from up to 5 URLs at oncePOST /extract/compare$0.08Compare two pages with AI analysisPOST /extract/translate$0.10Extract and translate to any languagePOST /extract/ai-summary$0.15Claude-powered page summary
All endpoints accept {"url": "https://example.com"} as the request body.
Live: https://web-extract-api.up.railway.app
API 3: Finance Data — 8 Endpoints
Stock prices, company data, forex rates, and AI analysis for financial agents.
EndpointPriceWhat It ReturnsGET /stocks/price/:symbol$0.02Real-time stock price with day statsGET /stocks/profile/:symbol$0.03Company profile, sector, key statsGET /stocks/news/:symbol$0.05Latest 10 news articlesGET /stocks/financials/:symbol$0.10Income statement, balance sheet, cash flowGET /forex/rate/:pair$0.01Exchange rate (e.g., USD-EUR) with 5-day historyPOST /forex/convert$0.02Convert amount between currenciesGET /market/overview$0.05S&P 500, Dow, NASDAQ, Gold, Oil, YieldsGET /market/ai-report/:symbol$0.25Claude AI-powered stock analysis
Data from Yahoo Finance free API. Covers all major US stocks, ETFs, and forex pairs.
Live: https://finance-data-api-production.up.railway.app
The Tech Stack
All three APIs use the same core stack:
Server: Express.js 4 with @x402/express payment middleware
Payment: @coinbase/x402 for CDP facilitator authentication
Discovery: @x402/extensions for Bazaar, plus MCP config and A2A agent cards
AI: Claude API (Anthropic) for the LLM-powered endpoints
Scraping: Cheerio for HTML parsing (Web Extract API)
Blockchain: USDC on Base mainnet (eip155:8453)
Hosting: Railway (Docker containers)
Wallet: MetaMask on Base network
The Core Code
The magic is surprisingly simple. One middleware handles all payment logic:
javascriptimport { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
import { createFacilitatorConfig } from "@coinbase/x402";
const facilitatorConfig = createFacilitatorConfig(
process.env.CDP_API_KEY_ID,
process.env.CDP_API_KEY_SECRET
);
const facilitatorClient = new HTTPFacilitatorClient(facilitatorConfig);
const resourceServer = new x402ResourceServer(facilitatorClient)
.register("eip155:8453", new ExactEvmScheme());
app.use(
paymentMiddleware(
{
"GET /api/v1/price/": {
accepts: [{
scheme: "exact",
price: "$0.01",
network: "eip155:8453",
payTo: "0xYourWallet",
}],
description: "Real-time crypto price data",
},
},
resourceServer
)
);
That's it. One middleware call, and your endpoint is paywalled with USDC.
Agent Discovery
For agents to find my APIs, each server exposes three discovery mechanisms:
MCP Config (/.well-known/mcp.json) — for Claude, GPT, and other MCP-compatible AI models to discover available tools.
A2A Agent Card (/.well-known/agent.json) — for Google's Agent-to-Agent protocol.
Bazaar Extensions — using @x402/extensions/bazaar, the CDP facilitator automatically catalogs endpoints after the first payment.
All three APIs are also registered on x402scan.com for manual discovery.
Economics
Here's why this model works:
APIPrice RangeUpstream CostProfit MarginCrypto$0.01 - $0.25Free (CoinGecko) + Claude85-90%Web Extract$0.01 - $0.15Free (Cheerio) + Claude85-95%Finance$0.01 - $0.25Free (Yahoo Finance) + Claude85-90%
At 1,000 requests/day across all endpoints: roughly $1,500-2,400/month.
Total cost to run: $15/month hosting (3 Railway services). Total upfront investment: ~$55 including USDC working capital.
Challenges I Faced
Express 5 vs Express 4: The x402 middleware was built for Express 4. Express 5 changed how middleware intercepts routes. Downgrading fixed it.
Route wildcards: The middleware didn't match :symbol parameters. Switching to wildcard patterns (/api/v1/price/) solved it.
CDP key authentication in Docker: Node.js 20+ is required for Ed25519 JWT signing used by the CDP facilitator.
Package version mismatch: @coinbase/x402@^2.1.0 caused 401 errors with the CDP facilitator. Pinning to ^1.0.0 fixed it. If your server crashes with "Facilitator getSupported failed (401): Unauthorized", check this version.
Docker build cache: Railway caches Docker layers aggressively. If you change package versions, force a clean rebuild by updating the Dockerfile comment or deleting and redeploying the service.
What I Learned
x402 is production-ready. The SDK works, payments settle in seconds, and the tooling is solid.
The ecosystem is early but growing. Discovery is still fragmented across x402scan, Bazaar, MCP, and A2A. Register on all of them.
Micropayments work for machines. The psychological friction that killed micropayments for humans doesn't exist for AI agents.
Base is the right chain. Sub-cent gas fees, 1-second finality, native Coinbase ecosystem support.
Free data sources are gold. CoinGecko, Yahoo Finance, and Cheerio cost nothing. Wrap them in x402 and charge per request.
AI-powered endpoints command higher prices. Adding Claude-powered analysis lets you charge 5-25x more than raw data endpoints.
Try It
Crypto Data API
Root: https://crypto-enrichment-api-production.up.railway.app
Test 402: https://crypto-enrichment-api-production.up.railway.app/api/v1/price/BTC
MCP: https://crypto-enrichment-api-production.up.railway.app/.well-known/mcp.json
Web Extract API
Root: https://web-extract-api.up.railway.app
Health: https://web-extract-api.up.railway.app/health
MCP: https://web-extract-api.up.railway.app/.well-known/mcp.json
Finance Data API
Root: https://finance-data-api-production.up.railway.app
Test 402: https://finance-data-api-production.up.railway.app/api/v1/stocks/price/AAPL
MCP: https://finance-data-api-production.up.railway.app/.well-known/mcp.json
All three APIs accept USDC on Base. All payments go to a single wallet. All endpoints are discoverable via MCP, A2A, and the x402 Bazaar.
Build Your Own
If you want to build your own x402 API:
Pick a free data source (weather, news, sports, anything)
Wrap it in an Express.js server with @x402/express
Set up a MetaMask wallet on Base
Get CDP API keys from https://portal.cdp.coinbase.com
Deploy to Railway with a Dockerfile
Register on x402scan.com
Add MCP and agent card endpoints for discovery
The entire setup takes about 2 hours. The x402 SDK handles all payment verification, so you just write your API logic and let the middleware handle the rest.
Building with x402? I'd love to hear about it. Drop a comment or find me on Twitter.
Tags: x402, ai, crypto, api, micropayments, usdc, base, express, nodejs, agenticpayments
Top comments (0)