> 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.

# Python SDK API reference

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](/sdks/python/configuration). For the failures you may need to handle, see [Errors](/sdks/python/errors).

## The Mitu client

```python
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

```python
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.

| Argument      | Description                                                                                                                                                                                                    |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `amount`      | The 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. |
| `currency`    | The currency code for this price. If you leave it out, the client's default currency is used.                                                                                                                  |
| `description` | A 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.

```python
@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

```python
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](/sdks/python/configuration#shutting-down) 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.

```python
# 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`.

```python
@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
```

| Field            | Description                                                                                                                                            |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `payment_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`      | A unique reference the SDK gave the payment when it was created.                                                                                       |
| `tx_hash`        | The hash of the settlement transaction, when the payment method settles onchain.                                                                       |
| `amount_paid`    | The amount the caller paid, as a `Decimal`.                                                                                                            |
| `currency`       | The currency of `amount_paid`.                                                                                                                         |
| `network`        | The network the payment settled on.                                                                                                                    |
| `payment_method` | The payment method the caller used, as Mitu names it.                                                                                                  |

Where your view finds the payment depends on your framework.

| Framework | Where to read the payment    |
| --------- | ---------------------------- |
| FastAPI   | `request.state.mitu_payment` |
| Flask     | `g.mitu_payment`             |
| Django    | `request.mitu_payment`       |