Skip to navigation

Python SDK API reference

Every public class and method in mitulabs-payments, with what each one does and when.

View as Markdown

The package exports a small set of names. The Mitu class is the client you create and use on your routes. The Payment class describes a settled payment. The three exception classes, MituError, MituConfigError and MituApiError, cover the problems your code may need to handle, and __version__ holds the installed version.

For the arguments you can pass when creating a client, see Configuration. For the failures you may need to handle, see Errors.

The Mitu client

class Mitu:
def __init__(self, api_key: str, *, default_currency: str = "USD") -> None: ...
environment: Literal["sandbox", "production"]
default_currency: str

Creates a client from your secret key. The constructor validates the key and the default currency immediately and raises a MituConfigError if either is invalid. Create a single client when your application starts and use it on every route that charges.

The environment attribute tells you whether the client is connected to the sandbox or to production, which is decided by your key. The default_currency attribute holds the currency used for prices that do not name their own.

Mitu.charge

def charge(
self,
amount: str | Callable[[Request], str | None],
*,
currency: str | None = None,
description: str | None = None,
) -> Callable[[View], View]

Returns a decorator that puts a view behind a payment. The view only runs once Mitu has settled a payment for the request at the price you set. Place the decorator directly below your framework’s route decorator. It works on both sync and async views.

ArgumentDescription
amountThe price as a decimal string, such as "0.05" or "2500". It must be greater than zero and can have at most two decimal places. You can also pass a function that receives the request and returns a price.
currencyThe currency code for this price. If you leave it out, the client’s default currency is used.
descriptionA short label shown to the caller in the payment challenge.

When you pass a fixed amount, the decorator checks it as soon as it is applied. An invalid amount therefore raises a MituConfigError when your module is imported, long before any traffic arrives.

@app.get("/v1/search")
@mitu.charge(amount="0.05", description="Search")
async def search(request: Request):
return run_search(request.query_params, request.state.mitu_payment)

Mitu.close and Mitu.close_sync

async def close(self) -> None
def close_sync(self) -> None

Close the client’s HTTP connections to Mitu. Call close() when your application shuts down if it is async, and close_sync() if it is sync. The Configuration page shows how to wire this into FastAPI, Flask and Django.

Setting prices

A price can be a fixed string or a function of the request. The examples below cover the common cases.

# A fixed price in the default currency
mitu.charge(amount="0.05")
# A fixed price in a named currency, with a description
mitu.charge(amount="2500", currency="NGN", description="Search")
# A price that depends on the caller's plan
mitu.charge(amount=lambda request: "1.00" if is_premium(request) else "0.10")
# A route that is free for some callers
mitu.charge(amount=lambda request: None if is_free(request) else "0.05")

When you use a function, keep these rules in mind.

  • It must return the same price twice.
    The SDK calls the function on the first, unpaid request and again when the caller retries with payment. If the two answers differ, the payment will not match and the caller will be asked to pay again.
  • It can make a request free.
    Returning None serves that request without creating a payment.
  • It can be async on async views.
    If your view is async, the price function may be async too.
  • It must never trust the request body.
    Base the price on information you control, such as the caller’s plan or a stored order total.

Payment

When a request has been paid, your view can read a Payment describing it. For a free request the payment is None.

@dataclass(frozen=True, slots=True, kw_only=True)
class Payment:
payment_id: str
reference: str
tx_hash: str | None
amount_paid: Decimal | None
currency: str | None
network: str | None
payment_method: str | None
FieldDescription
payment_idMitu’s identifier for the payment. It matches the identifier in the payment.completed webhook, so you can link the two.
referenceA unique reference the SDK gave the payment when it was created.
tx_hashThe hash of the settlement transaction, when the payment method settles onchain.
amount_paidThe amount the caller paid, as a Decimal.
currencyThe currency of amount_paid.
networkThe network the payment settled on.
payment_methodThe payment method the caller used, as Mitu names it.

Where your view finds the payment depends on your framework.

FrameworkWhere to read the payment
FastAPIrequest.state.mitu_payment
Flaskg.mitu_payment
Djangorequest.mitu_payment