Skip to content

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.

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,
)

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)

If your workflow sends and then downloads invoices, keep the two phases separate:

  1. Send XML through an online or batch session.

  2. Persist the returned KSeF number or session reference.

  3. Poll session status or metadata until processing completes.

  4. Download by KSeF number, or build an export filter for the same period.