Zum Inhalt springen

REST-API für Entwickler

Anleitung: Ihre erste E-Rechnung

Vom API-Schlüssel zum ZUGFeRD-PDF und zur XRechnung-XML, mit Code und erwarteter Antwort für jeden Schritt.

Inhalt

Ziel und Voraussetzungen

Am Ende haben Sie eine Rechnung per API angelegt und als ZUGFeRD-PDF und als XRechnung-XML gespeichert.

  • Ein Konto mit API-Zugang. Alle Pläne enthalten die API im Rahmen ihrer Grenzen; siehe Pläne und API-Zugang.
  • Ein API-Schlüssel (Schritt 1).
  • curl und jq oder Python 3 mit dem Paket requests (pip install requests).

1. API-Schlüssel setzen

Legen Sie in Ihrem Konto unter API-Schlüssel einen Schlüssel an. Er wird nur einmal angezeigt; kopieren Sie ihn dann. Halten Sie ihn aus dem Quellcode heraus: Die Beispiele lesen ihn aus der Umgebungsvariable API_KEY.

curl (Shell)
read -rs API_KEY && export API_KEY  # paste the key; it does not end up in the shell history

curl -sS "https://www.e-rechnungsfreund.de/api/v1/account/" -H "Authorization: Api-Key $API_KEY" -o /dev/null -w "%{http_code}\n"
# 200 = key accepted, 401 = key missing, invalid, expired or revoked
Python (requests)
import os

import requests

BASE_URL = "https://www.e-rechnungsfreund.de"
HEADERS = {"Authorization": f"Api-Key {os.environ['API_KEY']}"}

response = requests.get(f"{BASE_URL}/api/v1/account/", headers=HEADERS, timeout=30)
print(response.status_code)  # 200 = key accepted, 401 = key missing, invalid, expired or revoked

Erwartetes Ergebnis: 200. Bei 401 prüfen Sie den Schlüssel und den Header.

2. Konto prüfen

GET /api/v1/account/ zeigt den Plan, die Berechtigungen und die restlichen Kontingente. Der Aufruf zählt in kein Kontingent. Zum Anlegen von Rechnungen muss permissions.api_access den Wert true haben.

curl (Shell)
curl -sS "https://www.e-rechnungsfreund.de/api/v1/account/" \
  -H "Authorization: Api-Key $API_KEY"
Python (requests)
account = requests.get(f"{BASE_URL}/api/v1/account/", headers=HEADERS, timeout=30)
account.raise_for_status()
data = account.json()
if not data["permissions"]["api_access"]:
    raise SystemExit("This account can only check and read via the API.")
invoices = data["quotas"]["invoices"]
print(f"{invoices['remaining']} of {invoices['limit']} invoices left until {invoices['reset_date']}")
Beispielantwort: GET /api/v1/account/ → 200 OK
JSON
{
  "plan": {
    "code": "free",
    "name": "Free"
  },
  "permissions": {
    "api_access": true,
    "api_validation_access": true,
    "tax_advisor_export": false,
    "invoice_emails": true
  },
  "quotas": {
    "invoices": {
      "used": 1,
      "limit": 5,
      "remaining": 4,
      "reset_date": "2027-01-01"
    },
    "validations": {
      "used": 3,
      "limit": 30,
      "remaining": 27,
      "reset_date": "2027-01-01"
    },
    "party_templates": {
      "used": 1,
      "limit": 3,
      "remaining": 2,
      "reset_date": null
    }
  },
  "party_templates": {
    "seller": 1,
    "buyer": 0,
    "has_seller_template": true
  }
}

3. Rechnung anlegen

Speichern Sie die Rechnungsdaten als invoice.json und senden Sie sie an POST /api/v1/invoices/. Die Antwort enthält die id der Rechnung und die berechneten Summen. Jede Rechnung zählt in die Rechnungen pro Monat Ihres Plans.

