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 ksef2 import Client, Environment, FormSchema
from ksef2.core.xades import generate_test_certificate

VALID_NIP = "5261040828"

client = Client(Environment.TEST)

# XAdES Authentication (TEST environment) - generates self-signed certificate automatically
cert, private_key = generate_test_certificate(VALID_NIP)
tokens = client.auth.authenticate_xades(
    nip=VALID_NIP,
    cert=cert,
    private_key=private_key,
)

# Send invoice
with client.sessions.open_online(
    access_token=tokens.access_token.token,
    form_code=FormSchema.FA3,
) as session:
    with open("invoice.xml", "rb") as f:
        result = session.send_invoice(f.read())
    print(result.reference_number)

# Sessions also support manual management:
session = client.sessions.open_online(
    access_token=tokens.access_token.token,
    form_code=FormSchema.FA3,
)
try:
    with open("invoice.xml", "rb") as f:
        result = session.send_invoice(f.read())
    print(result.reference_number)
finally:
    session.terminate()

Token Authentication

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

from ksef2 import Client, Environment

client = Client(Environment.TEST)

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.1.1.tar.gz (1.4 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.1.1-py3-none-any.whl (100.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ksef2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d5c9ce7acf72a2783da0ecdb253619537867fa6b4e21a3877068f2adc2afdc68
MD5 9691d577f107f5b3edcdb7449c08ba25
BLAKE2b-256 7e3f5f2caaa19aa3d62c8cd500c468cd5a74da1735af502080b4cf9259201b8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2-0.1.1.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.1.1-py3-none-any.whl.

File metadata

  • Download URL: ksef2-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 100.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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 050a69fcaab0eaca480a594f1abda5a4b7087d610d2fe3e2a16ba3c2074e5464
MD5 50eed95ce7f0d00afd0628708a8375b3
BLAKE2b-256 55686595ef94844cff2e351be39e9e0306020b9f52648a98b332c2f17d8ae6be

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2-0.1.1-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