# Python SDK

> One page of the LogDuck documentation. Every page is listed in https://logduck.com/llms.txt, and https://logduck.com/llms-full.txt is all of them in one file.

The official Python client. Supports Python 3.9+, ships type hints (py.typed), and offers both a synchronous and an asyncio client so a web handler never blocks just to record that something happened.

## Installation

```bash
pip install logduck
```

## Minimal example

The fewest settings that work — an API key and a source.

```python
import os

from logduck import LogDuckClient

logduck = LogDuckClient(
    api_key=os.environ["LOGDUCK_API_KEY"],
    source="checkout-api",
)

logduck.send("order.placed")
```

## Complete example

Every option, with its default. Only the first two are required; the rest are shown set to the value you get by omitting them.

```python
import os
from datetime import datetime, timezone

from logduck import LogDuckClient

logduck = LogDuckClient(
    api_key=os.environ["LOGDUCK_API_KEY"],  # required - your key, starts with `ld_`
    source="checkout-api",                  # required - which app sent this, max 200 chars
    throw_on_error=False,                   # optional, default False - True raises instead of returning None
    retry_enabled=True,                     # optional, default True  - retry once on 5xx, network errors and 429
    max_retry_delay=10.0,                   # optional, default 10.0  - never block your caller longer than this
    timeout=30.0,                           # optional, default 30.0  - per-request timeout, seconds
    base_url="https://api.logduck.com",     # optional - override only for a self-hosted deployment
)

order_id = "order_9127"                     # your own identifiers
session_id = "sess_8f21c"

response = logduck.send(
    "order.placed",                             # required - max 100 chars, naming rule below
    subject=order_id,                           # optional - what the event is about, max 500 chars
    session_id=session_id,                      # optional - groups related events, max 256 chars
    message="Order placed on the Pro plan",     # optional - the push notification body, max 500
    time=datetime.now(timezone.utc),            # optional - defaults to when the server received it
    data={"total": 4999, "currency": "NOK"},    # optional - any JSON-serialisable mapping
    emoji="🛒",                                 # optional - shown next to the event, max 10 chars
)

# None when the send failed and throw_on_error is False (the default).
print(response.event_id if response else None)
```

## Async

Every option above applies to `AsyncLogDuckClient` too. Used as a context manager it releases the connection pool on exit.

```python
import os

from logduck import AsyncLogDuckClient

async with AsyncLogDuckClient(
    api_key=os.environ["LOGDUCK_API_KEY"],
    source="checkout-api",
) as logduck:
    await logduck.send("order.placed")
```

Full reference and changelog: [github.com/Log-Duck/logduck-python](https://github.com/Log-Duck/logduck-python)
