Python SDK API reference
Every public class and method in mitulabs-payments, with what each one does and when.
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
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
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.
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.
Mitu.close and Mitu.close_sync
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.
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.
ReturningNoneserves 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.
Where your view finds the payment depends on your framework.