Skip to main content

Getting started with FastAPI

Start accepting x402 payments in your FastAPI server in 2 minutes.
You can find the full code for this example here.

Step 1: Install dependencies

solana<0.40 is a temporary pin. x402[svm] requires solana>=0.36.0 with no upper bound, but solana 0.40 removed solana.rpc.api and rpc.types.TxOpts, which x402’s SVM signer still imports — so an unpinned install resolves to a version the SDK cannot use. A fix is proposed upstream in x402-foundation/x402#3071 — drop the pin once it ships in an x402 release.

Step 2: Set your environment variables

Your .env file should look like this:

Step 3: Create a new FastAPI app

Step 4: Run the server

Your server is now accepting x402 payments!

Step 5: Test the server

You can test payments against your server locally by following the httpx example or the requests example from the x402 repository. Just set your environment variables to match your local server, install the dependencies, and run the examples.

Going to production

This setup works on the free tier out of the box — no API keys required. Beyond the free tier the facilitator authenticates merchants with short-lived Ed25519 JWTs. Create a merchant account at merchant.payai.network, then add your keys to your .env:
There is no PayAI package for Python — the x402 SDK takes an auth_provider, so signing the token is all you need. Install cryptography and drop this file beside your main.py:
Then pass it to the facilitator client:
The provider caches each token and re-signs 30 seconds before expiry, so a long-running server signs roughly once every two minutes rather than on every request. See Facilitator Pricing for tier details and Facilitator Authentication for the protocol reference.

x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the x402 Reference.

Need help?

Join our Community

Have questions or want to connect with other developers? Join our Discord server.