Skip to content

Creazione di fatture da JSON

POST /v1/invoices/generate?target=… crea una fattura elettronica a partire dai Suoi dati. Invii la fattura come JSON nel modello canonico EN 16931 — la stessa struttura restituita da POST /v1/invoices/parse, pubblicata come components.schemas.Invoice nel documento OpenAPI.

target Risultato
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, profilo EN 16931 (predefinito) o XRECHNUNG tramite 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);

Cosa imposta il servizio e cosa non fa mai

  • specificationId (BT-24) segue il target; un valore presente nel Suo JSON viene sostituito.
  • businessProcess (BT-23) assume per impostazione predefinita il processo di fatturazione Peppol per XRechnung e Peppol quando non ne invia uno; la risposta lo elenca in warnings con derived: true.
  • Nient'altro viene compilato. Una Leitweg-ID, un indirizzo o un'aliquota IVA mancanti restano mancanti e la chiamata fallisce.

Quando i dati non bastano

Il documento viene validato con le regole ufficiali del target prima di essere restituito. Se non le supera, riceve 422 generated_document_invalid; ogni segnalazione ha fields, i percorsi JSON coinvolti — ad esempio BR-DE-15 → buyerReference. Se il JSON stesso non corrisponde al modello, riceve 422 invalid_invoice_data con issues[].path.

Tramite MCP la stessa funzione è lo strumento create_invoice: un agente può creare il documento, leggere fields, chiedere all'utente i dati mancanti e riprovare.

Fatturazione e archiviazione

Ogni documento creato conta come un documento, come una validazione o una conversione. Con store=true (o con l'impostazione predefinita della Sua organizzazione) il documento creato viene conservato nella cronologia delle fatture per il periodo di conservazione del Suo piano.

Fissare il set di regole

Tutti gli endpoint per i documenti accettano rules=<version> (veda validation.ruleset.version, attualmente 2026-11). Una richiesta per un set di regole non disponibile viene rifiutata con 422 unsupported_rules_version invece di essere verificata con regole diverse.