Skip to navigation

Go SDK configuration

Every field in Options, and how the key, currency, HTTP client and logger shape the SDK.
View as Markdown

You configure the SDK once, when you call mitu.New. The function validates your options straight away and returns an error wrapping ErrConfig if anything is wrong, so a mistake such as a malformed key is caught when your program starts. Creating the client does not contact Mitu, so your program can start even if Mitu happens to be unreachable at that moment. The client is safe to share across goroutines and holds no background resources, so there is nothing to close when your program exits.

m, err := mitu.New(ctx, mitu.Options{
APIKey: os.Getenv("MITU_API_KEY"),
DefaultCurrency: "USD",
Logger: slog.Default(),
})
if err != nil {
log.Fatal(err)
}

Options

FieldTypeDescription
APIKeystringRequired. Your Mitu secret key, either a test key for the sandbox or a live key for production. It is only ever sent to the Mitu API.
DefaultCurrencystringOptional, defaults to USD. The currency used for any Price that does not name its own, such as USD or NGN.
HTTPClient*http.ClientOptional, defaults to a new client. The client used to call Mitu. The SDK never follows redirects, and every call has a time limit.
Logger*slog.LoggerOptional, discards all logs by default. The logger that receives the SDK’s messages.

Sandbox and production

Your key decides which Mitu environment the SDK connects to, so there is no separate environment field.

KeyEnvironment
Test keySandbox
Live keyProduction

Use a sandbox key while you develop and test, and switch to a live key when you are ready to accept real payments. Any other value causes mitu.New to return an error wrapping ErrConfig.

Default currency

Most APIs price everything in one currency. Setting DefaultCurrency means you only have to give the amount for each route. A route can still name a different currency when it needs to.

m, _ := mitu.New(ctx, mitu.Options{APIKey: key, DefaultCurrency: "NGN"})
m.Charge(mitu.Price{Amount: "2500"}) // charges 2,500 NGN
m.Charge(mitu.Price{Amount: "0.05", Currency: "USD"}) // charges 0.05 USD instead

The currency must be enabled on your Mitu account. When a route is priced in a currency other than the one the caller pays in, Mitu converts the amount at a rate it locks when the payment is created.

HTTP client

By default the SDK creates its own HTTP client. You can pass your own instead, which is useful when you want to add tracing to outgoing calls or send them through a particular transport.

m, err := mitu.New(ctx, mitu.Options{
APIKey: key,
HTTPClient: &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)},
})

Logging

The SDK only logs through the *slog.Logger you provide. If you do not set one, its messages are discarded. The SDK takes care never to log anything sensitive, so its messages never include your key, a payment credential, a receipt or a request body.