invoice.json Komplettes Skript: invoice.json
{
  "invoice_number": "RE-2026-001",
  "issue_date": "2026-10-01",
  "delivery_date": "2026-10-01",
  "type_code": "380",
  "currency_code": "EUR",
  "payment_terms": "Zahlbar innerhalb von 14 Tagen ohne Abzug",
  "seller": {
    "name": "Muster GmbH",
    "street": "Musterstraße 1",
    "postal_code": "10115",
    "city": "Berlin",
    "country_code": "DE",
    "vat_id": "DE123456789",
    "contact_name": "Max Mustermann",
    "contact_phone": "+49 30 12345678",
    "contact_email": "rechnung@muster.example"
  },
  "buyer": {
    "name": "Käufer AG",
    "street": "Käuferstraße 5",
    "postal_code": "80331",
    "city": "München",
    "country_code": "DE",
    "vat_id": "DE987654321"
  },
  "items": [
    {
      "line_id": "1",
      "name": "Beratungsleistung",
      "quantity": "8",
      "unit_code": "HUR",
      "net_price": "95.00",
      "tax_category": "S",
      "tax_rate": "19.00"
    }
  ],
  "tax_breakdowns": [
    {
      "tax_category": "S",
      "tax_rate": "19.00",
      "basis_amount": "760.00",
      "tax_amount": "144.40"
    }
  ]
}
curl (Shell)
INVOICE_ID=$(curl -sS -X POST "https://www.e-rechnungsfreund.de/api/v1/invoices/" \
  -H "Authorization: Api-Key $API_KEY" \
  -H "Content-Type: application/json" \
  -d @invoice.json | jq -r .id)
echo "$INVOICE_ID"
Python (requests)
import json

with open("invoice.json", encoding="utf-8") as f:
    invoice = json.load(f)

created = requests.post(f"{BASE_URL}/api/v1/invoices/", json=invoice, headers=HEADERS, timeout=30)
if created.status_code == 400:
    raise SystemExit(created.json()["validation_errors"])  # see the guide on fixing errors
created.raise_for_status()
invoice_id = created.json()["id"]
Beispielantwort: POST /api/v1/invoices/ → 201 Created

Auszug: Die Antwort enthält alle Felder der Rechnung, auch die berechneten Summen.

JSON
{
  "id": "3ff6fce7-0c0f-4726-8288-92947decc8aa",
  "is_xrechnung": false,
  "profile": "EN16931",
  "invoice_number": "RE-2026-001",
  "issue_date": "2026-10-01",
  "type_code": "380",
  "currency_code": "EUR",
  "line_total_amount": "760.0000",
  "tax_basis_total_amount": "760.0000",
  "tax_total_amount": "144.4000",
  "grand_total_amount": "904.4000",
  "due_payable_amount": "904.4000",
  "is_paid": false,
  "export_status": "not_generated",
  "created_at": "2026-10-01T09:30:00.000000Z",
  "items": [
    {
      "id": "8be0e456-ae96-4a52-9139-f5359580fec3",
      "line_id": "1",
      "name": "Beratungsleistung",
      "quantity": "8.0000",
      "unit_code": "HUR",
      "net_price": "95.0000",
      "line_total_amount": "760.0000",
      "tax_rate": "19.00",
      "tax_category": "S"
    }
  ]
}

Bei 400 fehlt ein Feld oder es ist ungültig; Anleitung: Fehler beheben zeigt, wie Sie die Antwort lesen.

4. ZUGFeRD-PDF laden

POST /api/v1/invoices/{invoice_id}/generate_zugferd_pdf/ erzeugt das ZUGFeRD-PDF (PDF/A-3 mit eingebettetem XML) und prüft es. Die Antwort ist die Datei selbst. Findet die Prüfung Fehler, erhalten Sie statt der Datei 400 mit den Meldungen in details.

curl (Shell)
curl -sS -X POST "https://www.e-rechnungsfreund.de/api/v1/invoices/$INVOICE_ID/generate_zugferd_pdf/" \
  -H "Authorization: Api-Key $API_KEY" \
  -o invoice.pdf -w "%{http_code}\n"
Python (requests)
pdf = requests.post(
    f"{BASE_URL}/api/v1/invoices/{invoice_id}/generate_zugferd_pdf/",
    headers=HEADERS,
    timeout=120,
)
if pdf.status_code == 400:
    raise SystemExit(pdf.json())  # "details" lists the messages of the check
pdf.raise_for_status()
with open("invoice.pdf", "wb") as f:
    f.write(pdf.content)
