> ## Documentation Index
> Fetch the complete documentation index at: https://alibi.itsdote.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> FastAPI middleware + Postgres/Redis plugins — the reference Alibi SDK.

Python is the reference implementation. Install from the monorepo (not published to PyPI yet):

```bash theme={null}
pip install -e "python/alibi_sdk[plugins]"
```

## Modes

| Mode | Behavior |
| - | - |
| `off` (default) | No-op. Use in production. |
| `record` | Sandbox requests only: observe plugins, upload step when session id is set |
| `verify` | Sandbox requests only: restore baseline once per run id, diff vs case steps, report |

Per-request override (when sandbox header present): `x-alibi-mode: record|verify|off`.

## Fail-closed sandbox marker

* Default header: `x-alibi-sandbox` (override via `init` / `ALIBI_SANDBOX_HEADER`)
* **Fail-closed (default):** record/verify without the header → **403**
* Escape hatches: `ALIBI_FAIL_CLOSED=0` or `ALIBI_RECORD_ALL=1` (record only)

## FastAPI

```python theme={null}
from alibi_sdk import alibi, create_postgres_plugin, create_redis_plugin
from alibi_sdk import create_kafka_plugin, create_s3_plugin, create_queue_plugin

sql = create_postgres_plugin(connection_string=DATABASE_URL, schema="alibi_sandbox")
redis = create_redis_plugin(url=REDIS_URL, prefix="alibi:", db=1)
kafka = create_kafka_plugin(topic_prefix="alibi.")
s3 = create_s3_plugin(bucket="alibi", prefix="alibi/")
queue = create_queue_plugin(queue_name="orders", prefix="alibi:")

alibi.init(project="my-service", plugins=[sql, redis, kafka, s3, queue])
app.middleware("http")(alibi.middleware)

# In handlers — use wrappers so observe() sees mutations/reads:
await sql.execute("INSERT INTO orders ...", ...)
await redis.set(f"order:{id}", {...})
await kafka.produce("orders", {"id": id})
await s3.put(f"receipts/{id}.json", {"total": 9})
await queue.enqueue({"sku": "latte"})

# Or drop listeners on an existing SQLAlchemy engine (commit-aware):
# from alibi_sdk import create_sqlalchemy_plugin
# alibi.init(..., plugins=[create_sqlalchemy_plugin(engine)])
# Pending mutations flush on outermost commit; rollback discards them.
```

Reference app: `apps/demo-coffee-py` (Compose on :4200).

## Environment

| Variable | Purpose |
| - | - |
| `ALIBI_MODE` | `off` \| `record` \| `verify` |
| `ALIBI_CONTROL_PLANE_URL` | Control plane base (with or without `/api`) |
| `ALIBI_SESSION_ID` | Record session (also `x-alibi-session-id`) |
| `ALIBI_RUN_ID` / `ALIBI_CASE_ID` | Verify run (also headers) |
| `ALIBI_SANDBOX_HEADER` | Sandbox marker header name |
| `ALIBI_FAIL_CLOSED` | `1` (default) require sandbox header |
| `ALIBI_REDACT` | `1` (default) redact secrets/PII in recorded steps |

## Control plane calls the SDK uses

```
POST /api/projects/:id/sessions
POST /api/sessions/:id/steps
POST /api/sessions/:id/stop
POST /api/sessions/:id/save-case
POST /api/projects/:id/baselines
POST /api/projects/:id/runs
POST /api/runs/:id/steps
POST /api/runs/:id/finish
POST /api/projects/:id/verify-live
```

Full HTTP surface: [API overview](https://alibi.itsdote.tech/api/overview.md) (OpenAPI from control-plane TypeScript routes).\
Full Python API: [SDK Reference](https://alibi.itsdote.tech/sdk/python.md) (generated from `python/alibi_sdk`).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.