LogDuck Documentation

Python SDK

Markdown: this page · all of the docs · index

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

terminal
pip install logduck

Minimal example

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

Pythonapp.py
import osfrom logduck import LogDuckClientlogduck = 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.

Pythonapp.py
import osfrom datetime import datetime, timezonefrom logduck import LogDuckClientlogduck = 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 identifierssession_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.

Pythonhandler.py
import osfrom logduck import AsyncLogDuckClientasync 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