F

FastAPI x402 w/Payouts

Pay-per-inference on HuggingFace with x402+CDP

Created on 8th June 2025

F

FastAPI x402 w/Payouts

Pay-per-inference on HuggingFace with x402+CDP

The problem FastAPI x402 w/Payouts solves

Developers build and train models every day on HuggingFace and put them up for use in Spaces with Gradio or custom apps. HF does a great job of offering a low cost hosting options but there isn't a great way to monetize or charge for testing out custom trained models unless you are taken out side of HF which introduces friction to the experience. For this hackathon I built 2 things to solve for how we can.

  1. Give FastAPI a first-class x402 experience for taking micropayments for any endpoint in a permission-less way.
  2. Make cheap instant payouts to collaborators for revenue sharing on a pay-per-inference model.
  3. Monetize right in HuggingFace Spaces to get early feedback and balance costs and inference resources. People pay for what they like! Validation can come even earlier with x402 and api endpoints.

Training and setup models, inference apis and any other developer apis traditionally need to setup payments and have a website and checkout experience using traditional fiat methods.
When I started working on the hackathon I realized there was no coinbase/x402 python support so I started off by releasing the first version of fastapi-x402 on 6/7/25

One command install: pip install fastapi-x402
One line setup: init_x402(pay_to="0x...", network="base-sepolia")
One decorator usage: @pay("$0.01")

A Penny for your Thoughts is now achievable at scale. Fiat rails would make pay-per-inference untenable even with the lowest cost payment methods out there. But with x402 you can split a USDC penny up very easily and take advantage of x402 facilitators for gas-less transfers.

image

Middleware pattern → Standard FastAPI approach
Decorator pattern → Matches FastAPI's design philosophy
Automatic configuration → Just like FastAPI's dependency injection
✅ Multi-network support (5 networks)
✅ Real blockchain settlement
✅ Network control patterns
✅ Replay protection
✅ Error handling
📦 Production Readiness (10/10)
✅ Published on PyPI
✅ CI/CD pipeline
✅ Real transaction testing
✅ Clean package structure

After getting the package ready, I wante to demonstrate the instant payout problem solution. For sharing revenue sharing across the developer, HuggingFace for hosting cost, and AWS for model costs on Nova Canvas as the example 3 way revenue payout.
I setup the demo here in Spaces https://huggingface.co/spaces/hamel88/x402-fast-payout-demo
and for any of the 3 generation examples

  1. Text Generation with DistillGPT-2 (0.01 USDC) ,
  2. Sentiment Analysis with RoBERTa (0.005 USDC)
  3. Image Generation with Amazon Nova Canvas. (0.02 USDC)

image

The Generate button with no wallet connected will display the http 402 return and you should connect a wallet that has Base Sepolia USDC in it to pay for inference. Metamask on desktop & mobile app is supported.

You can open up Spaces Logs and watch the console logs for the Payout details to the 3 wallets.There is a central payout v2 wallet and 3 payout wallets
(10% remains in the payout wallet and the rest distributed /30%/30%/30%) and then also view the split payout on basescan.

image
image

Challenges I ran into

I had to figure out how to work within the FastAPI ecosystem, but I believe it's very high leverage for a developer to test the market with a paid api on day 1 of development without any web or mobile checkout needed. There were some challenges getting the txns to settle on any network that the Coinbase Facillitator supports and I had one bug that kept coming back no how to settle after verification of the payment. I ended up having to review line by line the x402 milddleware to avoid any mis-matched headers etc.
I was able to overcome the challenges by debugging schemes inspired by the existing TS examples from x402 and using the discord channel to ask questions to understand the different ways to use the CDP facilitator and the x402.org facilitator. The Payout idea came when I realized how powerful gas-less txn would be for these kind of pay per use examples and I was able to research how Wallets V2 api worked for creating the wallets and managing them by name vs address in the SDK.
I also ran into some interesting front-end issues in learning how to sign msges with Metamask for the demo as I wanted it to be useful for HuggingFace spaces and not directly programmable calls alone to the backend apis. I over came a bug on the signing by noticing only the /legacy/facilitator/verify could accept base64 for the paymentPayload, once I swapped to json it resolved the invalid_payload issue, then I had a evm signature issue - nasty bug related to signing version on metamask. Metamask eth_signTypedData_v4 instead of viem.js when I was testing in the browser revealed signature issues that the facilitator didn't like

  1. Don’t use the legacy base64 in sending the paymentPayload to the facilitator
  2. Use viem.js in the client (saves lot of time)
  3. Calling metamask directly for the signature just created issues on the evm signature check

I made the demo app in Spaces to work with MetaMask web or the MetaMask IOS app. It was not intended to be fully featured with all wallet apps but works nicely on desktop and mobile with MetaMask and could easily be implemented to work with WalletConnect etc.

Tracks Applied (2)

Best Use of x402pay + CDPWallet

My project built the first Python x402 package I called fastapi-x402 (pip install fastapi-x402) I released 6/7/25 before...Read More

Best Use of x402pay

The building of fastapi-x402 as the first python package for x402 https://pypi.org/project/fastapi-x402/

Discussion

Builders also viewed

See more projects on Devfolio