Welcome to Part 4 of building a real-time data API that AI agents can pay for with USDC micropayments via the x402 protocol.
This week was less about shipping features and more about wrestling with FastAPI’s opinionated defaults. I’m not a FastAPI expert, so a couple of its “helpful” behaviors caused real friction with the strict requirements of x402. Here’s what broke, why it broke, and how I fixed it.
Quirk #1: The Dreaded "detail" Wrapper
The x402 protocol is strict. When an agent hits an unpaid endpoint, you must return HTTP 402 with a JSON body whose root keys match the challenge format exactly (typically accepts , resource , and related fields). No wrapping allowed.
My first attempt used FastAPI’s standard exception:
raise HTTPException ( status_code = 402 , detail = { "accepts" : [ . . . ] , "resource" : "..." , # other x402 fields } )
Enter fullscreen mode
Exit fullscreen mode
FastAPI automatically wraps the payload like this:
{ "detail" : { "accepts" : [ ...…