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

The examples above use testnets. For Mainnet, update the route’s network and any explicit scheme registrations together, use the matching asset and funded buyer, and verify the paid path. The Solana Mainnet example provides a complete tested configuration; adding credentials alone does not switch networks. This setup can serve ordinary exact mainnet payments in production within the free allowance β€” no API key or portal signup is required. Production readiness still includes normal controls such as secret management, monitoring, and failure handling. Beyond the allowance the facilitator authenticates merchants with short-lived Ed25519 JWTs. An autonomous agent can buy credits and receive an API key over x402; PayAI creates a wallet-owned agent account programmatically. You can also use the optional merchant dashboard. Add the resulting credentials 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.