Metadata-Version: 2.5
Name: maskflare
Version: 0.1.0
Summary: Official Python SDK for Maskflare - mask sensitive data before it reaches an AI model, unmask it when you get a result back.
Project-URL: Homepage, https://github.com/maskflare/maskflare-sdk-python#readme
Project-URL: Repository, https://github.com/maskflare/maskflare-sdk-python
Project-URL: Issues, https://github.com/maskflare/maskflare-sdk-python/issues
Author: Maskflare
License-Expression: MIT
License-File: LICENSE
Keywords: ai,data masking,llm,maskflare,pii,redaction,tokenization
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx<1,>=0.27
Description-Content-Type: text/markdown

# maskflare

Official Python SDK for [Maskflare](https://maskflare.com) - mask sensitive data before it
reaches an AI model, unmask it when you get a result back.

Built on [httpx](https://www.python-httpx.org/), its only runtime dependency.

## Install

```bash
pip install maskflare
```

## Quickstart

Generate a **Redaction**-purpose API key from your Maskflare dashboard (Configure → API keys →
Create key → Purpose: Redaction) and you're ready to go:

```python
import os
from maskflare import MaskFlare

client = MaskFlare(
    api_key=os.environ["MASKFLARE_API_KEY"],
    # base_url defaults to https://api.maskflare.com - point it at your own deployment
    # (e.g. http://localhost:8000 for local development) until that's live.
)

result = client.mask("Contact ada@example.com for details")
# result.output: "Contact mf_mask_170b8521c1b944b3beb9ca02b328efbe for details"
# result.tokens: [MaskedToken(token="170b8521c1b944b3beb9ca02b328efbe", rule_key="email")]

# ... send result.output to your LLM, store it, log it - the real value never leaves your key's reach ...

original = client.unmask([t.token for t in result.tokens])
# original: {"170b8521c1b944b3beb9ca02b328efbe": "ada@example.com"}
```

`MaskFlare` also works as a context manager, which releases its connection pool on exit:

```python
with MaskFlare(api_key=os.environ["MASKFLARE_API_KEY"]) as client:
    result = client.mask("Contact ada@example.com for details")
```

A **Redaction** key can only mask/unmask - it's rejected if used against Maskflare's AI-gateway
forwarding routes, so a leaked SDK key can never be used to spend your AI-provider budget. Masking
rules (what counts as sensitive, and whether request/response text is checked) are configured per
environment on the dashboard's Rules page - the SDK just applies whatever your key's environment
has configured.

Tokens resolve back to their original value for 30 days from creation, then expire.

## API

### `MaskFlare(api_key, *, base_url=..., timeout=..., max_retries=..., http_client=...)`

| Parameter     | Type            | Default                     | Description                                                             |
| ------------- | --------------- | ---------------------------- | ------------------------------------------------------------------------ |
| `api_key`     | `str`           | _required_                   | A Redaction-purpose API key.                                             |
| `base_url`    | `str`           | `https://api.maskflare.com`  | Override for a self-hosted deployment or local development.              |
| `timeout`     | `float`         | `10.0`                       | Per-request timeout, in seconds.                                        |
| `max_retries` | `int`           | `2`                           | Automatic retries on `429`/`5xx`, honoring `Retry-After`. `0` disables.  |
| `http_client` | `httpx.Client`  | a new, internally-owned one  | Bring your own `httpx.Client` (e.g. to share a connection pool or add custom transport/proxy settings). When supplied, `MaskFlare` never closes it for you. |

### `client.mask(input: str) -> MaskResult`

```python
@dataclass(frozen=True)
class MaskResult:
    output: str  # input, with each match replaced by an opaque token
    tokens: tuple[MaskedToken, ...]


@dataclass(frozen=True)
class MaskedToken:
    token: str
    rule_key: str
```

### `client.unmask(tokens: Sequence[str]) -> dict[str, str]`

Returns a map of token → original value, for every token that was found and belonged to the
calling key. A token you don't own, or one that's expired, is simply omitted - check
`token in result` for the ones you need.

### Errors

- `MaskFlareError` - the API rejected the request. Has `.status`, `.code`, `.detail`, and (for a
  `429`) `.retry_after_seconds`.
- `MaskFlareConnectionError` - the request never reached the API (network failure or timeout).
- A plain `TypeError` for invalid input caught client-side before any request is made (e.g. an
  empty string to `mask`).

```python
from maskflare import MaskFlare, MaskFlareError

try:
    client.mask(user_input)
except MaskFlareError as error:
    if error.code == "rate_limited":
        ...  # back off - or just let the SDK's built-in retry handle it (see max_retries)
    raise
```

## Development

```bash
uv sync
uv run pytest       # tests
uv run ruff check .  # lint
uv run pyright       # typecheck
```

## License

MIT
