> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/platform-guides/python.md).

# Python

Use the DNotifier Python SDK in **asyncio** applications: FastAPI backends, background workers, CLI tools, and data pipelines. The client is **async-first** — call `await notifier.connect()` and other methods from `async def` functions.

## Requirements

| Requirement           | Version / notes                                                           |
| --------------------- | ------------------------------------------------------------------------- |
| Python                | 3.9 or later                                                              |
| pip / poetry / uv     | Any recent version                                                        |
| DNotifier credentials | `app_id` and `secret` from [app.dnotifier.com](https://app.dnotifier.com) |

→ [Install — Python](/product-docs/getting-started/installation/python.md)

***

## Install

```bash
pip install dnotifier
```

Dependencies **httpx** and **websockets** are installed automatically.

***

## Basic pattern

```python
import asyncio
from dnotifier import DNotifier

async def main():
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="ws",
        user_id="user-123",
        on_connected=lambda: print("Connected"),
        on_message=lambda msg: print(msg.payload.to_json()),
        on_disconnected=lambda **kw: print("Disconnected", kw),
    )
    await notifier.connect()
    await notifier.send(
        sender_id="user-123",
        receiver_id="user-456",
        data={"type": "text", "text": "Hello"},
    )
    await notifier.disconnect()

asyncio.run(main())
```

***

## Transport choice

| Use case                             | Transport |
| ------------------------------------ | --------- |
| Live chat, push-style updates        | `"ws"`    |
| AI prompts, RAG, workflows, sync RPC | `"http"`  |

See [Choose your transport](/product-docs/getting-started/choose-transport.md) and the [transport matrix](/product-docs/reference/transport-matrix.md).

***

## FastAPI example

Run DNotifier in a lifespan handler or background task:

```python
from contextlib import asynccontextmanager
from fastapi import FastAPI
from dnotifier import DNotifier

notifier: DNotifier | None = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    global notifier
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="http",
        user_id="service-bot",
        on_connected=lambda: None,
        on_message=lambda _: None,
        on_disconnected=lambda **_: None,
    )
    await notifier.connect()
    yield
    await notifier.disconnect()

app = FastAPI(lifespan=lifespan)

@app.post("/ask")
async def ask(prompt: str):
    return await notifier.send_ai(sender_id="service-bot", message=prompt)
```

***

## Environment variables

```python
import os
from dnotifier import DNotifier

notifier = DNotifier(
    app_id=os.environ["DNOTIFIER_APP_ID"],
    secret=os.environ["DNOTIFIER_SECRET"],
    transport="ws",
    user_id="user-123",
    on_connected=lambda: None,
    on_message=lambda _: None,
    on_disconnected=lambda **_: None,
)
```

→ [Credentials & environment](/product-docs/getting-started/credentials.md)

***

## Next steps

* [Your first connection — Python](/product-docs/getting-started/first-connection/python.md)
* [Your first message — Python](/product-docs/getting-started/first-message/python.md)
* [DNotifier class reference — Python](/product-docs/reference/dnotifier-class/python.md)
