Skip to navigation

Python SDK configuration

Every argument the Mitu client accepts, and how the key, currency, logging and shutdown work.
View as Markdown

You configure the SDK once, when you create the Mitu client. The constructor checks your arguments straight away, so a mistake such as a malformed key or an unknown currency raises a MituConfigError when your application starts, rather than on the first paying request.

import os
from mitulabs_payments import Mitu
mitu = Mitu(os.environ["MITU_API_KEY"], default_currency="USD")

Arguments

ArgumentTypeDescription
api_keystrRequired, passed as the first positional argument. Your Mitu secret key, either a test key or a live key. It is only ever sent to the Mitu API.
default_currencystrOptional, passed by keyword and defaults to USD. The currency used for any price that does not name its own.

Sandbox and production

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

KeyEnvironmentMitu API address
Test keySandboxhttps://sandbox.mitu.ai
Live keyProductionhttps://api.mitu.ai

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 raises a MituConfigError. You can check which environment a client is using by reading mitu.environment, which is either sandbox or production.

Default currency

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

mitu = Mitu(os.environ["MITU_API_KEY"], default_currency="NGN")
mitu.charge(amount="2500") # charges 2,500 NGN
mitu.charge(amount="0.05", currency="USD") # charges 0.05 USD instead

A currency is written as a short code such as USD or NGN, and it 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.

Logging

The SDK writes its log messages through Python’s standard logging module, under a logger named mitulabs_payments. Nothing is shown until you turn it on, which you can do in the same way as for any other library.

import logging
logging.getLogger("mitulabs_payments").setLevel(logging.INFO)

The SDK takes care never to log anything sensitive. Its messages never include your API key, a payment credential or the body of a request.

Shutting down

The client keeps a pool of HTTP connections open to Mitu so that each request is fast. When your application stops, close the client to release those connections. Use close() in async applications and close_sync() in sync ones.

In FastAPI, close the client in the application’s lifespan handler.

from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
await mitu.close()
app = FastAPI(lifespan=lifespan)