DEV Community

M M
M M

Posted on • Originally published at monapi.dev

I built an API that roasts your code for $0.02

I wanted to show that API monetization for AI agents actually works. Not in theory — with a live endpoint that takes money and returns results.

So I built Roast my Code: an API that accepts a code snippet, charges $0.02 in USDC, and returns a brutal but technically accurate code review powered by an LLM.

It's live right now: roast.monapi.dev

How it works

Send code to the API without paying:

curl -X POST https://roast.monapi.dev/api/roast \
  -H "Content-Type: application/json" \
  -d '{"code": "function add(a,b) { return a - b; }", "language": "javascript"}'
Enter fullscreen mode Exit fullscreen mode

You get back:

HTTP 402 Payment Required
Enter fullscreen mode Exit fullscreen mode

The response body contains everything an x402-compatible agent needs: price ($0.02), token (USDC), network, wallet address. The agent pays, retries, and gets the roast.

What a roast looks like

I fed it this classic:

function isEven(n) {
  if (n === 0) return true;
  if (n === 1) return false;
  return isEven(n - 2);
}
Enter fullscreen mode Exit fullscreen mode

The response:

"Recursion: because why use modulo when you can make the call stack suffer?"

Call isEven(1000000) and watch your browser weep. This will crash faster than a crypto startup. The iterative n % 2 === 0 exists for a reason.

Negative numbers are your kryptonite — isEven(-5) triggers infinite recursion into the shadow realm.

Performance theater — O(n) time complexity for something that should be O(1). It's like taking a helicopter to the mailbox.

Brutal, but every point is technically correct.

The entire server

The whole thing is ~40 lines. Here's the core:

import express from "express";
import { monapi } from "@monapi/sdk";
import { generateRoast } from "./llm.js";

const app = express();
app.use(express.json());

app.use("/api/roast", monapi({
  wallet: process.env.MONAPI_WALLET!,
  price: 0.02,
  network: "base-sepolia",
}));

app.post("/api/roast", async (req, res) => {
  const { code, language } = req.body;

  const roast = await generateRoast(code, language);

  res.json({
    roast,
    language,
    linesAnalyzed: code.split("\n").length,
  });
});
Enter fullscreen mode Exit fullscreen mode

That's it. The monapi() middleware handles the entire payment flow — 402 response, payment verification, settlement. The route handler only runs after payment is confirmed.

Why this exists

This isn't just a toy. It demonstrates a real problem and a real solution:

The problem: AI agents are becoming major API consumers. They can call your endpoint, but they can't sign up for an account or enter a credit card. Today, you either block them or serve them for free.

The solution: HTTP 402. The agent hits your API, gets a price, pays in USDC, gets the data. No signup, no API keys, no invoicing. Fully automated.

The Roast API is a proof of concept — but the pattern works for any API. Weather data, financial feeds, code analysis, search results. Anything an AI agent would pay for.

The economics

  • LLM cost is ~$0.001 per roast
  • I charge $0.02
  • That's a 95% margin
  • Network fees on Base are < $0.01 (sponsored by the x402 facilitator)

If an agent roasts 1,000 code snippets, that's $20 in revenue vs ~$1 in LLM costs. The math works even at micro-scale.

Try it

The API is live on Base Sepolia (testnet). Hit the root endpoint to see pricing:

curl https://roast.monapi.dev
Enter fullscreen mode Exit fullscreen mode
{
  "name": "Roast my Code",
  "pricing": { "POST /api/roast": "$0.02 USDC" },
  "network": "base-sepolia",
  "poweredBy": "monapi.dev"
}
Enter fullscreen mode Exit fullscreen mode

To actually pay and get a roast, you need an x402-compatible client (like Coinbase AgentKit or the @x402/fetch wrapper).

Build your own

Want to build something like this? The SDK is open source:

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

Full docs: monapi.dev | GitHub | npm


This is part 3 of a series on monetizing APIs for AI agents. Part 1: One line of code · Part 2: Paid MCP tools

Top comments (0)