Skip to main content

Python SDK and Tools for Poland's KSeF (Krajowy System e-Faktur) API

Project description

KSeF Toolkit

Python SDK and Tools for Poland's KSeF (Krajowy System e-Faktur) v2.0 API.

API Coverage Python Version
beartype pre-commit Ruff License: MIT

Installation

uv add ksef2

Requires Python 3.12+.

Quick Start

from datetime import datetime, timezone
from ksef2 import Client, Environment, FormSchema
from ksef2.core.xades import generate_test_certificate
from ksef2.domain.models import (
    InvoiceQueryFilters, InvoiceSubjectType, InvoiceQueryDateRange, DateType,
)

NIP = "5261040828"
client = Client(Environment.TEST)

# Authenticate (XAdES — TEST environment)
cert, key = generate_test_certificate(NIP)
tokens = client.auth.authenticate_xades(nip=NIP, cert=cert, private_key=key)

with client.sessions.open_online(
    access_token=tokens.access_token.token,
    form_code=FormSchema.FA3,
) as session:

    # Send an invoice
    result = session.send_invoice(open("invoice.xml", "rb").read())
    print(result.reference_number)

    # Check processing status
    status = session.get_invoice_status(result.reference_number)

    # Export invoices matching a query
    export = session.schedule_invoices_export(
        filters=InvoiceQueryFilters(
            subject_type=InvoiceSubjectType.SUBJECT1,
            date_range=InvoiceQueryDateRange(
                date_type=DateType.ISSUE,
                from_=datetime(2026, 1, 1, tzinfo=timezone.utc),
                to=datetime.now(tz=timezone.utc),
            ),
        ),
    )

    # Download the exported package
    export_result = session.get_export_status(export.reference_number)
    if package := export_result.package:
        for path in session.fetch_package(package=package, target_directory="downloads"):
            print(f"Downloaded: {path}")

Full runnable version: send_query_export_download.py — more examples in scripts/examples.

Token Authentication

For production, or when you have a pre-generated KSeF token:

from ksef2 import Client, Environment

# uses production environment by default
client = Client()

tokens = client.auth.authenticate_token(
    ksef_token="your-ksef-token",
    nip=VALID_NIP,
)
print(tokens.access_token.token)

Development

just sync          # Install all dependencies (including dev)
just test          # Run unit tests
just regenerate-models  # Regenerate OpenAPI models

Other commands

just integration   # Run integration tests (requires KSEF credentials in .env)
just coverage       # Calculate API coverage (updates coverage.json)
just fetch-spec     # Fetch latest OpenAPI spec from KSeF

API Coverage

The SDK currently covers 33 of 77 KSeF API endpoints (43%). Calculated via scripts/api_coverage.py. See individual feature docs for endpoint details:

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ksef2-0.6.2.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ksef2-0.6.2-py3-none-any.whl (91.0 kB view details)

Uploaded Python 3

File details

Details for the file ksef2-0.6.2.tar.gz.

File metadata

  • Download URL: ksef2-0.6.2.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ksef2-0.6.2.tar.gz
Algorithm Hash digest
SHA256 4a835de87ca539cbd66f37e3b04bc83684581aac5c55a722039ba50ce772a4ff
MD5 6de66e51249d7b2526d463075152a0a6
BLAKE2b-256 bcf3fce78b284bf916f7d23638fcd996d68108e2bb2899e4683aad1a4b32d3c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2-0.6.2.tar.gz:

Publisher: publish.yml on artpods56/ksef2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ksef2-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: ksef2-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 91.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ksef2-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f8c918440f4038220e47583b2a1d1ea637f03558e8a61768885e8bd426bc27d5
MD5 dde197056fdcdfeb56e667c01088fe9b
BLAKE2b-256 06e63ac9f7e75f75ef8eb467a9c3b2a42b26b8f8a5e6e4ca1661086da23bf827

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2-0.6.2-py3-none-any.whl:

Publisher: publish.yml on artpods56/ksef2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page