Go SDK quickstart
Charge for your first net/http route with one piece of middleware, through to settlement.
This guide takes you through charging for a single net/http route. By the end, a request without payment will receive a 402 Payment Required response, and a paid request will reach your handler along with the details of the payment. If you would like to understand the payment flow before you start, read the SDK overview.
Install the module
Add the SDK to your module with go get.
The SDK requires Go 1.26 or later. The core module depends only on the Go standard library, so it adds nothing else to your dependency tree.
Before you start
You will need a Mitu merchant account and a secret key, which you can create in the dashboard under Keys and secrets. Use a test key while you build. It connects to the sandbox, so you can test the whole flow without moving real money.
Keep your secret key out of your code by storing it in an environment variable.
Add payments to a route
The program below creates a Mitu client and puts one route behind a payment of 0.05 USD per request.
The mitu.New function creates a client from your key. You create it once when your program starts and use it on every route that charges. The Charge method takes a price and returns middleware that wraps your handler.
When a request reaches /v1/search, the following happens.
- The first request has no payment.
The middleware asks Mitu to create a payment for this route and price, then replies with 402 Payment Required. The response explains how much to pay and lists the MPP and x402 options the caller can use. - The caller pays.
The caller completes the payment and sends the same request again, this time with a payment credential attached. - The payment is checked and settled.
The middleware confirms that the credential belongs to a payment made for this route at this price, and Mitu verifies and settles it. - Your handler runs.
The request reaches your handler with the settled payment stored in its context, which you read withmitu.PaymentFromContext. The response includes the payment receipt in its headers.
Your handler never runs for a request that has not been paid, so you do not need to write any payment checks of your own. The Middleware page describes each step in more detail.
Try the route
Run your program and call the route without paying.
You should see a response that begins like this.
To complete the flow, pay with any MPP or x402 client, such as the mppx command line tool or an Agent Wallet. The paid request returns 200 OK along with your handler’s response.
Using a framework
The core Charge middleware works with net/http and with any router that accepts standard middleware, including chi. For Gin, Echo and Fiber, the SDK provides small adapter modules. Each adapter is a separate module, so the core module never pulls in a web framework you do not use.
Whichever framework you use, your handler reads the payment with mitu.PaymentFromContext.