{
  "name": "facturai: Webhook to E-Rechnung PDF",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "facturai-e-rechnung-pdf",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "facturai-webhook-input",
      "name": "Receive invoice input",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "webhookId": "facturai-e-rechnung-pdf"
    },
    {
      "parameters": {
        "jsCode": "const source = $input.first().json.body ?? $input.first().json;\nconst buyer = source.buyer ?? {};\nconst address = buyer.address ?? {};\nconst lines = Array.isArray(source.lines) ? source.lines : [];\n\nreturn [\n  {\n    json: {\n      invoice_input: {\n        invoice_date: source.invoice_date,\n        due_date: source.due_date,\n        currency: source.currency || 'EUR',\n        buyer: {\n          name: buyer.name,\n          address: {\n            line_one: address.line_one,\n            postcode: address.postcode,\n            city: address.city,\n            country: address.country || 'DE'\n          },\n          vat_id: buyer.vat_id ?? null,\n          buyer_reference: buyer.buyer_reference ?? null,\n          contact_email: buyer.contact_email ?? null\n        },\n        lines: lines.map((line) => ({\n          name: line.name,\n          description: line.description ?? null,\n          quantity: String(line.quantity ?? '1'),\n          unit: line.unit || 'C62',\n          unit_price: String(line.unit_price ?? line.price ?? '0.00'),\n          vat_rate: String(line.vat_rate ?? '19.00')\n        })),\n        allowances: Array.isArray(source.allowances) ? source.allowances : [],\n        charges: Array.isArray(source.charges) ? source.charges : [],\n        number_prefix: source.number_prefix ?? null\n      }\n    }\n  }\n];"
      },
      "id": "facturai-normalize-input",
      "name": "Normalize to Facturai schema",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-invoice-date",
              "leftValue": "={{ $json.invoice_input.invoice_date }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-due-date",
              "leftValue": "={{ $json.invoice_input.due_date }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-buyer",
              "leftValue": "={{ $json.invoice_input.buyer.name }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-address-line",
              "leftValue": "={{ $json.invoice_input.buyer.address.line_one }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-address-postcode",
              "leftValue": "={{ $json.invoice_input.buyer.address.postcode }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-address-city",
              "leftValue": "={{ $json.invoice_input.buyer.address.city }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            },
            {
              "id": "check-lines",
              "leftValue": "={{ $json.invoice_input.lines }}",
              "rightValue": "",
              "operator": {
                "type": "array",
                "operation": "notEmpty"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "facturai-check-required-fields",
      "name": "Check required fields",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://fakturai.de/api/v1/invoices",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.FACTURAI_API_KEY || 'replace-with-your-api-key' }}"
            },
            {
              "name": "Accept",
              "value": "application/pdf"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.invoice_input) }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "data"
            }
          },
          "retry": {
            "enabled": true,
            "maxTries": 3
          }
        }
      },
      "id": "facturai-create-invoice",
      "name": "Create E-Rechnung PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        980,
        240
      ]
    },
    {
      "parameters": {
        "respondWith": "binary",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/pdf"
              },
              {
                "name": "Content-Disposition",
                "value": "=attachment; filename=\"facturai-e-rechnung-{{ $('Normalize to Facturai schema').item.json.invoice_input.number_prefix || 'download' }}.pdf\""
              }
            ]
          }
        }
      },
      "id": "facturai-return-pdf",
      "name": "Return PDF",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1220,
        240
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseCode": 400,
        "responseBody": "={ \"error\": \"missing_required_invoice_fields\", \"detail\": \"Send invoice_date, due_date, currency, buyer.name, buyer.address.line_one, buyer.address.postcode, buyer.address.city, and at least one line item. See https://fakturai.de/schemas/invoice-input/v1.json\" }"
      },
      "id": "facturai-return-validation-error",
      "name": "Return validation error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        980,
        380
      ]
    }
  ],
  "connections": {
    "Receive invoice input": {
      "main": [
        [
          {
            "node": "Normalize to Facturai schema",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize to Facturai schema": {
      "main": [
        [
          {
            "node": "Check required fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check required fields": {
      "main": [
        [
          {
            "node": "Create E-Rechnung PDF",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Return validation error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create E-Rechnung PDF": {
      "main": [
        [
          {
            "node": "Return PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {
    "Receive invoice input": [
      {
        "json": {
          "invoice_date": "2026-06-01",
          "due_date": "2026-06-15",
          "currency": "EUR",
          "buyer": {
            "name": "Acme GmbH",
            "address": {
              "line_one": "Beispielweg 42",
              "postcode": "80331",
              "city": "Muenchen",
              "country": "DE"
            },
            "vat_id": "DE123456789",
            "contact_email": "buchhaltung@example.com"
          },
          "lines": [
            {
              "name": "Software development",
              "description": "Implementation work",
              "quantity": "8",
              "unit": "HUR",
              "unit_price": "150.00",
              "vat_rate": "19.00"
            }
          ],
          "number_prefix": "RE-2026"
        }
      }
    ]
  },
  "meta": {
    "templateId": "facturai-webhook-to-e-rechnung-pdf-v1",
    "description": "Receives FacturaiInvoiceInput JSON by webhook, calls the public facturai invoice API, and returns the generated EN 16931 / ZUGFeRD PDF."
  },
  "_readme": {
    "setup": [
      "Import this workflow into n8n.",
      "Set FACTURAI_API_KEY in the n8n environment, or replace the X-API-Key header value in the HTTP Request node.",
      "Activate the workflow and POST a body matching https://fakturai.de/schemas/invoice-input/v1.json to the webhook URL."
    ],
    "api_docs": "https://fakturai.de/docs/api",
    "schema_ref": "https://fakturai.de/schemas/invoice-input/v1.json"
  }
}
