> 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/getting-started/first-connection/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

This guide connects a DNotifier client over **WebSocket** from Python. You need [**credentials**](https://dnotifier.gitbook.io/product-docs/getting-started/credentials.md) and the [**Python SDK installed**](https://dnotifier.gitbook.io/product-docs/getting-started/installation/python.md).

## Complete example

```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-alice",
        on_connected=lambda: print("Connected — ready to send messages"),
        on_message=lambda msg: print(
            f"Incoming from: {msg.metadata.sender}\nPayload: {msg.payload.to_json()}"
        ),
        on_disconnected=lambda code=None, reason=None: print(
            f"Disconnected: code={code} reason={reason}"
        ),
    )

    try:
        await notifier.connect()
        print(f"is_connected: {notifier.is_connected}")
        print(f"message_size_limit: {notifier.message_size_limit}")
        limits = notifier.get_plan_limits()
        print(f"Plan limits: {limits}")
    except Exception as e:
        print(f"Connect failed: {e}")

asyncio.run(main())
```

Replace `'your-app-id'` and `'your-app-secret'` with values from the dashboard, or load them from environment variables — see [**Credentials & environment**](https://dnotifier.gitbook.io/product-docs/getting-started/credentials.md).

Run:

```bash
python connect.py
```

Expected console output:

```
Connected — ready to send messages
is_connected: True
message_size_limit: 32768
Plan limits: DNotifierPlanLimits(...)
```

## FastAPI lifespan example

```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=os.environ["DNOTIFIER_APP_ID"],
        secret=os.environ["DNOTIFIER_SECRET"],
        transport="ws",
        user_id="user-alice",
        on_connected=lambda: print("Connected"),
        on_message=lambda msg: print(msg.payload.to_json()),
    )
    await notifier.connect()
    yield
    await notifier.disconnect()

app = FastAPI(lifespan=lifespan)
```

## HTTP transport example

For AI/RAG/workflows without a persistent socket:

```python
notifier = DNotifier(
    app_id="your-app-id",
    secret="your-app-secret",
    transport="http",
    user_id="user-alice",
    on_connected=lambda: print("Authenticated via HTTP"),
)

await notifier.connect()
# Ready for send_ai(), search(), run_workflow(), etc.
```

## Common errors

| Error                                                             | Likely cause                                |
| ----------------------------------------------------------------- | ------------------------------------------- |
| `ValueError: app_id, secret, transport, and user_id are required` | Empty constructor argument                  |
| `RuntimeError: Authentication failed`                             | Wrong secret or app\_id                     |
| `RuntimeError: Not connected`                                     | Called `send()` before `connect()` finished |
| Connection errors                                                 | Network or certificate issues               |

## What happens under the hood

1. SDK sends credentials to the DNotifier auth endpoint (`api.dnotifier.com`)
2. Auth returns `token` and plan `limits`
3. WebSocket connects to the DNotifier realtime endpoint
4. Client sends handshake with token
5. Server acknowledges → `on_connected` callback runs

## Next step

Send a message to another user:

→ [**Your first message — Python**](https://dnotifier.gitbook.io/product-docs/getting-started/first-message/python.md)
