Prerequisites
Before you begin, ensure you have:- A crypto wallet to receive funds (any EVM or SVM compatible wallet)
- Node.js and npm, Go, or Python and pip installed
- An existing API or server
There are pre-configured examples available in the x402 repo for both Node.js and Go. There is also an advanced example that shows how to use the x402 SDKs to build a more complex payment flow.
1. Install Dependencies
- Express
- Next.js
- Hono
- Fastify
- Go (Gin)
- Go (Echo)
- FastAPI
- Flask
Install the x402 Express middleware package.
2. Add Payment Middleware
Integrate the payment middleware into your application. You will need to provide:- The Facilitator URL or facilitator client. For testing, use
https://x402.org/facilitatorwhich works on Base Sepolia and Solana devnet.- For mainnet setup, see Running on Mainnet
- The routes you want to protect.
- Your receiving wallet address.
- Express
- Next.js
- Hono
- Fastify
- Go (Gin)
- Go (net/http)
- Go (Echo)
- FastAPI
- Flask
Full example in the repo here.
Payment Schemes: Exact, Upto, and Batch Settlement
x402 supports several payment schemes that control how charges are calculated:exact (default) — The client pays the exact advertised price. This is the simplest scheme and works across all networks (EVM, SVM, Stellar, Aptos) and all SDKs (TypeScript, Go, Python). Best for fixed-price endpoints where the cost is known upfront.
upto — The client authorizes a maximum amount, but the server settles only what was actually used. This enables usage-based billing where the final charge depends on work performed (LLM token count, compute time, bytes served, etc.). Currently available on EVM networks only (Permit2), in TypeScript, Go, and Python SDKs.
batch-settlement — For high-frequency or repeated micropayment traffic, the buyer funds a channel once, signs off-chain vouchers per request, and the seller settles onchain in batches (not every request). Each call still advertises a per-request maximum (price); you can charge actual usage up to that cap using the same setSettlementOverrides pattern as upto. See Batch settlement and the Payment Schemes overview.
The examples in step 2 above all use the exact scheme. To use upto instead, there are two key differences:
- Set
scheme: "upto"in your route config, wherepricebecomes the maximum the client authorizes - Call
setSettlementOverridesin your handler to specify the actual amount to charge
- Express
- Go (Gin)
- FastAPI
Full example in the repo here.
setSettlementOverrides amount supports three formats:
- Raw atomic units — e.g.,
"1000"settles exactly 1,000 atomic units of the token (for USDC with 6 decimals,"1000"= $0.001) - Percentage of authorized maximum — e.g.,
"50%"settles 50% ofPaymentRequirements.amount. Supports up to two decimal places (e.g.,"33.33%"). The result is floored to the nearest atomic unit. - Dollar price — e.g.,
"$0.05"converts a USD-denominated price to atomic units. This format works when you configured your route with$-prefixed pricing (e.g.,price: "$0.10"). Token decimals are determined from the registered scheme. The result is rounded to the nearest atomic unit.
"0", no onchain transaction occurs and the client is not charged.
3. Test Your Integration
To verify:- Make a request to your endpoint (e.g.,
curl http://localhost:4021/weather). - The server responds with a 402 Payment Required, including payment instructions in the
PAYMENT-REQUIREDheader. - Complete the payment using a compatible client, wallet, or automated agent. This typically involves signing a payment payload, which is handled by the client SDK detailed in the Quickstart for Buyers.
- Retry the request, this time including the
PAYMENT-SIGNATUREheader containing the cryptographic proof of payment. - The server verifies the payment via the facilitator and, if valid, returns your actual API response (e.g.,
{ "data": "Your paid API response." }).
4. Enhance Discovery with Metadata (Recommended)
When using a facilitator that supports the Bazaar extension, your endpoints can be listed in the x402 Bazaar, the discovery layer that helps buyers and AI agents find services. For HTTP endpoints, add the discovery extension to your route config:5. Error Handling
- If you run into trouble, check out the examples in the repo for more context and full code.
- Run
npm installorgo mod tidyto install dependencies
Running on Mainnet
Once you’ve tested your integration on testnet, you’re ready to accept real payments on mainnet.1. Update the Facilitator URL
For mainnet, use a production facilitator. See Facilitators for selected options. Example using one facilitator:- Node.js
- Go (Gin)
- Go (Echo)
- Python (FastAPI)
- Python (Flask)
2. Update Your Network Identifier
Change from testnet to mainnet network identifiers:- Base Mainnet
- Solana Mainnet
- Multi-Network
3. Register Multiple Schemes (Multi-Network)
For multi-network support, register both EVM and SVM schemes:- Express / Hono / Fastify
- Go (Gin)
- Go (Echo)
- Python
4. Update Your Wallet
Make sure your receiving wallet address is a real mainnet address where you want to receive USDC payments.5. Test with Real Payments
Before going live:- Test with small amounts first
- Verify payments are arriving in your wallet
- Monitor the facilitator for any issues
Network Identifiers (CAIP-2)
x402 v2 uses CAIP-2 format for network identifiers:| Network | CAIP-2 Identifier |
|---|---|
| Base Mainnet | eip155:8453 |
| Base Sepolia | eip155:84532 |
| Solana Mainnet | solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp |
| Solana Devnet | solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1 |
Next Steps
- Check out the Advanced Example for a more complex payment flow
- Explore Advanced Concepts like lifecycle hooks for custom logic before/after verification/settlement
- Explore Extensions like Bazaar for service discovery
- Get started as a buyer
Summary
This quickstart covered:- Installing the x402 SDK and relevant middleware
- Adding payment middleware to your API and configuring it
- Choosing between
exact(fixed-price),upto(usage-based per settlement), andbatch-settlement(EVM micropayments with batched redemption) payment schemes - Testing your integration
- Deploying to mainnet with CAIP-2 network identifiers