> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlurk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> getlurk: a typed client for Python 3.11 and newer, with an asyncio twin. No dependencies.

```bash theme={null}
pip install getlurk
```

It needs Python 3.11 or newer, has no dependencies, and ships type hints (`py.typed`). The package is `getlurk` and so is the import.

## Send an event

```python theme={null}
from getlurk import Lurk

lurk = Lurk()  # reads the key from LURK_KEY

receipt = lurk.events.send(
    {
        "event": "checkin.scanned",
        "user": "maya@example.com",
        "properties": {"venue": "mission-st"},
        "idempotency_key": "chk-mission-st-maya-0812",
    }
)

print(receipt["receipts"])
```

Every operation in the API is a method named after it: `lurk.rules.list()`, `lurk.units.get(id="beans")`, `lurk.users.get(id="maya@example.com")`. Input is a dict or keywords: `lurk.events.send(event="checkin.scanned", user="maya@example.com")`. The types of inputs and answers are in `getlurk.types`, and your editor completes them.

## Configure

```python theme={null}
lurk = Lurk(
    api_key=None,  # default: the LURK_KEY environment variable
    base_url="https://api.getlurk.com",  # default
    max_retries=2,  # default
    timeout=30.0,  # seconds; default
)
```

Per call, pass `options=RequestOptions(...)`:

```python theme={null}
from getlurk import RequestOptions

lurk.events.send(
    {"event": "order.paid", "user": "maya@example.com"},
    options=RequestOptions(idempotency_key="order-8841", timeout=5.0, headers={"traceparent": traceparent}),
)
```

## Errors

```python theme={null}
from getlurk import Lurk, LurkConnectionError, LurkError

lurk = Lurk()
try:
    lurk.units.use(user="maya@example.com", unit="coupon", reference="scan-1")
except LurkError as error:
    print(error.code, error.param, error.request_id)  # `code` is the part to branch on
    print(error.next_step)  # one sentence on what to do
    if error.is_retryable:
        pass  # sending the same request again can work; the SDK already tried twice
except LurkConnectionError:
    pass  # no answer at all: the network, or a timeout
```

A `LurkError` has `status`, `code`, `param`, `request_id`, `doc_url`, `next_step` and, for `rate_limited`, `retry_after_seconds`. Be ready for a `code` you have not seen: Lurk adds codes, fields and event types over time and never removes one inside a version ([versioning](https://docs.getlurk.com/guides/versioning.md)).

## Pages

```python theme={null}
for rule in lurk.paginate("rules.list", {"status": "live"}):
    print(rule["key"])
```

[More](https://docs.getlurk.com/guides/pagination.md).

## asyncio

`AsyncLurk` takes the same arguments and has the same methods, awaited.

```python theme={null}
import asyncio

from getlurk import AsyncLurk


async def main() -> None:
    lurk = AsyncLurk()
    receipt = await lurk.events.send(event="checkin.scanned", user="maya@example.com")
    print(receipt["receipts"])
    async for rule in lurk.paginate("rules.list"):
        print(rule["key"])


asyncio.run(main())
```

Calls run on a worker thread, so they never block your event loop. Retries, idempotency keys and errors are the same as in the synchronous client.

## Webhooks

```python theme={null}
from getlurk import WebhookVerificationError, construct_event

try:
    event = construct_event(raw_body, request_headers, webhook_secret)
except WebhookVerificationError:
    ...  # answer 400 and ignore the body
```

Pass the raw body (bytes or text), not parsed JSON. [The whole page](https://docs.getlurk.com/webhooks.md).

## Use another HTTP library

`Lurk(transport=...)` replaces the HTTP call: a proxy, `requests`, `httpx`, or a test double. [Testing your code](https://docs.getlurk.com/sdks/testing.md) has an example.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.