Quickstart
This page shows the shape of the SDK in the TEST environment. Use a real MCU certificate for DEMO or PRODUCTION.
Choose a client mode
Section titled “Choose a client mode”Use the sync client in ordinary scripts and command-line tools. Use the async client when your application already owns an event loop.
from datetime import datetime, timedelta, timezonefrom pathlib import Path
from ksef2 import Client, Environment, FormSchemafrom ksef2.domain.models import InvoicesFilter
NIP = "5261040828"
client = Client(Environment.TEST)auth = client.authentication.with_test_certificate(nip=NIP)
with auth.online_session(form_code=FormSchema.FA3) as session: result = session.send_invoice(invoice_xml=Path("invoice.xml").read_bytes()) status = session.wait_for_invoice_ready( invoice_reference_number=result.reference_number, timeout=60.0, ) print(status.ksef_number)
filters = InvoicesFilter( role="seller", date_type="issue_date", date_from=datetime.now(tz=timezone.utc) - timedelta(days=1), date_to=datetime.now(tz=timezone.utc), amount_type="brutto",)
export = auth.invoices.schedule_export(filters=filters)package = auth.invoices.wait_for_export_package( reference_number=export.reference_number, timeout=120.0,)
for path in auth.invoices.fetch_package( package=package, export=export, target_directory="downloads",): print(path)import asynciofrom datetime import datetime, timedelta, timezonefrom pathlib import Path
from ksef2 import AsyncClient, Environment, FormSchemafrom ksef2.domain.models import InvoicesFilter
NIP = "5261040828"
async def main() -> None: async with AsyncClient(Environment.TEST) as client: auth = await client.authentication.with_test_certificate(nip=NIP)
async with auth.online_session(form_code=FormSchema.FA3) as session: result = await session.send_invoice( invoice_xml=Path("invoice.xml").read_bytes() ) status = await session.wait_for_invoice_ready( invoice_reference_number=result.reference_number, timeout=60.0, ) print(status.ksef_number)
filters = InvoicesFilter( role="seller", date_type="issue_date", date_from=datetime.now(tz=timezone.utc) - timedelta(days=1), date_to=datetime.now(tz=timezone.utc), amount_type="brutto", )
export = await auth.invoices.schedule_export(filters=filters) package = await auth.invoices.wait_for_export_package( reference_number=export.reference_number, timeout=120.0, ) zip_parts = await auth.invoices.fetch_package_bytes( package=package, export=export, ) print(len(zip_parts))
asyncio.run(main())What happened
Section titled “What happened”- The root client selected the TEST environment.
with_test_certificate()created a TEST-only XAdES certificate and returned an authenticated client.online_session()opened a session with invoice-specific encryption data.wait_for_invoice_ready()andwait_for_export_package()used timeout seconds and polling intervals rather than attempt counts.