For inference, streaming, or other costly
exact requests, confirm settlement
before starting the work. Verification alone does not guarantee payment, and
middleware may settle after your handler runs. See payment safety
for ordering, expiry checks, and the separate guidance for funded channels.Getting started with Gin (Go)
Start accepting x402 payments in your Gin server in 2 minutes.You can find the full code for this example here.
Step 1: Create a Go module and install dependencies
Step 2: Set your environment variables
Your.env file should look like this:
Step 3: Preview the server code
This is themain.go the example uses. It loads your env, applies the x402 payment middleware, and defines protected and health routes.
Step 4: Run the server
Your server is now accepting 402 payments!
Step 5: Test the server
You can test payments against your server locally by following the fetch example or the axios example, or by using the Go client examples in the x402 repository.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 with your 1,000 free credits — no API key or portal signup is required. Production readiness still includes normal controls such as secret management, monitoring, and failure handling. After your free credits run out, 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 top up in the merchant dashboard. Add the resulting credentials to your.env:
AuthProvider, so
signing the token is all you need. Put this in payaiauth/provider.go:
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.

