> 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/examples/ai-qa/python.md).

# Python

```python
import asyncio
import os
from dnotifier import DNotifier

USER_ID = "user-123"

async def main():
    notifier = DNotifier(
        app_id=os.environ["DNOTIFIER_APP_ID"],
        secret=os.environ["DNOTIFIER_SECRET"],
        transport="http",
        user_id=USER_ID,
        logs=True,
    )

    await notifier.connect()

    limits = notifier.get_plan_limits()
    if not limits or not limits.ai_enabled:
        raise RuntimeError("AI is not enabled on this plan")

    response = await notifier.send_ai(
        sender_id=USER_ID,
        message={"text": "What are three benefits of realtime messaging?"},
    )

    print(f"AI response: {response}")

    session_id = None
    if isinstance(response, dict):
        meta = response.get("metadata")
        if isinstance(meta, dict):
            packet = meta.get("packet")
            session_id = (packet.get("id") if isinstance(packet, dict) else None) or meta.get("id")

    if session_id:
        follow_up = await notifier.send_ai(
            sender_id=USER_ID,
            session_id=str(session_id),
            message={"text": "Give me one concrete example for each."},
        )
        print(f"Follow-up: {follow_up}")

    history = await notifier.fetch_ai_history(sender_id=USER_ID)
    print(f"History: {history}")

asyncio.run(main())
```

## Run

```bash
export DNOTIFIER_APP_ID=...
export DNOTIFIER_SECRET=...
python ai_qa.py
```

## Web integration

Call `send_ai` from a route handler and return `response["data"]["content"]` (shape may vary by plan). Use async frameworks (FastAPI, aiohttp) for non-blocking requests.
