Skip to main content

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 the main.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

This setup works on the free tier out of the box — no API keys required. Beyond the free tier the facilitator authenticates merchants with short-lived Ed25519 JWTs. Create a merchant account at merchant.payai.network, then add your keys to your .env:
There is no PayAI module for Go — the x402 SDK takes an AuthProvider, so signing the token is all you need. Put this in payaiauth/provider.go:
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.