Issuing a structured invoice using the Open API of the National e-Invoice System (KSeF)
The process of issuing e-Invoices using API KSeF is not trivial at all, which is why we have prepared the instructions below so that anyone who is not afraid can try their hand at this "competition". Anyway, there's no point in extending the introduction - see for yourself:
1. Authorization challenge
To start communication with the API KSeF first we need to invoke AutrhorisationChallenge , i.e. the so-called authorization challenge based on which we will be able to establish a session. So we invoke:
POST /online/Session/AuthorizationChallenge
Where we give the time in the format timestamp (e.g.: „timestamp": "2023-10-10T13:00:00Z") and identifier type („identifierType„: „onip„) and its value („identifier": " ")
The timestamp and the value of the authorization challenge itself will be returned
2. Session signed
Then we invoke the establishment of a signed session:
POST /online/Session/InitSigned
InitSessionSignedRequest – contains an optional section Encryption – each invoice sent as part of the created interactive session will be encrypted with the symmetric key indicated in the session Encryption.
We add:
Attention! Having token session – each subsequent query should be authorized with this token
3. Granting permission to send requests to KSeF
Granting authorizations (i.e. to whom and what we grant) for a specific PESEL number, i.e. to a person who within the company will be able to, for example, issue and read invoices:
POST /online/Credentials/Grant
The process of granting permissions is asynchronous and may take a while. But using the value of the returned attribute elementReferenceNumber we can find out what the status is - in this case - of granting permissions.
3a. Checking the status of granting permissions
GET /online/Credentials/Status/{CredentialsElementReferenceNumber}
4. Authentication request
Now, having been granted PESEL permissions, we can once again send a signed authentication request with a qualified signature (with PESEL):
POST /online/Session/InitSigned
In response we should receive:
He is important token session (token), which allows you to perform operations such as issuing and receiving invoices. Of course, there is also a completely different session (different value for referenceNumber).
5. Generating an authorization token
Below are examples of requests that should be sent to the method GenerateToken.
POST /online/Credentials/GenerateToken
Call and response:
Attention! Token authorization (authorizationToken) is returned and available for download only once after calling the method GenerateToken, later it is no longer obtainable.
This token authorization code can now be entered in the application.
Due to processingCode:100 we need to check when the generation process will end token…. And only then (when it will be processingCode:200) token will be ready to use.
5a. Checking the token generation status
To check the generation status token, we need to send the attribute value elementReferenceNumber from the answer obtained in step 5 send to the method address:
GET /online/Credentials/Status/{CredentialsElementReferenceNumber}
6. Establishing a session enabling sending e-Invoices
if we have token authorization, now you need to establish a session with it in order to perform activities such as sending or receiving invoices.
Attention! We no longer have to sign such a prepared request - this is the difference between establishing a session using a signature and a session using token authorization.
In response we get token session: in the attribute sessionToken > token
Attention! With this token We can only issue/receive invoices!!!
7. Sending the e-Invoice to KSeF
PUT /online/Invoice/Send
Value in the invoice attribute <Identification of> must be the same as the context value declared in the authorization challenge. An exception is e.g. self-billing, then a different NIP may appear in this field, but you must have permissions granted to self-billing (and the recipient's data must be the same as the NIP declared in the authorization challenge).
Request example:
Attention! Attribute referenceNumber, i.e. the session reference number should be put aside because after it we will be able to check and download the UPO.
7a. Checking the status of receipt of the invoice we sent by KSeF
GET /online/Credentials/Status/{CredentialsElementReferenceNumber}
If the invoice has been accepted (status 200), this attribute is important ksefReferenceNumber, which uniquely identifies the invoice. Attribute acquisitionTimestamp will specify the date and time when exactly the invoice was accepted into the system KSeF.
Yes, that's it and we can be happy to issue the first e-invoice in... KSeF.
This entry was based on Webinar conducted on November 25, 2022. by representatives of the Ministry of Finance.