I Built a Pay-Per-Call Trading Signal API for AI Agents

python dev.to

If you're building AI agents that trade, they need market signals. But most data APIs require human signup, API keys, and monthly subscriptions — none of which an autonomous agent can handle.

I built a trading signal API where AI agents pay per call using the x402 protocol (USDC micropayments on Base L2). No signup, no API keys. Agent shows up, pays half a cent, gets data.

How x402 Works

x402 revives the HTTP 402 ("Payment Required") status code:

  1. Agent calls GET /signal/NVDA
  2. Server responds with HTTP 402 + payment instructions
  3. Agent wallet signs a USDC transfer on Base L2
  4. Agent retries with a payment proof header
  5. Server verifies via Coinbase facilitator, returns data

The entire flow takes ~200ms.

The Signal Engine

Each call returns a composite momentum score built from:

  • RSI (14) — oversold/overbought detection
  • ADX (14) — trend strength (>25 = strong trend)
  • MACD (12/26/9) — crossover + direction
  • Volume ratio — current vs 20-day average

Score maps to BUY (>=30), SELL (<=-30), or HOLD, with a confidence value from 0 to 1.

Endpoints

Endpoint Price Returns
/signal/TICKER $0.005 Single stock signal
/scan/momentum $0.01 Top 10 momentum setups
/risk?tickers=X,Y $0.01 Portfolio risk analysis

Stack

  • FastAPI (sync endpoints for yfinance blocking I/O)
  • x402 Python SDK with ASGI middleware
  • LRU cache (200 entries, 5-min TTL)
  • Deployed on Fly.io

Open source: https://github.com/pmestre-Forge/signal-api

If you're thinking about building services for the emerging AI agent economy, this is a template you can fork and adapt.

Source: dev.to

arrow_back Back to Tutorials