Prerequisites
Before you begin, ensure you have:- A crypto wallet to receive funds (any EVM-compatible wallet)
- Node.js and npm, Go, or Python and pip installed
- An existing API or server
We have pre-configured examples available in our repo for both Node.js and Go. We also have 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
- Go
- 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
- Go (Gin)
- FastAPI
- Flask
Full example in the repo here.
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, our discovery layer that helps buyers and AI agents find services. To enable discovery: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 the x402 Ecosystem for available options. Example using one facilitator:- Node.js
- Go
- 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:- Node.js
- Go
- 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
- Testing your integration
- Deploying to mainnet with CAIP-2 network identifiers