Skip to content

Creating invoices from JSON

POST /v1/invoices/generate?target=… creates an e-invoice from your own data. Send the invoice as JSON in the canonical EN 16931 model — the same shape POST /v1/invoices/parse returns, published as components.schemas.Invoice in the OpenAPI document.

target Result
xrechnung-ubl, xrechnung-cii XRechnung 3.0
en16931-ubl, en16931-cii EN 16931 core
peppol-bis-billing Peppol BIS Billing 3.0 (UBL)
zugferd-facturx ZUGFeRD / Factur-X PDF/A-3b, profile EN 16931 (default) or XRECHNUNG via profile=

generate.ts

// Create an XRechnung from your own invoice data (Node 20+). The JSON follows the canonical
// EN 16931 model; here it is built from minimal fields. Missing data is reported, never invented.
import { writeFile } from "node:fs/promises";

const invoice = {
  documentKind: "invoice",
  id: "RE-2026-0042",
  issueDate: "2026-11-22",
  typeCode: "380",
  currency: "EUR",
  dueDate: "2026-12-22",
  buyerReference: "04011000-12345-34", // Leitweg-ID of the public buyer
  seller: {
    name: "Muster Software GmbH",
    vatId: "DE123456789",
    electronicAddress: { value: "[email protected]", scheme: "EM" },
    address: { line1: "Hauptstraße 1", city: "Berlin", postcode: "10115", countryCode: "DE" },
    contact: { name: "Buchhaltung", phone: "+49 30 1234567", email: "[email protected]" },
  },
  buyer: {
    name: "Stadtverwaltung Musterstadt",
    electronicAddress: { value: "[email protected]", scheme: "EM" },
    address: { line1: "Rathausplatz 1", city: "Musterstadt", postcode: "12345", countryCode: "DE" },
  },
  payment: { meansCode: "58", creditTransfers: [{ accountId: "DE02120300000000202051" }] },
  paymentTerms: "Zahlbar innerhalb 30 Tagen ohne Abzug.",
  totals: { lineNet: "1000.00", taxExclusive: "1000.00", tax: "190.00", taxInclusive: "1190.00", payable: "1190.00" },
  vatBreakdown: [{ taxableAmount: "1000.00", taxAmount: "190.00", category: "S", rate: "19" }],
  lines: [
    {
      id: "1",
      quantity: "10",
      unitCode: "HUR",
      netAmount: "1000.00",
      price: { net: "100.00" },
      vat: { category: "S", rate: "19" },
      item: { name: "Softwareentwicklung" },
    },
  ],
};

const res = await fetch(`${process.env.EINV_BASE}/v1/invoices/generate?target=${process.argv[2] ?? "xrechnung-ubl"}&store=false`, {
  method: "POST",
  headers: { authorization: `Bearer ${process.env.EINV_KEY}`, "content-type": "application/json" },
  body: JSON.stringify({ invoice }),
});
const body = await res.json();
if (!res.ok) {
  console.log(body.error.code, body.error.message);
  // generated_document_invalid: each finding names the JSON fields to fill
  for (const f of body.error.details?.findings ?? []) console.log(f.ruleId, f.fields?.join(", "), f.message);
  // invalid_invoice_data: the JSON does not match the model
  for (const i of body.error.details?.issues ?? []) console.log(i.path, i.message);
  process.exit(1);
}
const file = body.target.encoding === "base64" ? "invoice.pdf" : "invoice-generated.xml";
await writeFile(file, body.target.encoding === "base64" ? Buffer.from(body.document, "base64") : body.document);
console.log(body.validation.status, body.target.format, file);

What the service sets, and what it never does

  • specificationId (BT-24) follows the target; a value in your JSON is replaced.
  • businessProcess (BT-23) defaults to the Peppol billing process for XRechnung and Peppol when you do not send one; the response lists it under warnings with derived: true.
  • Nothing else is filled in. A missing Leitweg-ID, address or VAT rate stays missing and the call fails.

When the data is not enough

The document is validated with the official rules of the target before it is returned. If it does not pass, you get 422 generated_document_invalid; every finding has fields, the JSON paths involved — for example BR-DE-15 → buyerReference. If the JSON itself does not match the model, you get 422 invalid_invoice_data with issues[].path.

Over MCP the same function is the tool create_invoice: an agent can create the document, read fields, ask the user for the missing data and try again.

Billing and storage

Each created document counts as one document, like a validation or conversion. With store=true (or your organization default) the created document is kept in the invoice history for the retention period of your plan.

Pinning the rule set

All document endpoints accept rules=<version> (see validation.ruleset.version, currently 2026-11). A request for a rule set that is not available is refused with 422 unsupported_rules_version instead of being checked with different rules.