Skip to main content

ksefe

Official async Python SDK for the Ksefe API — a JSON-first REST gateway over Poland's KSeF e-invoicing system. Generated from the API's OpenAPI 3.1 description with Kiota.

Install

pip install ksefe

Requires Python 3.10+.

Quickstart (async)

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):

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 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:

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 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:

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.

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:

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.

Metadata

Release files for ksefe 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ksefe 0.1.0
File Size Uploaded Provenance Actions
ksefe-0.1.0.tar.gz 127.8 kB With provenance Details for ksefe-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ksefe 0.1.0
File Interpreter ABI Platform Actions
ksefe-0.1.0-py3-none-any.whl Python 3 none any Details for ksefe-0.1.0-py3-none-any.whl

Total release size: 523.1 kB

Release files / ksefe-0.1.0.tar.gz

Download URL ksefe-0.1.0.tar.gz
Size 127.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7f7fcde0b5d823e11ade70214a52b7c137dcb5dd7ec41ca43a810c8316a82a3d
BLAKE2b-256 checksum
How to use checksums
67bd948a58a65dcf28d28adbfe8c4654fafca6e2562c9f039e1da6a41b7a9676
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / ksefe-0.1.0-py3-none-any.whl

Download URL ksefe-0.1.0-py3-none-any.whl
Size 395.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c325f85542c5d7f46d050d9d0f6ac0166695bdfece189b04f00c574640c18d68
BLAKE2b-256 checksum
How to use checksums
75db88784bb1c120b8d713488c9e9e04d6ccfc9800d4f2ba9da6e0cebc873b0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for ksefe 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ksefe-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page