Skip to navigation

Go SDK quickstart

Charge for your first net/http route with one piece of middleware, through to settlement.

View as Markdown

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.

go get github.com/mitulabs/payments/go

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.

export MITU_API_KEY=your_test_key

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.

main.go
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
mitu "github.com/mitulabs/payments/go"
)
func main() {
m, err := mitu.New(context.Background(), mitu.Options{APIKey: os.Getenv("MITU_API_KEY")})
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.Handle("GET /v1/search", m.Charge(mitu.Price{Amount: "0.05"})(http.HandlerFunc(search)))
log.Fatal(http.ListenAndServe(":8080", mux))
}
func search(w http.ResponseWriter, r *http.Request) {
payment, _ := mitu.PaymentFromContext(r.Context())
json.NewEncoder(w).Encode(map[string]any{"paymentId": payment.ID})
}

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.

  1. 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.
  2. The caller pays.
    The caller completes the payment and sends the same request again, this time with a payment credential attached.
  3. 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.
  4. Your handler runs.
    The request reaches your handler with the settled payment stored in its context, which you read with mitu.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.

curl -i http://localhost:8080/v1/search

You should see a response that begins like this.

HTTP/1.1 402 Payment Required

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.

FrameworkHow to add payments to a route
net/httpmux.Handle("GET /v1/search", m.Charge(price)(handler))
chir.With(m.Charge(price)).Get("/v1/search", handler)
Ginrouter.GET("/v1/search", mitugin.Charge(m, price), handler)
Echo v5e.GET("/v1/search", handler, mituecho.Charge(m, price))
Fiber v3app.Get("/v1/search", mitufiber.Charge(m, price), handler)

Whichever framework you use, your handler reads the payment with mitu.PaymentFromContext.

Next steps