Python SDK and Tools for Poland's KSeF (Krajowy System e-Faktur) API
Project description
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 inscripts/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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ksef2-0.5.0.tar.gz.
File metadata
- Download URL: ksef2-0.5.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5233ce6f170f384bbdc3c9c7ec8f0d45472b927c621d749d7c2352b560d76da
|
|
| MD5 |
ca62afe5f6bb068355e90faef1bc90e4
|
|
| BLAKE2b-256 |
4e426ed4b097d83293e1222a20e05cc5e1a63672ff81c4f884a73bb21e15f32e
|
Provenance
The following attestation bundles were made for ksef2-0.5.0.tar.gz:
Publisher:
publish.yml on artpods56/ksef2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ksef2-0.5.0.tar.gz -
Subject digest:
b5233ce6f170f384bbdc3c9c7ec8f0d45472b927c621d749d7c2352b560d76da - Sigstore transparency entry: 955105137
- Sigstore integration time:
-
Permalink:
artpods56/ksef2@7053229b125d893c45bba560e9a6be7bbc1cc838 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/artpods56
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7053229b125d893c45bba560e9a6be7bbc1cc838 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ksef2-0.5.0-py3-none-any.whl.
File metadata
- Download URL: ksef2-0.5.0-py3-none-any.whl
- Upload date:
- Size: 80.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b286d76c2caa5c232af2e536b6a1f3cd11d902e7b79cd3cda6b6a5a297b08dc
|
|
| MD5 |
5f1567053508c01b499cad51f0c83d30
|
|
| BLAKE2b-256 |
c0e8269636d7fbacacb506f86a42f3c813860eba25ed8e6e0b3853512a639944
|
Provenance
The following attestation bundles were made for ksef2-0.5.0-py3-none-any.whl:
Publisher:
publish.yml on artpods56/ksef2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ksef2-0.5.0-py3-none-any.whl -
Subject digest:
9b286d76c2caa5c232af2e536b6a1f3cd11d902e7b79cd3cda6b6a5a297b08dc - Sigstore transparency entry: 955105139
- Sigstore integration time:
-
Permalink:
artpods56/ksef2@7053229b125d893c45bba560e9a6be7bbc1cc838 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/artpods56
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7053229b125d893c45bba560e9a6be7bbc1cc838 -
Trigger Event:
push
-
Statement type: