DEV Community

Nuh Taspinar
Nuh Taspinar

Posted on Edited on Originally published at github.com

Validating XRechnung / ZUGFeRD invoices from Python (no dependencies)

Every German B2B invoice now has to be machine-readable: XRechnung (UBL or CII) or ZUGFeRD. When you generate those files from your own code, the interesting question is not how do I produce XML — it is is this XML actually valid.

This post is about validating e-invoices from Python, and about the specific failure modes you will hit.

The five failures you will actually see

1. Missing buyer reference (BT-10). Public-sector buyers require a Leitweg-ID. Without it the invoice cannot be routed. Rule: BR-DE-15.

2. Missing seller tax identifier (BT-31 / BT-32). A commercial-register number (BT-30) does not satisfy this. The seller needs either a VAT ID or a tax number. Rule: BR-DE-16.

3. Tax category S with 0 %. "No VAT" is not "standard rate at zero". For zero you need E, Z or AE — and category E additionally requires a written exemption reason (BT-120). Rules: BR-S-05, BR-E-10.

4. Totals that do not add up. Net per tax rate × rate must equal the tax amount, and net + tax must equal the payable amount. Document-level discounts break this quietly. Rules: BR-CO-13, BR-CO-15.

5. Wrong type code or missing payment means. InvoiceTypeCode must be present (380 invoice, 381 credit note, …) and payment means (BT-81) is mandatory in XRechnung.

Validating without dependencies

Most validation libraries pull in a schema validator plus a pile of XML tooling. If you only need a yes/no plus the rule IDs, a plain HTTP POST is enough — and it is the same rule engine the web validator uses:

from xrechnung_pruefer import validate, is_valid, error_codes

with open("rechnung.xml", "rb") as f:
    result = validate(f.read())

if not is_valid(result):
    print(error_codes(result))        # ['BR-DE-15', 'BR-DE-16']
    for fehler in result["fehler"]:
        print(fehler["code"], fehler["text"])
Enter fullscreen mode Exit fullscreen mode

xrechnung-pruefer is a small client + CLI I published: zero dependencies (stdlib only), UBL and CII, and it returns the official rule identifiers, not a generic "invalid document".

pip install xrechnung-pruefer
xrechnung-pruefer rechnungen/ --json
Enter fullscreen mode Exit fullscreen mode

Exit codes are CI-friendly: 0 all valid, 1 rule violations, 2 file/network error.

What the response looks like

{
  "ok": true,
  "status": "FEHLER",
  "rechnung": {
    "nummer": "RE-2026-0042",
    "datum": "2026-11-14",
    "waehrung": "EUR",
    "betrag": 1588.39
  },
  "fehler": [
    {"code": "BR-DE-15", "text": "Buyer Reference / Leitweg-ID fehlt (BT-10)."},
    {"code": "BR-DE-16", "text": "USt-IdNr. (BT-31) oder Steuernummer (BT-32) fehlt."}
  ]
}
Enter fullscreen mode Exit fullscreen mode

You get the parsed invoice header for free, which is handy for logging: you can write "invoice RE-2026-0042 failed with BR-DE-16" instead of "something was invalid".

Honest limits

  • Only XML is validated. A scanned PDF without embedded XML cannot be checked — that is a technical boundary, not a policy.
  • This is a rule check against EN 16931 and the German CIUS. It is not a certification and not tax advice.
  • The default endpoint is the hosted API at xrechnungprofi.de (free tier, paid plans above it); --endpoint / endpoint= lets you point it at any compatible service, and the response format is documented in the README so you are not locked in.

Links

Top comments (0)