Metadata-Version: 2.5
Name: xrechnung
Version: 0.1.1
Summary: Python client for the xrechnung.dev API: generate, validate (official KoSIT rules) and parse XRechnung and ZUGFeRD e-invoices. Zero dependencies, no JVM required.
Project-URL: Homepage, https://xrechnung.dev
Project-URL: Documentation, https://xrechnung.dev/xrechnung-api/
Project-URL: API reference, https://xrechnung.dev/docs
Project-URL: Error codes, https://xrechnung.dev/xrechnung-fehlercodes/
Author-email: "xrechnung.dev" <dev@xrechnung.dev>
License-Expression: MIT
License-File: LICENSE
Keywords: e-rechnung,einvoice,en16931,factur-x,kosit,validator,xrechnung,zugferd
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Natural Language :: German
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Office/Business :: Financial :: Accounting
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# xrechnung

Python client for the [xrechnung.dev](https://xrechnung.dev) REST API: **generate, validate and parse**
German EN 16931 e-invoices — XRechnung 3.0 XML and ZUGFeRD 2.x PDF/A-3 — with one function call.

- **No JVM required.** Validation runs server-side; you don't install the KoSIT validator or Java.
- **Always validated with the current official KoSIT rules (XRechnung 3.0.2, Schematron 2.6.0).**
  Every generated document is checked before it is returned, so you never receive an invalid invoice.
- **Zero dependencies.** Standard library only (`urllib`), Python 3.9+, fully typed.
- **Free tier:** 10 documents per month without an API key.
- Every error links to a plain-language explanation, e.g. `BR-DE-15` → https://xrechnung.dev/fehler/br-de-15/

```bash
pip install xrechnung
```

## Validate

```python
import xrechnung

result = xrechnung.validate("rechnung.xml")      # path, or raw bytes; XML or ZUGFeRD PDF
print(result.valid, result.scenario)
for e in result.errors:
    print(e.code, e.message, e.location)
    print("  how to fix:", e.doc_url)             # https://xrechnung.dev/fehler/br-de-15/
```

`result.warnings` has the same shape; `result.raw` is the full JSON report.

## Generate

```python
invoice = {
    "number": "RE-2026-0001", "issue_date": "2026-09-07",
    "seller": {"name": "Beispiel GmbH", "vat_id": "DE123456789", "email": "rechnung@beispiel.de",
               "phone": "+49 30 1234567", "contact_name": "Erika Muster",
               "address": {"street": "Musterstr. 1", "postcode": "10115", "city": "Berlin"}},
    "buyer": {"name": "Kunde AG", "email": "buchhaltung@kunde.de", "address": {"street": "Kundenweg 9", "postcode": "80331", "city": "München"}},
    "buyer_reference": "04011000-12345-67",
    "lines": [{"description": "Beratung", "quantity": "10", "unit": "HUR", "unit_price": "120.00", "vat_rate": "19"}],
    "payment": {"iban": "DE02120300000000202051", "due_date": "2026-09-21", "terms": "14 Tage netto"},
}

xml = xrechnung.generate(invoice)                                   # XRechnung (CII XML) as bytes
pdf = xrechnung.generate(invoice, format="zugferd", language="en")  # ZUGFeRD PDF/A-3 as bytes
report = xrechnung.generate(invoice, output="json")                 # dict: base64 file + validation report
```

`Decimal` and `date` values are serialised as strings, so you can pass them directly.
The invoice fields follow the [OpenAPI schema](https://xrechnung.dev/docs) (tax categories S/Z/E/AE/K/G/O,
`small_business` for §19 UStG, credit notes via `type_code` 381 and `preceding_invoice_number`).

## Parse

```python
data = xrechnung.parse("eingang.pdf")   # {"syntax": "CII", "number": ..., "seller": {...}, "lines": [...], ...}
```

## API key, usage, errors

```python
client = xrechnung.Client(api_key="xr_live_...")   # or set XRECHNUNG_API_KEY
client.validate(b"<Invoice ...>")
client.usage()                                      # this month's usage
```

| HTTP | Exception | Meaning |
|------|-----------|---------|
| 401 | `xrechnung.AuthenticationError` | invalid or inactive API key |
| 402 | `xrechnung.PaymentRequiredError` | payment / plan required |
| 429 | `xrechnung.RateLimitError` | monthly limit reached or too many requests |
| other | `xrechnung.XRechnungError` | base class; `.status`, `.message` (the server's text), `.body` |

Limits: 5 MB per document, 60 s timeout. Plans: Free 10 · Starter 200 (€19) · Pro 1,000 (€40) · Business 5,000 (€99) · Unlimited (€499)
documents per month, net prices — see https://xrechnung.dev/#pricing.

The same client is also published as [`zugferd`](https://pypi.org/project/zugferd/) (`import zugferd`).

---

## Deutsch

Python-Client für die [xrechnung.dev](https://xrechnung.dev)-API: **E-Rechnungen erzeugen, prüfen und lesen**
— XRechnung 3.0 (XML) und ZUGFeRD 2.x (PDF/A-3).

- **Keine JVM nötig.** Sie installieren weder Java noch den KoSIT-Validator.
- **Immer mit den aktuellen offiziellen KoSIT-Regeln geprüft (XRechnung 3.0.2, Schematron 2.6.0).**
  Jede erzeugte Rechnung wird vor der Auslieferung validiert.
- **Keine Abhängigkeiten**, nur Standardbibliothek, Python ab 3.9.
- **Kostenlos:** 10 Dokumente pro Monat ohne API-Schlüssel.
- Zu jedem Fehlercode gibt es eine Erklärung mit Lösung, z. B. https://xrechnung.dev/fehler/br-de-15/

```python
import xrechnung

ergebnis = xrechnung.validate("rechnung.xml")
if not ergebnis.valid:
    for f in ergebnis.errors:
        print(f.code, f.message, f.doc_url)

pdf = xrechnung.generate(rechnung, format="zugferd")   # ZUGFeRD-PDF
open("RE-2026-0001.pdf", "wb").write(pdf)
```

Fehler 401 (Schlüssel ungültig), 402 (Zahlung erforderlich) und 429 (Monatslimit erreicht) werden als
eigene Exceptions mit der Meldung des Servers ausgelöst.

## License

MIT