Beispielantwort: POST /api/v1/invoices/{invoice_id}/generate_zugferd_pdf/ → 200 OK
JSON
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Disposition: attachment; filename="ZUGFeRD_RE-2026-001.pdf"

Die Datei wird mit der Rechnung gespeichert; GET /api/v1/invoices/{invoice_id}/download/ lädt sie erneut, ohne sie neu zu erzeugen.

5. XRechnung-XML laden

Für eine XRechnung, z. B. an Behörden, legen Sie die Rechnung mit "profile": "XRECHNUNG" an, mit der Leitweg-ID der Behörde in buyer_reference (hier der Testwert 04011000-12345-03), den elektronischen Adressen von Verkäufer und Käufer und der Zahlungsart:

xrechnung-invoice.json Komplettes Skript: xrechnung-invoice.json
{
  "invoice_number": "RE-2026-002",
  "issue_date": "2026-10-01",
  "delivery_date": "2026-10-01",
  "type_code": "380",
  "currency_code": "EUR",
  "payment_terms": "Zahlbar innerhalb von 14 Tagen ohne Abzug",
  "profile": "XRECHNUNG",
  "specification_identifier": "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0",
  "business_process_type": "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0",
  "buyer_reference": "04011000-12345-03",
  "seller": {
    "name": "Muster GmbH",
    "street": "Musterstraße 1",
    "postal_code": "10115",
    "city": "Berlin",
    "country_code": "DE",
    "vat_id": "DE123456789",
    "contact_name": "Max Mustermann",
    "contact_phone": "+49 30 12345678",
    "contact_email": "rechnung@muster.example",
    "electronic_address": "rechnung@muster.example",
    "electronic_address_scheme": "EM"
  },
  "buyer": {
    "name": "Stadtverwaltung Musterstadt",
    "street": "Rathausplatz 1",
    "postal_code": "80331",
    "city": "München",
    "country_code": "DE",
    "electronic_address": "991-33333TEST-33",
    "electronic_address_scheme": "0204"
  },
  "payment_means": [
    {
      "type_code": "58",
      "iban": "DE02120300000000202051",
      "account_name": "Muster GmbH"
    }
  ],
  "items": [
    {
      "line_id": "1",
      "name": "Beratungsleistung",
      "quantity": "8",
      "unit_code": "HUR",
      "net_price": "95.00",
      "tax_category": "S",
      "tax_rate": "19.00"
    }
  ],
  "tax_breakdowns": [
    {
      "tax_category": "S",
      "tax_rate": "19.00",
      "basis_amount": "760.00",
      "tax_amount": "144.40"
    }
  ]
}

Danach liefert POST /api/v1/invoices/{invoice_id}/generate_xrechnung/ die XRechnung als XML. Eine XRechnung hat kein PDF; generate_zugferd_pdf antwortet dafür mit 400.

curl (Shell)
XRECHNUNG_ID=$(curl -sS -X POST "https://www.e-rechnungsfreund.de/api/v1/invoices/" \
  -H "Authorization: Api-Key $API_KEY" \
  -H "Content-Type: application/json" \
  -d @xrechnung-invoice.json | jq -r .id)

curl -sS -X POST "https://www.e-rechnungsfreund.de/api/v1/invoices/$XRECHNUNG_ID/generate_xrechnung/" \
  -H "Authorization: Api-Key $API_KEY" \
  -o invoice.xml -w "%{http_code}\n"
Python (requests)
with open("xrechnung-invoice.json", encoding="utf-8") as f:
    xrechnung = json.load(f)

created = requests.post(f"{BASE_URL}/api/v1/invoices/", json=xrechnung, headers=HEADERS, timeout=30)
created.raise_for_status()
xrechnung_id = created.json()["id"]

xml = requests.post(
    f"{BASE_URL}/api/v1/invoices/{xrechnung_id}/generate_xrechnung/",
    headers=HEADERS,
    timeout=120,
)
xml.raise_for_status()
with open("invoice.xml", "wb") as f:
    f.write(xml.content)
Beispielantwort: POST /api/v1/invoices/{invoice_id}/generate_xrechnung/ → 200 OK
JSON
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Content-Disposition: attachment; filename="XRechnung_RE-2026-002.xml"

Vollständiges Skript auf GitHub: first_invoice.sh, first_invoice.py

Nächste Schritte