{
  "name": "Invoice extraction to Google Sheets (TestedFlows)",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyHour"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "has:attachment filename:pdf (invoice OR receipt OR bill)"
        },
        "options": {
          "downloadAttachments": true
        }
      },
      "name": "Invoice email arrives",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        300
      ],
      "id": "b77fec16-ccb4-44ac-8ef4-e8349da2ecf5"
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "attachment_0",
        "options": {}
      },
      "name": "Read PDF text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "id": "8f6210fb-109c-4538-968d-6f4e4c9da2fa"
    },
    {
      "parameters": {
        "text": "={{ $json.text }}",
        "attributes": {
          "attributes": [
            {
              "name": "vendor",
              "description": "Company that issued the invoice",
              "required": true
            },
            {
              "name": "invoice_number",
              "description": "Invoice or receipt number",
              "required": true
            },
            {
              "name": "invoice_date",
              "type": "date",
              "description": "Date the invoice was issued",
              "required": true
            },
            {
              "name": "due_date",
              "type": "date",
              "description": "Payment due date, if shown"
            },
            {
              "name": "currency",
              "description": "Three-letter currency code, e.g. USD, EUR, GBP",
              "required": true
            },
            {
              "name": "subtotal",
              "type": "number",
              "description": "Amount before tax"
            },
            {
              "name": "tax",
              "type": "number",
              "description": "Total tax amount"
            },
            {
              "name": "total",
              "type": "number",
              "description": "Total amount due, including tax",
              "required": true
            }
          ]
        },
        "options": {
          "systemPromptTemplate": "You extract data from invoices and receipts. Use only values printed in the document. If a field is not present, leave it empty. Never calculate or guess amounts."
        }
      },
      "name": "Extract invoice fields",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        480,
        300
      ],
      "id": "14c8ccd9-e5d5-48aa-850a-1579b7873c16"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-mini"
        },
        "options": {}
      },
      "name": "OpenAI model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        480,
        520
      ],
      "id": "99b1fb1d-3c75-406b-875b-d82ab093ad5d"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b1",
              "name": "received",
              "value": "={{ $now.toISODate() }}",
              "type": "string"
            },
            {
              "id": "b2",
              "name": "vendor",
              "value": "={{ $json.output.vendor }}",
              "type": "string"
            },
            {
              "id": "b3",
              "name": "invoice_number",
              "value": "={{ $json.output.invoice_number }}",
              "type": "string"
            },
            {
              "id": "b4",
              "name": "invoice_date",
              "value": "={{ $json.output.invoice_date }}",
              "type": "string"
            },
            {
              "id": "b5",
              "name": "due_date",
              "value": "={{ $json.output.due_date }}",
              "type": "string"
            },
            {
              "id": "b6",
              "name": "currency",
              "value": "={{ $json.output.currency }}",
              "type": "string"
            },
            {
              "id": "b7",
              "name": "subtotal",
              "value": "={{ $json.output.subtotal }}",
              "type": "number"
            },
            {
              "id": "b8",
              "name": "tax",
              "value": "={{ $json.output.tax }}",
              "type": "number"
            },
            {
              "id": "b9",
              "name": "total",
              "value": "={{ $json.output.total }}",
              "type": "number"
            },
            {
              "id": "b10",
              "name": "email_subject",
              "value": "={{ $('Invoice email arrives').item.json.subject }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "name": "Prepare invoice row",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        800,
        300
      ],
      "id": "20d5084d-dd9d-4528-a970-020a9fd7b95a"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {},
        "authentication": "oAuth2"
      },
      "name": "Add row to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1040,
        300
      ],
      "id": "a109edc3-33b7-4dd7-87f4-17b7a33a5a73"
    }
  ],
  "connections": {
    "Invoice email arrives": {
      "main": [
        [
          {
            "node": "Read PDF text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read PDF text": {
      "main": [
        [
          {
            "node": "Extract invoice fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract invoice fields",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Extract invoice fields": {
      "main": [
        [
          {
            "node": "Prepare invoice row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare invoice row": {
      "main": [
        [
          {
            "node": "Add row to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "https://testedflows.com/templates/"
  }
}