KSeF 2.0 National e-Invoicing System API

repository·main·Indexed 42 days ago

https://github.com/cirfmf/ksef-api

Integration guide for the Polish Ministry of Finance's National e-Invoicing System (Krajowy System e-Faktur) version 2.0. Covers authentication, XAdES signature requirements, session management, and security protocols. Includes technical specifications for RSASSA-PKCS1-v1_5, RSASSA-PSS, and ECDSA algorithms, as well as guidance on using official C# (ksef-client-csharp) and Java (ksef-client-java) reference libraries.

Tokens
44.9K
Snippets
102
Records
167
Agent score
50%

What's inside KSeF

  1. Overview of KSeF 2.0 Integration Guide

    main

    The KSeF 2.0 - Przewodnik dla Integratorów is a technical compendium designed for developers, analysts, and system integrators working with the National e-Invoicing System (Krajowy System e-Faktur - KSeF) version 2.0. It focuses on technical and practical aspects of communicating with the KSeF API.

    Key integration areas covered include:

    • Authentication: Access acquisition and session management.
    • Permissions & Certificates: Managing KSeF certificates and user permissions.
    • Operational Modes: Offline modes, technical corrections, interactive sessions, and batch sessions.
    • Invoice Management: Downloading invoices and incremental invoice downloading.
    • Security: QR codes, KSeF token management, and public encryption keys.
    • System Constraints: API limits and test data scenarios.

    Each topic typically includes detailed functional descriptions, code examples in C# and Java, and links to the official OpenAPI specification.

  2. Overview of KSeF Batch Sessions

    main

    Batch sending (Wysyłka wsadowa) allows for the single-time transmission of multiple invoices within a single ZIP file, rather than sending each invoice individually. This is designed to accelerate and simplify the processing of large volumes of documents.

    Key constraints and requirements:

    • Each invoice must be in XML format according to the current Ministry of Finance schema.
    • The ZIP package should be divided into parts no larger than 100 MB (before encryption). These parts are encrypted and sent separately.
    • Information about each part must be provided in the fileParts object.
    • You must have a valid accessToken obtained via the authentication process.
  3. What is an interactive session in KSeF?

    main

    An interactive session is used to send individual structured invoices to the KSeF API. Each invoice must be prepared in XML format according to the current schema published by the Ministry of Finance.

    Sessions are lightweight and synchronous, allowing multiple interactive sessions to be maintained simultaneously under a single authentication. A session remains valid for 12 hours from its creation, after which it is automatically closed.

  4. How time-window synchronization (Windowing) works

    main

    To prevent data gaps, invoices should be retrieved in adjacent time windows using the PermanentStorageHwmDate.

    Key Rules for Windowing:

    • Enable HWM: You must set restrictToPermanentStorageHwmDate = true in your export request.
    • Continuity: Each subsequent window must start exactly where the previous one ended. The 'end moment' is defined as the dateRange.to value (if provided) or the PermanentStorageHwmDate (if dateRange.to is omitted).
    • Subject Types: Invoices must be retrieved separately for each subject type (Podmiot 1, Podmiot 2, Podmiot 3, Podmiot upoważniony) to ensure completeness, as a company can appear in different roles on different invoices.
    • Truncated Batches: If a batch is truncated (IsTruncated = true), the next call must start from LastPermanentStorageDate. If not truncated (IsTruncated = false), the next call starts from the returned PermanentStorageHwmDate.
  5. Manage internal KSeF certificates

    main

    A new module allows entities to manage internal KSeF certificates, which are required for authenticating in KSeF and issuing invoices in offline mode.

    Capabilities include:

    • Submitting an application for an internal certificate (using attributes from the signature certificate used during authentication).
    • Downloading issued digital certificates.
    • Checking the status of certificate applications.
    • Retrieving metadata for issued certificates.
    • Checking the allowed limit of certificates.
  6. Understand KSeF Certificate types and identity

    main

    A KSeF certificate is an identity carrier for an authenticating entity (identified by PESEL or NIP). It does not grant permissions itself; permissions are managed by KSeF based on the authenticated entity's model.

    There are two mutually exclusive certificate types:

    TypeDescriptionKey Usage
    AuthenticationUsed for authenticating in the KSeF system.Digital Signature (80)
    OfflineUsed exclusively for issuing invoices in offline mode to confirm authenticity and integrity via QR code II. Does not allow authentication.Non-Repudiation (40)

    Important: A certificate can only have one type. You cannot issue a certificate that combines both functions.

  7. Use _metadata.json for invoice deduplication

    main

    The _metadata.json file is a JSON object containing an invoices property, which is an array of InvoiceMetadata elements (matching the structure returned by POST /invoices/query/metadata).

    This file is critical for deduplication because it contains the KSeF numbers for all invoices within the package. Use these numbers to ensure that invoices processed in overlapping windows are not duplicated in your system.

  8. Understand the new indirect permission model in KSeF API 2.0

    main

    KSeF API 2.0 replaces the previous permission inheritance model with an indirect permission mechanism. This allows for a clear separation between a subject's (e.g., an accounting office) own invoice management and the management of their clients' invoices.

    Key Concepts:

    • Indirect Granting: A client can grant a subject permission to view or issue invoices with a special option enabled that allows the subject to further delegate that permission. The subject can then grant these permissions to their own employees.
    • General Permissions: A subject can grant 'general permissions' to an employee, allowing them to handle all clients of that subject, provided the clients have authorized the subject and the employee's specific scope (viewing and/or issuing) allows it.
    • Decoupling: Permissions for managing a subject's own invoices are now independent of the permissions required to manage client invoices. This prevents the need for 'excessive' permissions that were required in version 1.0.
    • EU Self-Invoicing: A new permission type and login context support self-invoicing for EU entities. Users can log in using a context defined by a Polish entity (identified by NIP) and an EU entity (identified by its EU VAT number), allowing authenticated representatives to issue invoices on behalf of the Polish entity.
  9. Implement High Water Mark (HWM) for continuous synchronization

    main

    The High Water Mark (HWM) mechanism manages starting points for subsequent incremental exports to minimize duplicates and ensure completeness. It consists of two components:

    • PermanentStorageHwmDate: A stable upper boundary representing the point up to which the system guarantees data completeness.
    • LastPermanentStorageDate: The date of the last invoice in the package, used specifically when a package is truncated (IsTruncated = true).

    Continuation Strategy

    To maintain continuity and avoid missing documents, determine the starting point for the next export using this priority hierarchy:

    1. If the package is truncated (IsTruncated = true): Start the next export from LastPermanentStorageDate.
    2. If the package is stable: Use PermanentStorageHwmDate as the starting point for the next window.

    Note: Subsequent windows start at the same point as the previous one (adjacency); any resulting duplicates should be removed during the deduplication stage using KSeF numbers from _metadata.json.

  10. How authentication works in KSeF API 2.0 (JWT)

    main

    In version 2.0, authentication is a standalone process independent of session initialization. The system uses standard JWT (JSON Web Tokens) for authorizing all protected operations.

    Key improvements over v1.0:

    • Authentication is decoupled from session management.
    • Tokens can be reused to create multiple sessions.
    • Supports standard token refreshing and revocation.
  11. Use certificate fingerprints for authentication

    main
    If a qualified certificate does not have the correct identifiers stored in the subject attribute OID.2.5.4.5, authentication is possible by first granting permissions to the SHA-256 hash (fingerprint) of that certificate. Refer to the authentication documentation for details on how to implement this.