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

# Python

Package: `dnotifier` ([PyPI](https://pypi.org/project/dnotifier/))

## Constructor

```python
from dnotifier import DNotifier

notifier = DNotifier(
    app_id="your-app-id",
    secret="your-app-secret",
    transport="ws",  # or "http"
    user_id="user-123",
    logs=False,
    # url="wss://YOUR_HOST",  # optional — only if provided by DNotifier
    on_connected=lambda: None,
    on_message=lambda msg: None,
    on_disconnected=lambda code=None, reason=None: None,
)
```

## Option reference

| Parameter                | Type                                 | Required | Default                      | Description                                            |
| ------------------------ | ------------------------------------ | -------- | ---------------------------- | ------------------------------------------------------ |
| `app_id`                 | `str`                                | ✅        | —                            | DNotifier application id                               |
| `secret`                 | `str`                                | ✅        | —                            | Application secret                                     |
| `transport`              | `str`                                | ✅        | —                            | `"ws"` or `"http"`                                     |
| `user_id`                | `str`                                | ✅        | —                            | User or agent id                                       |
| `logs`                   | `bool`                               | —        | `False`                      | Enable AI session logging                              |
| `url`                    | `str`                                | —        | production WSS (SDK default) | Custom WebSocket URL only if provided by DNotifier     |
| `on_connected`           | `Callable[[], None]`                 | —        | no-op                        | Ready callback                                         |
| `on_message`             | `Callable[[DNotifierMessage], None]` | —        | no-op                        | Incoming message callback                              |
| `on_disconnected`        | `Callable[..., None]`                | —        | no-op                        | Disconnect callback (`code`, `reason` kwargs)          |
| `permission_enforcement` | `str`                                | —        | `"backend"`                  | `"backend"` or `"disabled"` for tool permission checks |
| `on_agent_event`         | `Callable[[dict], None]`             | —        | —                            | Local agent/tool/MCP event callback                    |

Raises `ValueError` if `app_id`, `secret`, `transport`, or `user_id` is empty, or if `transport` is not `"ws"` or `"http"`.

Agent tools and MCP: see [**Tools & MCP agents**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/python.md).

## Environment variables example

```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-alice",
    on_connected=lambda: print("Connected"),
    on_message=lambda msg: print(msg.payload.to_json()),
    on_disconnected=lambda code=None, reason=None: print(f"Disconnected: {reason}"),
)

await notifier.connect()
```

## HTTP transport example

```python
notifier = DNotifier(
    app_id=app_id,
    secret=secret,
    transport="http",
    user_id="service-bot",
    logs=True,
    on_connected=lambda: None,
)

await notifier.connect()
```

## Instance fields (after `connect()`)

| Field                | Type          | Description                           |
| -------------------- | ------------- | ------------------------------------- |
| `app_id`             | `str`         | Constructor value                     |
| `secret`             | `str`         | Constructor value                     |
| `user_id`            | `str`         | Constructor value                     |
| `url`                | `str`         | WebSocket URL                         |
| `is_connected`       | `bool`        | Connection state                      |
| `ai_enabled`         | `bool`        | Plan AI flag                          |
| `auth_token`         | `str \| None` | Auth token (nullable until connected) |
| `message_size_limit` | `int`         | Chunk size limit in bytes             |

## `disconnect()`

```python
await notifier.disconnect()  # closes WebSocket, sets is_connected = False
```

## Custom WebSocket URL

```python
notifier = DNotifier(
    app_id=app_id,
    secret=secret,
    transport="ws",
    user_id=user_id,
    url="wss://YOUR_HOST",  # only if provided by DNotifier
)
```

Production defaults target **`api.dnotifier.com`**. See [**Endpoints & custom URL**](https://dnotifier.gitbook.io/product-docs/reference/endpoints.md).

## Callback signatures

```python
from dnotifier import DNotifierMessage

def on_connected() -> None: ...
def on_message(message: DNotifierMessage) -> None: ...
def on_disconnected(code=None, reason=None) -> None: ...
```
