Metadata-Version: 2.5
Name: ksefe
Version: 0.1.0
Summary: Official Python SDK for the Ksefe API — Poland's KSeF e-invoicing gateway.
Project-URL: Homepage, https://ksefe.pl
Project-URL: Source, https://github.com/ksefe/sdk
Project-URL: Issues, https://github.com/ksefe/sdk/issues
License-Expression: MIT
License-File: LICENSE
Keywords: e-invoicing,faktura,ksef,ksefe,poland
Requires-Python: >=3.10
Requires-Dist: httpx>=0.28
Requires-Dist: microsoft-kiota-abstractions==1.18.0
Requires-Dist: microsoft-kiota-http==1.18.0
Requires-Dist: microsoft-kiota-serialization-form==1.18.0
Requires-Dist: microsoft-kiota-serialization-json==1.18.0
Requires-Dist: microsoft-kiota-serialization-multipart==1.18.0
Requires-Dist: microsoft-kiota-serialization-text==1.18.0
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=1.5; extra == 'dev'
Requires-Dist: pytest>=9.2; extra == 'dev'
Requires-Dist: respx>=0.23; extra == 'dev'
Description-Content-Type: text/markdown

# ksefe

Official **async** Python SDK for the **[Ksefe API](https://ksefe.pl)** — a
JSON-first REST gateway over Poland's KSeF e-invoicing system. Generated from the
API's OpenAPI 3.1 description with [Kiota](https://learn.microsoft.com/openapi/kiota/).

## Install

```bash
pip install ksefe
```

Requires Python 3.10+.

## Quickstart (async)

```python
import asyncio

from ksefe import ApiKeyAuthenticationProvider, HttpxRequestAdapter, KeyLocation, KsefeClient, unwrap
from ksefe.models import (
    CreateContext,
    CreateContext_environment,
    EntityCreate,
    EntityCreate_kind,
    EntityCreate_taxIdKind,
)


async def main() -> None:
    api_key = "..."
    ksefe = KsefeClient(
        HttpxRequestAdapter(
            ApiKeyAuthenticationProvider(KeyLocation.Header, f"Bearer {api_key}", "Authorization")
        )
    )

    entity = await ksefe.entities.post(
        EntityCreate(
            kind=EntityCreate_kind.Company,
            name="Example sp. z o.o.",
            country_code="PL",
            tax_id="5265877635",
            tax_id_kind=EntityCreate_taxIdKind.Nip,
        )
    )
    context = await ksefe.ksef.contexts.post(
        CreateContext(
            entity_id=entity.id, name="Example", environment=CreateContext_environment.Test
        )
    )

    submission = unwrap(await ksefe.ksef.submissions.by_id(submission_id).get())
    upo = await ksefe.ksef.submissions.by_id(submission_id).upo.get()

    invoices = await ksefe.ksef.invoices.get()
    vat = await ksefe.vat.by_tax_id("5265877635").get()


asyncio.run(main())
```

Models and enums import from `ksefe.models`, and models take keyword arguments.
Enum fields take the enum member (`EntityCreate_kind.Company`); a plain string
raises `TypeError` naming the field, before anything is sent.

KSeF TEST needs no certificate: the API generates a self-signed one. For `demo`
and `prod`, pass your KSeF certificate as base64 PKCS#12 (or a PEM pair):

```python
from ksefe.models import CertificateUpload

await ksefe.ksef.contexts.post(CreateContext(
    entity_id=entity.id,
    name="Production",
    environment=CreateContext_environment.Prod,
    certificate=CertificateUpload(pkcs12=p12_base64, password=password),
))
```

## API shape

A [Kiota](https://learn.microsoft.com/openapi/kiota/) fluent client — **async**.
Resources mirror the URL structure: `client.entities`, `client.ksef.contexts`,
`client.ksef.submissions`, `client.ksef.invoices`, `client.integrations`,
`client.bank.connections`, `client.bank.transactions`, `client.vat`. Path
parameters are `by_id(…)` / `by_ksef_number(…)` / `by_tax_id(…)`, and the HTTP verb is the
terminal awaited call (`await …get()`, `await …post(body)`). Every model and enum
is importable from `ksefe.models`.

## Pagination

Listings are cursor-paginated: each page returns `next_cursor`, which you pass
back as `cursor`. The `paginate` async iterator walks every page for you. Give it
the listing's request builder and its filters:

```python
from ksefe import paginate

async for invoice in paginate(ksefe.ksef.invoices, context_id=[context.id]):
    print(invoice.ksef_number)
```

Filters use the Python names of the listing's query parameters and are sent
unchanged on every page; an unknown one raises `TypeError` before any request.
Pass `page_size=…` to change the page size (default 50, maximum 100).

## Auth & errors

The client is the one [Kiota](https://learn.microsoft.com/openapi/kiota/authentication)
generates: a request adapter with an authentication provider, here an API key sent
as `Authorization: Bearer <api_key>` on every request. Error responses
(RFC 9457 `application/problem+json`) are raised as `Problem` exceptions carrying
`status`, `title` and `detail`. Detect them with `is_ksefe_error`:

```python
from ksefe import is_ksefe_error

try:
    await ksefe.ksef.contexts.post(body)
except Exception as err:
    if is_ksefe_error(err):
        print(err.status, err.detail)
    else:
        raise
```

A full runnable flow (entity → context → submit → poll → UPO, plus pagination)
is in [`examples/quickstart.py`](./examples/quickstart.py).

## Configuration

The client targets `https://ksefe.pl/api/v1`. To use another deployment, set the
base URL on the adapter before constructing the client; pass an `httpx.AsyncClient`
for proxies or custom transports:

```python
import httpx

adapter = HttpxRequestAdapter(
    ApiKeyAuthenticationProvider(KeyLocation.Header, f"Bearer {api_key}", "Authorization"),
    http_client=httpx.AsyncClient(proxy="http://proxy.example:8080"),
)
adapter.base_url = "https://staging.example/api/v1"
ksefe = KsefeClient(adapter)
```

The client serializes request bodies strictly: an enum field holding a plain
string raises `TypeError` instead of being dropped. Kiota keeps one serializer
registry per process, so if you also use another Kiota-generated client, construct
it before this one.

## License

MIT. Generated code is regenerated from the OpenAPI spec — do not edit
`ksefe/_generated/**` or `ksefe/models.py` by hand.
