Metadata-Version: 2.5
Name: kourio
Version: 0.1.0
Summary: Official Python SDK for the Kourio email API.
Project-URL: Homepage, https://kourio.app
Project-URL: Repository, https://github.com/rssilverioo/kourio
Project-URL: Issues, https://github.com/rssilverioo/kourio/issues
License-Expression: MIT
Keywords: api,deliverability,email,kourio,sdk,smtp,transactional-email,webhooks
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Communications :: Email
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# Kourio Python SDK

Send transactional and marketing email with the official, zero-dependency Kourio client.

```bash
pip install kourio
```

```python
import os

from kourio import Kourio

kourio = Kourio(os.environ['KOURIO_API_KEY'])

result = kourio.emails.send(
    {
        'from': 'hello@mail.example.com',
        'to': ['developer@example.com'],
        'subject': 'Welcome to Kourio',
        'html': '<h1>Welcome!</h1><p>Your account is ready.</p>',
        'text': 'Welcome! Your account is ready.',
        'type': 'TRANSACTIONAL',
    },
    idempotency_key='welcome-user-123',
)

print(result['data']['id'], result['data']['status'])
```

`POST /v1/emails` answers **202**: the message is queued and the response returns before it
leaves. Repeating the call with the same `idempotency_key` returns the original send
instead of creating a second one.

The client uses `https://kourio.app` and a 30-second timeout by default. Pass `base_url`,
`timeout`, or a transport callable as keyword arguments when needed. Requires Python 3.11+
and depends on nothing outside the standard library.

Every resource of the public API is covered — `emails`, `domains`, `api_keys`,
`suppressions`, `webhooks`, `templates`, `campaigns`, `contacts`, `contact_lists`,
`contact_properties`, `segments`, `topics` — with request and response types generated
from the published OpenAPI spec. The package ships `py.typed`; payloads and responses are
`TypedDict`s, so your editor completes every field.

## Errors

A non-2xx response raises `KourioError` carrying the API's structured envelope:

```python
from kourio import Kourio, KourioError

try:
    kourio.emails.send({'to': ['someone@example.com'], 'subject': 'Hi', 'text': 'Hi'})
except KourioError as error:
    # error.request_id is what support will ask for.
    print(error.code, error.status_code, error.request_id, error.details)
```

A request that exceeds the timeout raises `KourioTimeoutError` instead — the SDK never
maps a timeout to an API error, because a timed-out send may still have been accepted.
Retry it with the same `idempotency_key`.

## Webhooks

```python
endpoint = kourio.webhooks.create(
    {
        'name': 'Production',
        'url': 'https://hooks.example.com/kourio',
        'events': ['email.delivered', 'email.bounced', 'email.complained'],
    }
)

# The only response that ever carries it. Store it now — there is no endpoint to read it
# back, and recovering from a lost secret means rolling the endpoint.
print(endpoint['data']['secret'])
```

`list`, `update`, `remove` and `deliveries` complete the CRUD; `test` queues a synthetic
event so you can confirm your receiver before real traffic reaches it.

### Verifying a delivery

Every request carries a `kourio-signature` header of the form `t=<unix seconds>,v1=<hex>`,
an HMAC-SHA256 over `<timestamp>.<raw body>`:

```python
import json

from kourio import KOURIO_SIGNATURE_HEADER, verify_webhook_signature

def handle_webhook(request):
    # The raw bytes, before any parsing: a re-serialized object is not guaranteed to
    # reproduce the bytes that were signed, and any difference rejects a valid request.
    body = request.get_data()  # Flask; use request.body in Django
    signature = request.headers.get(KOURIO_SIGNATURE_HEADER, '')

    if not verify_webhook_signature(body, signature, os.environ['KOURIO_WEBHOOK_SECRET']):
        return 'Invalid signature', 401

    event = json.loads(body)
    # event['id'] is stable across retries — deduplicate on it. Delivery is at-least-once.
    print(event['id'], event['type'], event['data']['emailId'])
    return 'ok', 200
```

Signatures older or newer than five minutes are rejected, which bounds replay of a
captured request. Override the window with `tolerance_seconds` only if your receiver's
clock is known to drift.

## Development (this repository)

Request and response shapes live in `src/kourio/_generated/`, generated from
`docs-publicas/openapi.json` — never edit them by hand:

```bash
python scripts/generate.py          # regenerate after the spec changes
python scripts/generate.py --check  # fails if disk and spec diverge (CI runs this)
python -m unittest discover -s tests
```

The hand-written layer is the HTTP mechanics (`client.py`, `_http.py`), the typed error
(`errors.py`) and the webhook signature verification (`webhook_signature.py`). Releases
are built and published by `.github/workflows/publish-sdk-python.yml`.
