DEV Community

Cover image for How to monetize your API for AI agents (with one line of code)
M M
M M

Posted on • Originally published at monapi.dev

How to monetize your API for AI agents (with one line of code)

You built an API. It's good. Developers use it. But there's a new type of customer knocking on your door — and they can't sign up.

AI agents are the fastest-growing consumer of APIs right now. They discover endpoints, try to call them — and hit your signup page. Account creation, email verification, credit card form. They can't continue. They leave. You never know they were there.

This isn't a future problem. It's happening today.

The opportunity you're missing

AI agents already spend millions on API calls every month. Coinbase's x402 protocol has settled over 75 million transactions. The agent economy is real.

Quick math: 10,000 agents call your API once a day at $0.01 each. That's $100/day — or $36,500/year. From traffic you're currently losing.

The solution: HTTP 402

HTTP 402 means "Payment Required." It's been in the HTTP spec since 1997. Nobody used it — until now.

Here's how it works:

  1. An agent sends a request to your API
  2. Your server responds with 402 Payment Required (price, token, wallet)
  3. The agent pays in USDC and retries
  4. Your API responds with 200 OK and the data

No accounts. No API keys. No invoices. Just HTTP.

Add it to your API in 5 minutes

Install:

npm install @monapi/sdk @x402/express
Enter fullscreen mode Exit fullscreen mode

Add one function call:

import express from "express";
import { monapi } from "@monapi/sdk";

const app = express();

app.use(monapi({
  wallet: process.env.WALLET,
  price: 0.01, // $0.01 per request
}));

app.get("/api/weather", (req, res) => {
  res.json({ temp: 22, city: "Berlin" });
});

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Test it:

$ curl -i localhost:3000/api/weather

HTTP/1.1 402 Payment Required
X-Payment-Price:    0.01
X-Payment-Token:    USDC
X-Payment-Network:  base
X-Payment-Wallet:   0x83...913
Enter fullscreen mode Exit fullscreen mode

Any x402-compatible agent reads this, pays automatically, and retries. Your API responds normally.

Per-route pricing

Not every endpoint is worth the same:

app.use(monapi({
  wallet: process.env.WALLET,
  routes: {
    "/api/weather":  0.01,   // simple lookup
    "/api/forecast": 0.05,   // more compute
    "/api/search":   0.10,   // expensive query
  },
}));
Enter fullscreen mode Exit fullscreen mode

Also works with Next.js and MCP (Model Context Protocol). Same SDK, different entry points.

What happens under the hood

  1. Request — Agent calls your endpoint
  2. 402 — monapi intercepts, returns payment terms
  3. Payment — Agent signs USDC transfer via EIP-3009 and retries
  4. 200 — Facilitator verifies, settles on-chain, your API responds

Key details:

  • Gas fees are sponsored. Agents only need USDC — no ETH, no MATIC. The x402 facilitator covers gas.
  • Settlement in under 2 seconds. On Base, Arbitrum, or Polygon.
  • Non-custodial. Payments go directly to your wallet. monapi never touches your funds.
  • Free and open source. MIT licensed. No monapi fees.

Who's already paying?

Agents built with Coinbase AgentKit, Cloudflare Workers, and the Vercel AI SDK already support x402 payments. The infrastructure exists today.

Get started

Three options:

  1. Interactive CLI: npx monapi init — detects your framework, configures your wallet, generates code
  2. Copy the code above — install, add middleware, deploy
  3. Read the full docs — every config option, every framework

Your API is already getting traffic from agents. The only question is whether you're getting paid for it.

Links:

Top comments (0)