{
  "name": "Invoice Processing Pipeline",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "has:attachment subject:invoice"
        },
        "options": {
          "downloadAttachments": true
        }
      },
      "id": "91e61a71-7403-469b-bcfd-825d48a28763",
      "name": "Invoice Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "notes": "Watches an inbox for invoices. Swap for Google Drive trigger if invoices land in a folder."
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "attachment_0",
        "options": {}
      },
      "id": "93f9f60b-ea17-44ab-bb92-6df261f383d8",
      "name": "Extract PDF Text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{ $credentials.anthropicApi.apiKey }}"
            },
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"claude-sonnet-4-20250514\",\n  \"max_tokens\": 800,\n  \"system\": \"Extract invoice data. Return ONLY JSON: {vendor, invoice_number, date, due_date, currency, subtotal, tax, total, line_items:[{desc, qty, unit_price, amount}]}. Use null for missing fields.\",\n  \"messages\": [{\"role\": \"user\", \"content\": \"{{ $json.text }}\"}]\n}",
        "options": {}
      },
      "id": "1ff1f281-953f-44c3-b847-1e2b8e3e923a",
      "name": "Claude Extract Fields",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsScript": "const d = JSON.parse($input.first().json.content[0].text);\n// basic validation\nd._valid = !!(d.vendor && d.total);\nd._anomaly = (d.subtotal && d.tax && d.total) ? (Math.abs((d.subtotal + d.tax) - d.total) > 0.02) : false;\nreturn [{ json: d }];"
      },
      "id": "fd1a9d29-044a-409c-92a2-cc0c88491dcf",
      "name": "Parse + Validate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {},
          "conditions": [
            {
              "leftValue": "={{ $json._anomaly }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true"
              }
            }
          ]
        }
      },
      "id": "f5b4784c-dee7-4bf2-962c-89686d2f9df1",
      "name": "Anomaly?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "finance",
          "mode": "name"
        },
        "text": "=:warning: Invoice math mismatch from {{ $json.vendor }} ({{ $json.invoice_number }}). Subtotal+tax != total. Needs human review.",
        "otherOptions": {}
      },
      "id": "b469d6de-437f-4860-b4ae-77dfc924c9a0",
      "name": "Flag For Review",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2,
      "position": [
        1360,
        180
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Invoices",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Vendor": "={{ $json.vendor }}",
            "Invoice #": "={{ $json.invoice_number }}",
            "Date": "={{ $json.date }}",
            "Total": "={{ $json.total }}",
            "Currency": "={{ $json.currency }}"
          }
        },
        "options": {}
      },
      "id": "9d92812f-e21e-4c8a-bc0b-64e2385b849d",
      "name": "Append To Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1360,
        420
      ],
      "notes": "Replace with QuickBooks/Xero node for real accounting sync."
    }
  ],
  "connections": {
    "Invoice Email": {
      "main": [
        [
          {
            "node": "Extract PDF Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF Text": {
      "main": [
        [
          {
            "node": "Claude Extract Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Extract Fields": {
      "main": [
        [
          {
            "node": "Parse + Validate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse + Validate": {
      "main": [
        [
          {
            "node": "Anomaly?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anomaly?": {
      "main": [
        [
          {
            "node": "Flag For Review",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append To Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flag For Review": {
      "main": [
        [
          {
            "node": "Append To Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "saveManualExecutions": true
  },
  "versionId": "2f77fe8d-461f-4b8a-ba57-4d9de9d11a3b",
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Auto-processes invoice emails: extracts PDF text, Claude pulls structured fields (vendor, totals, line items), validates the math, flags anomalies to Finance in Slack, and logs clean invoices to your accounting sheet."
  },
  "tags": [
    {
      "name": "finance"
    },
    {
      "name": "ai"
    },
    {
      "name": "ops"
    }
  ],
  "pinData": {}
}