> 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 API reference

Import the core package under the name `mitu`.

```go
import mitu "github.com/mitulabs/payments/go"
```

The core package exports a client type, a function to create it, the types that describe prices and payments, a helper for reading the payment in a handler, and one error value. The Gin, Echo and Fiber adapters each live in their own module and export a single function. For the options you can set, see [Configuration](/sdks/go/configuration). For what happens on each request, see [Middleware](/sdks/go/middleware). For failures, see [Errors](/sdks/go/errors).

## New

```go
func New(ctx context.Context, opts Options) (*Mitu, error)
```

Creates a client from your options. The function validates the options immediately and returns an error wrapping `ErrConfig` if any of them is invalid. If the context is already done, it returns `ctx.Err()` instead. It does not make any network calls.

## Mitu

```go
type Mitu struct {
	// contains unexported fields
}
```

The client that charges for your routes. Always create it with `New`. A single client is safe to use from many goroutines at once and holds no background resources, so you can create it once and share it across your whole program.

### Mitu.Charge

```go
func (m *Mitu) Charge(p Price) func(http.Handler) http.Handler
```

Returns standard `net/http` middleware that serves a request only after Mitu has settled a payment for it at price `p`. The middleware also works with chi and any other router that accepts this middleware shape.

`Charge` checks the price when you call it. If the price is invalid, for example because the amount has three decimal places, it panics with an error wrapping `ErrConfig`. Because routes are usually registered when the program starts, this means a bad price is caught before any traffic arrives.

```go
mux.Handle("GET /v1/search", m.Charge(mitu.Price{Amount: "0.05"})(http.HandlerFunc(search)))
```

## Options

```go
type Options struct {
	APIKey          string
	DefaultCurrency string
	HTTPClient      *http.Client
	Logger          *slog.Logger
}
```

The settings you pass to `New`. Each field is described on the [Configuration](/sdks/go/configuration#options) page.

## Price

```go
type Price struct {
	Amount      string
	AmountFunc  func(r *http.Request) (string, error)
	Currency    string
	Description string
}
```

Describes how much a route costs. Set exactly one of `Amount` or `AmountFunc`.

| Field         | Description                                                                                                                                                                             |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Amount`      | A fixed price as a decimal string, such as `"5000"` or `"12.50"`. It must be greater than zero and can have at most two decimal places.                                                 |
| `AmountFunc`  | A function that works out the price for each request. Returning an empty string serves that request free. Returning an error, or an invalid amount, causes the route to reply with 500. |
| `Currency`    | The currency code for this price. If you leave it empty, `Options.DefaultCurrency` is used.                                                                                             |
| `Description` | A short label shown to the caller in the payment challenge.                                                                                                                             |

The examples below cover the common cases.

```go
// A fixed price in the default currency
mitu.Price{Amount: "0.05"}

// A fixed price in a named currency, with a description
mitu.Price{Amount: "2500", Currency: "NGN", Description: "Search"}

// A price that depends on the caller's plan
mitu.Price{AmountFunc: func(r *http.Request) (string, error) {
	if isPremium(r) {
		return "1.00", nil
	}
	return "0.10", nil
}}
```

When you use `AmountFunc`, it must return the same amount on the first, unpaid request and on the paid retry, otherwise the payment will not match and the caller will be asked to pay again. Base the price on information you control, such as a stored order total, and never read it from the request body.

## Payment

```go
type Payment struct {
	ID            string
	Reference     string
	TxHash        string
	AmountPaid    string
	Currency      string
	Network       string
	PaymentMethod string
}
```

Describes a payment that Mitu has settled for a request.

| Field           | Description                                                                                                                                            |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `ID`            | Mitu's identifier for the payment. It matches the identifier in the [`payment.completed` webhook](/platform/webhooks/events), so you can link the two. |
| `Reference`     | The unique reference the payment was created with.                                                                                                     |
| `TxHash`        | The hash of the settlement transaction, when the payment method settles onchain.                                                                       |
| `AmountPaid`    | The amount the caller paid, as a decimal string in `Currency`.                                                                                         |
| `Currency`      | The currency of `AmountPaid`.                                                                                                                          |
| `Network`       | The network the payment settled on.                                                                                                                    |
| `PaymentMethod` | The payment method the caller used, as Mitu names it.                                                                                                  |

## PaymentFromContext

```go
func PaymentFromContext(ctx context.Context) (Payment, bool)
```

Returns the payment for a request that was served through `Charge`. The second value is `false` when the request was served free, in which case there is no payment to read.

## ErrConfig

```go
var ErrConfig = errors.New("mitu: invalid configuration")
```

The error that every configuration problem wraps. `New` returns it for invalid options, and `Charge` panics with it for an invalid price. Use `errors.Is` to check for it.

## Framework adapters

Each adapter is a separate module under `github.com/mitulabs/payments/go` with a single function, `Charge(m *mitu.Mitu, p mitu.Price)`, which returns the middleware type of its framework. It behaves exactly like the core middleware, and your handler reads the payment with `mitu.PaymentFromContext` using the request's context.

| Framework | Package     | Read the payment from   |
| --------- | ----------- | ----------------------- |
| Gin       | `mitugin`   | `c.Request.Context()`   |
| Echo      | `mituecho`  | `c.Request().Context()` |
| Fiber     | `mitufiber` | `c.Context()`           |

The example below shows the Gin adapter.

```go
import mitugin "github.com/mitulabs/payments/go/gin"

router.GET("/v1/search", mitugin.Charge(m, mitu.Price{Amount: "0.05"}), search)
```