x402 HTTPpylive · you pay per call

The same paywall, on FastAPI

A minimal FastAPI server with a payment-protected /ask endpoint using the payments-py SDK and ASGI middleware.

http-simple-agent-py/
1 What you'll learn

The Python twin of the Express tutorial — identical x402 flow, FastAPI stack.

  • Protect a FastAPI route with the payments-py ASGI payment middleware
  • Handle the same 402 → sign → 200 round-trip the TypeScript version does
  • Mint the x402 token on the client with the payments-py SDK
  • Track OpenAI cost per call with Nevermined observability
2 How it works

Same contract as the TypeScript tutorial: no token yields 402 with a base64 payment-required header; the client mints an x402 token, retries with payment-signature; the agent verifies, executes, and settles credits before 200 with a payment-response receipt.

POST /askno token
402payment-required
sign + retrypayment-signature
200payment-response
3 Under the hood
FastAPIPython 3.10+payments-pyOpenAIPoetry
# src/agent.py — the protected endpoint (shape)
# FastAPI app with the Nevermined ASGI payment middleware.
# /ask returns 402 until a valid x402 payment-signature is presented,
# then verifies, runs the LLM, and settles credits before responding.
src/agent.pyFastAPI server with a payment-protected /ask endpoint
src/agent_observability.pysame agent with Nevermined observability for OpenAI cost
src/client.pydemo client showing the complete x402 payment flow
4 See it run
The same paywall, on FastAPI

Sandbox agent — real payment round-trips (402 → authorize → settle) with a per-session credit balance, no external service and no real money. Same x402 flow via payments-py; the sandbox runs the round-trip locally. See the README to point this at a hosted instance of the agent.