Downloading Invoices
Use direct download when you already know one KSeF number. Use exports when you need many invoices or want the SDK to download encrypted package parts.
Download one invoice
Section titled “Download one invoice”xml_bytes = auth.invoices.download_invoice(ksef_number="KSeF-number")
with open("invoice.xml", "wb") as handle: handle.write(xml_bytes)If the invoice was just sent, poll until KSeF makes the processed XML downloadable:
xml_bytes = auth.invoices.wait_for_invoice_download( ksef_number="KSeF-number", timeout=120.0, poll_interval=2.0,)Export many invoices
Section titled “Export many invoices”Exports are asynchronous. Schedule an export from filters, wait for the package, then fetch decrypted ZIP parts.
export = auth.invoices.schedule_export(filters=filters)package = auth.invoices.wait_for_export_package( reference_number=export.reference_number, timeout=300.0,)
for path in auth.invoices.fetch_package( package=package, export=export, target_directory="downloads",): print(path)zip_parts = auth.invoices.export_and_download( filters=filters, timeout=300.0,)
for part in zip_parts: print(len(part))After sending invoices
Section titled “After sending invoices”If your workflow sends and then downloads invoices, keep the two phases separate:
-
Send XML through an online or batch session.
-
Persist the returned KSeF number or session reference.
-
Poll session status or metadata until processing completes.
-
Download by KSeF number, or build an export filter for the same period.