Before you charge real money
- Move to the x402 SDK. This tutorial uses x402 version 1 message shapes
so you can read every check. New integrations should use version 2: the
@x402/express(or@x402/next,@x402/hono) middleware on the server,@x402/fetchon the client, and@x402/svmfor Solana. Version 2 sends the price in aPAYMENT-REQUIREDheader, and usesPAYMENT-SIGNATUREandPAYMENT-RESPONSEin place ofX-PAYMENTandX-PAYMENT-RESPONSE. x402 on Solana has a server and a client built with these packages. - Choose a facilitator. With the SDK, a facilitator verifies and settles payments for your server, and on Solana it can pay the network fee for the client. Use a hosted facilitator, run your own, or settle inside your server as this tutorial does. See x402 facilitators.
- Switch to mainnet values. Set the environment variables from the table below. Use an RPC provider: public endpoints are rate-limited.
- Create your USDC token account first. The client’s transfer doesn’t
create the
payTowallet’s USDC account, so create it before you publish the price, or every payment fails. - Store accepted payment signatures. The example keeps them in memory. Put them in your database so every server instance, and the server after a restart, refuses a payment that was already used.
- Log the signature with each paid request. It is your receipt: anyone can look the transaction up on the network.
- Rate-limit unpaid requests. A
402is cheap to send, but each one still costs you a request. - Limit what clients can spend. Give every client and agent a
maxAmount, and a wallet that holds only what it may spend.
Devnet and mainnet values
| Variable | Devnet (testing) | Mainnet (real money) |
|---|---|---|
RPC_URL | https://api.devnet.solana.com | Your RPC provider’s HTTPS URL |
RPC_SUBSCRIPTIONS_URL | wss://api.devnet.solana.com | Your RPC provider’s WebSocket URL |
USDC_MINT | 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v |
X402_NETWORK | solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1 | solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp |
| Money | Free test tokens | Real USDC |
You paywalled an API
You paid for an API as a client, read the server that takes the payment, and ran both on your machine. From here:Accept dollar payments
Next tutorial: add a checkout for people next to your API.
Let an agent pay
Launch an AI agent with Pay and let it pay for an x402 API, approving each payment first.
Machine payments (MPP)
Session-based payments between machines.
Kora
Run your own fee payer, so clients only hold USDC.