KSeF

KSeF webhooks: automating invoice status end to end

How Notaly webhooks deliver KSeF status asynchronously: invoice.accepted, invoice.rejected and invoice.error, HMAC signing and retries.

Published 3 min readBy Tomek

Overview

KSeF decides whether an invoice is accepted asynchronously, so the submit call alone is not the end of the story: Notaly polls KSeF for you and pushes the final result to your endpoint as a signed webhook — invoice.accepted (with the KSeF number, and the UPO when KSeF has issued it) or invoice.rejected. Each webhook is signed with HMAC and retried with backoff, so you can build reliable order-to-invoice automation without polling yourself.

Why webhooks fit KSeF

KSeF (Krajowy System e-Faktur) is Poland's national e-invoicing platform operated by the Ministry of Finance (Ministerstwo Finansów). When you submit a FA(3) invoice, the platform returns a submitted status and processes the document afterward. Polling for that result yourself means writing a background worker; a webhook inverts that, so the result comes to you once it is known.

The events you receive

invoice.accepted means KSeF accepted the invoice. The payload includes the KSeF number, the UPO if KSeF has already issued it (otherwise null) and an absolute PDF URL. If the UPO arrives later, Notaly stores it and you read it from GET /invoices/{id}; no second webhook is sent. invoice.rejected means KSeF rejected it and the error field carries a stable code such as ksef_rejected:450.

invoice.error is rare: it signals an invoice in an inconsistent state that needs support. It does not cover a failed submission. If KSeF does not confirm the submission during POST /invoices, the POST response returns the invoice as draft with error ksef_submitting and Notaly resends the same XML; you then get invoice.accepted or invoice.rejected once KSeF decides. An invoice marked ksef_duplicate_review or ksef_retry_exhausted gets no webhook: check it with GET /invoices/{id}.

HMAC signing

Each delivery carries X-Webhook-Id, X-Webhook-Timestamp and X-Webhook-Signature, an HMAC-SHA256 of the timestamp and the raw body computed with your endpoint secret. Verify the signature on your endpoint before trusting the body, and reject deliveries that fail the check or carry a stale timestamp.

Retries and idempotency

If your endpoint is unreachable or returns an error, Notaly retries the delivery with backoff. Delivery is at-least-once, so your handler must be idempotent: applying the same invoice.accepted event twice must not create two records. Use X-Webhook-Id as the deduplication key.

A minimal handler

On invoice.accepted, store the KSeF number (and the UPO, if present) against the invoice. The pdfUrl requires your API key, so your customer cannot open it directly: download the PDF on your server and serve it yourself. On invoice.rejected, read the error code, fix the data and send a new POST /invoices. Keep the handler fast, verify the HMAC, and return 2xx only after you have persisted the result.

Disclaimer

Notaly is not a tax, accounting or legal advisor. This article is general information, not tax advice, and may be outdated. You are responsible for your own VAT compliance; confirm with a qualified accountant where required.

Sources

Source: www.podatki.gov.pl

Frequently asked questions

Why do I need webhooks for KSeF?

KSeF processes invoices asynchronously. The submit call usually returns submitted (or draft with ksef_submitting while Notaly resends it), and the final result arrives later as an invoice.accepted or invoice.rejected webhook.

How do I know the webhook is genuine?

Notaly signs each webhook with HMAC-SHA256 in the X-Webhook-Signature header, so you can verify the signature on your endpoint before trusting the payload.

What should I do if my endpoint is down?

Notaly retries delivery with backoff. Keep your handler idempotent and deduplicate on X-Webhook-Id so a retried event is not applied twice.

Issue your first KSeF test invoice today

Create a sandbox account: API key in seconds, 3 free test invoices on KSeF TEST, no credit card.