HTTP 402 Payment Required: How AI Agents Handle It Automatically
The Future of API Monetization
HTTP 402 Payment Required is the forgotten HTTP status code—until now. As AI agents become autonomous economic actors, they need to handle paid APIs automatically, without human intervention.
What Is HTTP 402?
Originally designed for digital cash systems, 402 signals that payment is required before access is granted. AI agents can now:
- Detect the 402 response from the API
- Extract payment details from response headers
- Process payment automatically using on-chain wallets
- Retry the request with proof of payment
Practical Implementation
The code above demonstrates an AgentExchange that:
- Reads a machine-readable
.well-known/agent-beacon.jsonfile to discover available bots and pricing - Attempts requests without payment first (free tier)
- Catches 402 responses and extracts
X-Payment-AmountandX-Payment-Walletheaders - Automatically pays using USDC on Base network
- Retries with the
X-Paymentheader containing payment proof
Why It Matters
This pattern eliminates friction for AI agents accessing premium services. Instead of failing, agents negotiate payment autonomously—enabling micropayments, metered APIs, and true API marketplaces.
The result? Agents complete tasks end-to-end while developers focus on business logic, not payment plumbing.
Top comments (0)