{
  "name": "Invoice OCR to Google Sheets",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "invoice-ocr",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "iots0001-0001-4000-8000-000000000001",
      "name": "Receive Invoice Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "url": "={{ $json.body.invoiceUrl }}",
        "options": {
          "responseType": "arraybuffer",
          "timeout": 60000
        }
      },
      "id": "iots0001-0002-4000-8000-000000000002",
      "name": "Download Invoice PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {}
      },
      "id": "iots0001-0003-4000-8000-000000000003",
      "name": "Extract Invoice Text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [680, 300]
    },
    {
      "parameters": {
        "resource": "text",
        "operation": "message",
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "Extract the following fields from this invoice text and return ONLY valid JSON with these exact keys: vendor_name, invoice_number, invoice_date, due_date, subtotal, tax_amount, total_amount, currency, line_items (array of objects with description, quantity, unit_price, total). Use null for any field not found.\n\nInvoice text:\n{{ $json.text }}"
            }
          ]
        },
        "options": {
          "temperature": 0,
          "responseFormat": "json_object"
        }
      },
      "id": "iots0001-0004-4000-8000-000000000004",
      "name": "Parse Invoice Fields",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [900, 300]
    },
    {
      "parameters": {
        "jsCode": "const parsed = JSON.parse($input.item.json.message.content);\nreturn [{ json: { vendor_name: parsed.vendor_name, invoice_number: parsed.invoice_number, invoice_date: parsed.invoice_date, due_date: parsed.due_date, subtotal: parsed.subtotal, tax_amount: parsed.tax_amount, total_amount: parsed.total_amount, currency: parsed.currency || 'USD', line_items_count: (parsed.line_items || []).length, source_url: $('Receive Invoice Webhook').item.json.body.invoiceUrl, processed_at: new Date().toISOString() } }];"
      },
      "id": "iots0001-0005-4000-8000-000000000005",
      "name": "Flatten Invoice Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1120, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{ $env.INVOICE_SHEET_ID }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Invoices",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "iots0001-0006-4000-8000-000000000006",
      "name": "Append to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1340, 300]
    }
  ],
  "connections": {
    "Receive Invoice Webhook": {
      "main": [[{ "node": "Download Invoice PDF", "type": "main", "index": 0 }]]
    },
    "Download Invoice PDF": {
      "main": [[{ "node": "Extract Invoice Text", "type": "main", "index": 0 }]]
    },
    "Extract Invoice Text": {
      "main": [[{ "node": "Parse Invoice Fields", "type": "main", "index": 0 }]]
    },
    "Parse Invoice Fields": {
      "main": [[{ "node": "Flatten Invoice Data", "type": "main", "index": 0 }]]
    },
    "Flatten Invoice Data": {
      "main": [[{ "node": "Append to Google Sheets", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
