How to Monetize a FastAPI API With Pay-Per-Request x402 Payments
Add payments to FastAPI, charge per API request, and create a FastAPI API paywall with an x402 payment challenge and managed settlement.
To monetize a FastAPI API, choose a valuable route, declare its per-request USDC price in Python, return an x402 payment challenge to unpaid clients, verify and settle a valid payment, and run the original handler only after the payment succeeds. Test the entire flow on Base Sepolia before moving the installation to Live.
Key takeaways
- Start with one stable, read-only endpoint whose response has clear standalone value.
- Keep the route, price, access policy, and discovery intent beside the FastAPI handler in Python.
- An unpaid request should receive HTTP 402; the original response should run only after a verified paid retry.
- Test invalid amounts, recipients, networks, signatures, replays, and retries before accepting real USDC.
- Keep authentication and authorization separate from payment unless the route is intentionally public after purchase.
Start with a route people or agents already want
Do not begin by putting a FastAPI paywall in front of the entire application. Pick one endpoint with a stable method and path, a predictable cost to serve, and a result that is useful on its own.
Good first candidates include a document conversion, enrichment lookup, generated report, current dataset, or specialized calculation. Health checks, authentication routes, webhooks, and internal administration endpoints are poor candidates.
The goal is to sell one result. If the buyer has to understand the rest of your product before the endpoint makes sense, the unit is probably still too large.
Install the FastAPI payment adapter
Create a FastAPI installation inside an Access402 project, then install the Python package with pip install access402-fastapi. Store the installation ID and scoped API key in the server environment. The key must never appear in frontend code, OpenAPI output, logs, or a committed environment file.
Use a separate installation for each local, staging, and production deployment. That keeps credentials revocable and makes it possible to understand which server synchronized a route or produced a payment event.
Declare the price beside the route
The FastAPI application should remain the source of truth for which endpoint is protected. Add the Access402 decorator to the selected route and declare the USDC price in code. That makes payment policy reviewable in the same pull request as the handler.
A minimal paid route initializes Access402 from the server environment, adds @access402.protect(price="0.05") to the endpoint, and calls access402.install(app) after route declarations. Starting the application synchronizes the protected route and its OpenAPI details with the installation.
Only decorated routes should become payable. An application with hundreds of endpoints can start with one paid action without changing the rest of its behavior.
Understand what happens on an unpaid request
When a client requests the protected route without payment, the adapter should not call the original handler. It returns HTTP 402 with machine-readable payment requirements for that exact resource.
- The amount must match the price declared by the server.
- The receiving address must match the project wallet.
- Sandbox should identify Base Sepolia; Live should identify Base mainnet.
- The challenge should describe the exact method and resource being purchased.
- The response must not contain the premium result before payment succeeds.
Verify the paid retry before running the handler
A compatible client reads the challenge, signs a payment authorization, and retries the original FastAPI request with the payment payload. Access402 checks the submitted terms against the synchronized server-side rule before verification and settlement.
If the amount, asset, network, recipient, resource, signature, or authorization window is wrong, the request fails closed. If payment succeeds, the adapter records the result and allows the original FastAPI handler to return its normal response.
That order matters. The expensive calculation, private query, or file delivery should happen after the payment decision, not before it.
Test the failures, not only the happy path
A successful Sandbox payment proves very little by itself. Before moving a FastAPI API paywall to production, change one payment field at a time and make sure the request stays closed.
- Request the route without payment and confirm HTTP 402.
- Pay the correct challenge and confirm the original response is returned once.
- Submit the wrong amount, recipient, asset, and network.
- Retry with an expired or already-used authorization.
- Repeat the same idempotency key and confirm it does not create a second settlement.
- Make verification unavailable and confirm the API fails closed.
Move one paid FastAPI route to Live
When the Sandbox behavior is correct, verify the production receiving wallet and set ACCESS402_MODE=live in the server environment. Make one small real payment and independently confirm the transaction before sending meaningful traffic.
Then publish accurate discovery metadata only for routes that a third-party buyer can actually call. A public listing for an endpoint with undocumented parameters, private authentication, or unusable dynamic paths creates failed demand instead of revenue.
The first goal is not to convert the whole API into a marketplace. It is to learn whether one useful capability can attract paid requests. Expand only when the result justifies another route.
Put this into practice
Continue from the problem to the implementation path that fits your resource.
Sources and further reading
Primary documentation reviewed for the factual product and protocol claims in this article.

