> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mitu.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mitu.ai/_mcp/server.

# Go SDK quickstart

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](/sdks/overview).

## Install the module

Add the SDK to your module with `go get`.

```bash
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](/platform/security/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.

```bash
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`**

```go title="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](/sdks/go/middleware) page describes each step in more detail.

## Try the route

Run your program and call the route without paying.

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

You should see a response that begins like this.

```http
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](/guides/agent-wallets). 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.

| Framework  | How to add payments to a route                                |
| ---------- | ------------------------------------------------------------- |
| `net/http` | `mux.Handle("GET /v1/search", m.Charge(price)(handler))`      |
| chi        | `r.With(m.Charge(price)).Get("/v1/search", handler)`          |
| Gin        | `router.GET("/v1/search", mitugin.Charge(m, price), handler)` |
| Echo v5    | `e.GET("/v1/search", handler, mituecho.Charge(m, price))`     |
| Fiber v3   | `app.Get("/v1/search", mitufiber.Charge(m, price), handler)`  |

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

## Next steps

#### [Middleware](/sdks/go/middleware)

See exactly what Charge does with each request and how it behaves when something fails.

#### [Configuration](/sdks/go/configuration)

Learn about every option, how environments work, and how to add logging.

#### [API reference](/sdks/go/api-reference)

Browse every exported function and type.

#### [Errors](/sdks/go/errors)

Understand ErrConfig and the responses the SDK sends for you.