How to Pay for AI Services with USDC in One HTTP Call
What if paying for an API was as simple as making the request? No API keys. No OAuth tokens. No billing dashboard. No account creation. Just send a request, pay in USDC, and get your result.
That is exactly what the x402 protocol makes possible. And at 24K Labs, every one of our six AI services runs on it.
What is HTTP 402?
HTTP 402 Payment Required has been in the HTTP specification since 1991. It was reserved for "future use" -- and then ignored for thirty-five years. The idea was simple: a server should be able to say "this resource costs money" using a standard status code, the same way 401 means "you need to authenticate" and 404 means "not found."
The problem was that in 1991, there was no way to make that payment happen programmatically. Credit cards needed human input. PayPal did not exist yet. Micropayments were a pipe dream.
Then crypto happened. Specifically, stablecoins on fast L2 networks happened. USDC on Base settles in under a second for fractions of a cent in gas fees. Suddenly, a machine can pay another machine.
The x402 Flow: Three Steps
The x402 protocol, developed by Coinbase and backed by the Linux Foundation, turns HTTP 402 into a real payment flow. Here is how it works:
Step 1: Make the Request
You send a normal HTTP request to the service endpoint. Nothing special -- just a regular POST with your payload.
POST https://api.24klabs.ai/api/code-review
Content-Type: application/json
{
"code": "def hello():\n print('world')",
"language": "python"
} Step 2: Receive the 402
If you have not paid, the server responds with HTTP 402 and a payment requirement header. This tells you exactly what to pay, where to pay it, and on which network.
HTTP/1.1 402 Payment Required
X-Payment-Required: true
X-Payment-Amount: 10000000
X-Payment-Currency: USDC
X-Payment-Network: base
X-Payment-Address: 0x... The amount is in the token's smallest unit. For USDC, that means 10000000 = $10.00 (USDC has 6 decimals).
Step 3: Pay and Retry
Your client signs a USDC transaction on Base, gets the receipt, and retries the original request with the payment proof attached.
POST https://api.24klabs.ai/api/code-review
Content-Type: application/json
X-Payment-Receipt: 0xabc123...
{
"code": "def hello():\n print('world')",
"language": "python"
} The server verifies the payment on-chain, processes your request, and returns the result. Done.
Why This Matters for AI Agents
This is not just a neat payment trick. It is a fundamental shift in how software services get consumed.
Traditional APIs require you to create an account, generate an API key, store it securely, rotate it periodically, and manage billing separately. Every service has its own dashboard, its own billing page, its own rate limits tied to your plan tier.
With x402, the payment IS the authentication. If you paid, you are authorized. No keys to leak. No accounts to manage. No credentials to rotate. An AI agent with a USDC wallet can discover, evaluate, and pay for services autonomously.
Think about what that enables. An AI coding assistant that needs a security audit can find the endpoint, pay $25.00, and get the report -- without a human ever setting up an account. A CI/CD pipeline that needs code review can pay per PR. A research agent that needs analysis can pay per query.
Python Example with the x402 SDK
Here is a complete working example using the Coinbase x402 Python SDK:
import httpx
from x402.client import Client
# Initialize with your wallet
client = Client(
private_key="0xYOUR_PRIVATE_KEY",
network="base"
)
# Make a paid request -- the SDK handles the 402 flow
response = client.post(
"https://api.24klabs.ai/api/code-review",
json={
"code": open("main.py").read(),
"language": "python",
"options": {
"focus": ["security", "performance"],
"severity_threshold": "medium"
}
}
)
# The SDK automatically:
# 1. Sends the initial request
# 2. Receives the 402
# 3. Signs and sends the USDC payment
# 4. Retries with the receipt
# 5. Returns the final response
print(response.json()) That is it. One function call. The SDK handles the entire 402 negotiation, payment signing, and retry logic under the hood.
What You Can Pay For
24K Labs runs six AI-powered code analysis services on the x402 protocol. Every one of them follows the same flow described above:
- Explain Code (from $0.50) -- Clear explanation of what code does and why, powered by Claude Haiku
- Debug Assist (from $2.00) -- Bug detection, error explanation, and fix suggestions, powered by Claude Sonnet
- Code Review (from $10.00) -- PR-level analysis with severity scoring, powered by Claude Sonnet
- Security Audit (from $25.00) -- OWASP Top 10 scanning with CVSS scores and remediation steps, powered by Claude Opus
- Automation Script (from $15.00) -- Production-ready scripts from plain-English requirements, powered by Claude Sonnet
- MCP Blueprint (from $50.00) -- Complete MCP server architecture from a requirements description, powered by Claude Opus
Getting Started
You need two things: a wallet with USDC on Base, and a way to make HTTP requests. That is literally it. No signup. No API key. No free trial that expires.
# Install the x402 SDK
pip install x402-python
# Or use curl and handle the 402 manually
# The protocol is just HTTP -- any language works The x402 protocol is open. The payment flow is standardized. Any client that speaks HTTP can participate. Your existing code just needs a thin payment layer on top.
Try It Now
Send a request to any 24K Labs endpoint and see the x402 flow in action. No account needed -- just USDC on Base.
View Quickstart Guide