Integration guide

KSeF API Python integration guide

Send a KSeF FA(3) invoice from Python with the Notaly API using requests: POST /invoices, idempotency, error handling and status tracking.

Updated

Overview

Python is a natural fit for issuing KSeF invoices from a backend service, a cron job or a data pipeline. With Notaly you never touch FA(3) XML by hand: you POST a JSON dictionary to https://api.getnotaly.com/invoices, and Notaly generates the XML, validates it against the official XSD and submits it to KSeF. This guide uses the popular requests library, which you can install with pip install requests.

Setup

Grab your API key from the Notaly dashboard after creating a free sandbox account. In the sandbox the seller NIP must be exactly 2745013933; anything else is rejected with 422 seller_nip_not_authorized before the invoice is counted against your three free test invoices. Keep the key in an environment variable (NOTALY_API_KEY) rather than hard-coding it in source.

Create an invoice

import os
import requests

NOTALY_API_KEY = os.environ["NOTALY_API_KEY"]

invoice = {
    "number": "FV/2026/10/001",
    "issueDate": "2026-10-12",
    "currency": "PLN",
    "seller": {"nip": "2745013933", "name": "Example Sp. z o.o. (FICTIONAL)", "address": {"line1": "ul. Testowa 1, 00-001 Warszawa", "country": "PL"}},
    "buyer": {"nip": "1234563218", "name": "Test Customer S.A. (FICTIONAL)", "address": {"line1": "ul. Probna 2, 30-001 Kraków", "country": "PL"}},
    "items": [
        {"name": "SaaS subscription - October", "unit": "pcs.", "quantity": 1, "unitNetPrice": 1000, "vatRate": 23},
        {"name": "Support", "unit": "hrs.", "quantity": 2, "unitNetPrice": 150.5, "vatRate": 8},
    ],
}

response = requests.post(
    "https://api.getnotaly.com/invoices",
    headers={
        "Authorization": f"Bearer {NOTALY_API_KEY}",
        # stable per invoice: reuse the same key when you retry this invoice
        "Idempotency-Key": f"invoice-{invoice['number']}",
    },
    json=invoice,
    timeout=30,
)
response.raise_for_status()
created = response.json()
print(f"Invoice {created['id']} is {created['status']}", created.get("error") or "")

requests.post(..., json=invoice) serialises the dictionary and sets Content-Type: application/json. The timeout=30 guards against hanging requests. raise_for_status() turns a 4xx or 5xx into an exception. Retry only timeouts and 5xx responses, and always with the same Idempotency-Key; a 4xx means the request itself must be fixed.

Understand the response

The response is a JSON object with id and status. status is submitted when KSeF received the invoice and returned a reference number. If the response is draft with error: "ksef_submitting", KSeF has not confirmed the submission yet. Do not send a new POST and do not issue the invoice again under a new number: Notaly resends the same XML automatically. Poll GET /invoices/{id} until the status is submitted. Such a draft already counts towards the three sandbox invoices. If the status is submitted with error: "ksef_duplicate_review", KSeF already has this invoice from an earlier attempt. Do not reissue it with another number; we link it to the KSeF record on our side. If you need it sooner, write to hello@getnotaly.com with the invoice id. If the invoice is draft with error: "ksef_retry_exhausted", Notaly has stopped resending it automatically (after 24 hours, or earlier if KSeF returned a definitive error). Do not create a new invoice: write to hello@getnotaly.com with the invoice id, and we check in KSeF whether it arrived before anything is resent. If an invoice is still draft with error: null or ksef_unavailable after about a minute, Notaly does not resend it: do not create a new invoice; write to hello@getnotaly.com with the invoice id.

Accepted invoices gain ksefNumber, pdfUrl and, when KSeF issues it, upo. Notaly validates and submits the invoice; VAT rates and tax classification remain your responsibility.

React to failures

Branch on the JSON body rather than parsing messages. validation_failed (422) comes with a details list of field-level problems. xsd_invalid (422) means the generated XML failed the official FA(3) schema; nothing was sent or counted. currency_not_supported (422) means the currency is not PLN, the only one supported today. quota_exceeded (402) means your three free sandbox invoices are spent. A 503 (ksef_unavailable or validator_unavailable) is transient and nothing was stored — retry with exponential backoff. Authentication problems surface as 401 (missing or invalid key) or 404 (valid key, wrong resource).

Track status

Poll GET /invoices/{id} with the same Authorization header, or configure webhooks. The invoice moves through draft → submitted → accepted or rejected; rejected invoices carry an error code such as ksef_rejected:<code> that you can log and act on.

Sources

Source: getnotaly.com

Frequently asked questions

Do I need to install anything?

Only the requests library: pip install requests. Python 3.8+ is recommended.

How do I make retries safe?

Send an Idempotency-Key header derived from your own invoice identifier and reuse the same key on every retry of that invoice. Notaly then returns the invoice already stored instead of creating a new one.

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.