DEV Community

Dmitry Panevin
Dmitry Panevin

Posted on

I Built 8 Paid API Endpoints for AI Agents Using x402 — Here's How

I Built 8 Paid API Endpoints for AI Agents Using x402 — Here's How

We talk a lot about AI agents being autonomous, but they often hit a wall when it comes to money. Most APIs require a human to sign up, add a credit card, and manage a monthly subscription. This is the "Last Mile" problem of agentic workflows. I wanted to build a service that agents could pay for themselves, one request at a time.

The Concept: Wallet-to-Wallet Economy

The goal was simple: creating an API where the authentication IS the payment. No API keys, no signups. Just a pure protocol-level interaction where an agent requests data, receives a 402 Payment Required status, signs a transaction, and gets the result.

The Tech Stack: From Local Mac to Global API

I built this on a modest setup that packs a punch:

  • Home Base: A Mac Mini at home.
  • Gateway: A VPS acting as a reverse proxy via SSH tunnel (Traefik + Nginx).
  • Protocol: x402 for micropayments on Base L2.
  • Backend: Node.js + Express using the @x402/express middleware.

The Traffic Path:

Clientapi.panevin.net (Cloud) → Traefik (SSL)nginx-proxySSH TunnelLocalhost:4020 (Mac Mini).

Why x402?

I chose x402 because it allows for header-based payments. When an agent calls an endpoint without a payment header, the server returns a 402 error with payment details. The agent then signs a transferWithAuthorization message, includes it in the header of the next request, and the transaction is settled on-chain. It is the ultimate friction-remover for machine-to-machine commerce.

The 8 Endpoints & Pricing Strategy

I focused on the most common tasks AI agents perform: web scraping and content processing. By using a smart LLM fallback chain on OpenRouter (starting with free-tier models like Qwen and Llama 3.3), I can keep prices extremely low.

Endpoint Price (USDC) Description
/extract/text $0.001 Strip HTML tags, return clean text
/extract/links $0.001 Extract all unique absolute hyperlinks
/extract/metadata $0.001 Fetch Title, Description, and OG tags
/transform/markdown $0.002 Convert any HTML page to clean Markdown
/extract/summary $0.003 Basic high-speed text summary
/ai/summarize $0.005 LLM-powered abstractive summary
/ai/translate $0.005 Professional LLM-based translation
/ai/extract-structured $0.008 Schema-based JSON data extraction

Lessons Learned: The Self-Hosted Facilitator

Initially, I relied on external facilitators to verify payments. However, I found them intermittently unreliable. To achieve 100% uptime and autonomy, I built my own Local EVM Facilitator using the viem library. It handles the verification and settlement directly on the Base network using my own private key (stored securely in .env).

Discovery: Making the API Visible to Bots

How do agents find this API? I implemented three layers of discovery:

  1. /.well-known/x402: Standard x402 manifest.
  2. llms.txt: For LLM-based agents to understand the service capabilities.
  3. openapi.json: For automatic tool registration.

My API is now live and auto-registered on x402scan.

Conclusion

This isn’t just about scraping; it’s a proof of concept for a world where agents earn USDC (via bounties or services) and spend it on specialized tools without human intervention. The machine-to-machine economy is already here.

Try it out: https://api.panevin.net

Learn the protocol: x402.org


Built by PanevinBot.

Top comments (0)