{
  "nodes": [
    {
      "id": "85702921-bc6d-4f5c-a1d5-429fb0b969ac",
      "name": "When clicking 'Execute workflow'",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -288,
        208
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "0455ad8a-32bd-4c3f-a944-02a4c9a60f74",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1808,
        720
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ec4fa0f9-5f94-4490-8404-0666e655f4bd",
      "name": "Convert to File",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        1312,
        224
      ],
      "parameters": {
        "options": {
          "fileName": "={{ $('Save base64').item.json.name }}"
        },
        "operation": "toBinary",
        "sourceProperty": "data"
      },
      "typeVersion": 1.1
    },
    {
      "id": "09323ac4-58c7-4260-a11b-9d06f133b29f",
      "name": "Switch Mime Type",
      "type": "n8n-nodes-base.switch",
      "position": [
        880,
        400
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "image",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "fcf37995-e2b2-466d-a8a8-aad25380f329",
                    "operator": {
                      "type": "string",
                      "operation": "startsWith"
                    },
                    "leftValue": "={{ $binary.data.mimeType }}",
                    "rightValue": "image/"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "pdf",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0853b815-fd5d-413d-9f0e-0d2fc8dbdade",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $binary.data.mimeType }}",
                    "rightValue": "application/pdf"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "f15cfb3e-630b-47da-8318-dc58289f9e07",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        304,
        384
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "33e127d5-6a47-4637-bcb2-d135a9dae63b",
      "name": "Set file identity",
      "type": "n8n-nodes-base.set",
      "position": [
        528,
        400
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f021928e-16df-4280-9e52-8c671e7e36d5",
              "name": "id",
              "type": "string",
              "value": "={{ $json.id }}"
            },
            {
              "id": "6cdca709-1b00-4970-b9bb-9c2184600a5e",
              "name": "name",
              "type": "string",
              "value": "={{ $json.name }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "89803795-7f66-495a-98fc-5024627a0242",
      "name": "Save base64",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        704,
        400
      ],
      "parameters": {
        "options": {
          "keepSource": "both"
        },
        "operation": "binaryToPropery"
      },
      "typeVersion": 1
    },
    {
      "id": "2a32007a-337c-455c-9c96-52307e1efcc9",
      "name": "Re-set base64",
      "type": "n8n-nodes-base.set",
      "position": [
        1152,
        224
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "efee376a-ed86-4895-8fc5-df2ff3b08a80",
              "name": "data",
              "type": "string",
              "value": "={{ $('Save base64').item.json.data }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "7424c2eb-c3c3-46ff-82e4-2404f5cfc7fe",
      "name": "Check for new invoices",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -288,
        384
      ],
      "parameters": {
        "event": "fileCreated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "GOOGLE_DRIVE_FOLDER_ID_001",
          "cachedResultUrl": "https://drive.google.com/drive/folders/GOOGLE_DRIVE_FOLDER_ID_001",
          "cachedResultName": "Scanned invoices"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a6839ddd-aacc-405b-a145-dd2152f91f6e",
      "name": "Download file",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        128,
        384
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {},
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "17d99786-f1e9-46e8-85a5-64d5d4ed65ad",
      "name": "Fetch 50 docs",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -64,
        208
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list",
            "value": "GOOGLE_DRIVE_FOLDER_ID_001",
            "cachedResultUrl": "https://drive.google.com/drive/folders/GOOGLE_DRIVE_FOLDER_ID_001",
            "cachedResultName": "Scanned invoices"
          }
        },
        "options": {},
        "resource": "fileFolder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "3ad34319-3715-4ff0-bf90-bfdd0f3a27e8",
      "name": "Extract from File",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        1152,
        480
      ],
      "parameters": {
        "options": {},
        "operation": "pdf"
      },
      "typeVersion": 1
    },
    {
      "id": "a7daca53-ed48-4af9-8c27-c18e4b5710e2",
      "name": "Identify CamScanner",
      "type": "n8n-nodes-base.if",
      "position": [
        1312,
        480
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "d074d65f-65d4-46b1-baa6-306a2ca1e827",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.info.Author }}",
              "rightValue": "CamScanner"
            },
            {
              "id": "3d23a529-7e71-489d-9997-0d3f22bc0927",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              },
              "leftValue": "={{ $json.text }}",
              "rightValue": "CamScanner"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a4df0641-b633-4ba4-bb43-31cacac9824b",
      "name": "Analyze Image",
      "type": "n8n-nodes-base.httpRequest",
      "maxTries": 2,
      "position": [
        1504,
        304
      ],
      "parameters": {
        "url": "https://api.ocr.space/parse/image",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "multipart-form-data",
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "file",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "data"
            },
            {
              "name": "OCREngine",
              "value": "2"
            },
            {
              "name": "detectOrientation",
              "value": "true"
            }
          ]
        },
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2
    },
    {
      "id": "4ac1d408-c1e7-4b8c-9be8-53612b0addfe",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        1664,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "32ec54fd-db5a-4bc6-bbe4-9264d3a72c56",
              "name": "text",
              "type": "string",
              "value": "={{ $json.ParsedResults[0].ParsedText }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "332f6947-3460-4dc6-984a-982a3829b121",
      "name": "Information Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        1856,
        496
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {},
        "attributes": {
          "attributes": [
            {
              "name": "company name",
              "description": "The company which produced the invoice"
            },
            {
              "name": "total",
              "type": "number",
              "description": "The total price of the invoice"
            },
            {
              "name": "currency",
              "description": "The currency of the total sum as letters code or symbol"
            },
            {
              "name": "date",
              "description": "the date in which the invoice was generated"
            },
            {
              "name": "invoice number",
              "description": "a unique id of the invoice, can be a mixture of text and numbers"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "57343929-604b-4b2f-bbc4-3c601033538d",
      "name": "Append row in sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2176,
        496
      ],
      "parameters": {
        "columns": {
          "value": {
            "id": "={{ $('Set file identity').last().json.id }}",
            "Scan": "=https://drive.google.com/file/d/{{ $('Set file identity').last().json.id }}/view?usp=drive_link",
            "date": "={{ $json.output.date }}",
            "total": "={{ $json.output.total }}",
            "currency": "={{ $json.output.currency }}",
            "scan name": "={{ $('Set file identity').last().json.name }}",
            "company name": "={{ $json.output['company name'] }}",
            "invoice number": "={{ $json.output['invoice number'] }}"
          },
          "schema": [
            {
              "id": "id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "company name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "company name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "total",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "currency",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "currency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "invoice number",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "invoice number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Scan",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Scan",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "scan name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "scan name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Context",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Context",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/GOOGLE_SHEETS_DOCUMENT_ID_001/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "GOOGLE_SHEETS_DOCUMENT_ID_001",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/GOOGLE_SHEETS_DOCUMENT_ID_001/edit?usp=drivesdk",
          "cachedResultName": "Expenses from scanned invoices"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "96efa1fd-74e9-4242-8256-26185c021340",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1104,
        688
      ],
      "parameters": {
        "width": 380,
        "height": 220,
        "content": "### Reroute to OCR if needed\n\n- Try to extract text directly from PDF. \n- CamScanner is not saving text in a readable format, so we fall back to the image path instead.\n- Otherwise we send the text directly to the information extractor.\n\nOCR.Space API registration: https://ocr.space/OCRAPI"
      },
      "typeVersion": 1
    },
    {
      "id": "670c7063-2d84-4b34-b99b-68414f55e27f",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        304
      ],
      "parameters": {
        "color": 2,
        "width": 460,
        "height": 80,
        "content": "### Setting some handy values for later down the road\n"
      },
      "typeVersion": 1
    },
    {
      "id": "21b66216-fd94-49cf-8050-0b165308c440",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1120,
        144
      ],
      "parameters": {
        "width": 696,
        "height": 308,
        "content": "### OCR for images and badly extracted PDFs \n"
      },
      "typeVersion": 1
    },
    {
      "id": "ea1bc5c4-d8ed-47c0-a5b9-9bf6277342ca",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -352,
        16
      ],
      "parameters": {
        "width": 440,
        "height": 640,
        "content": "# Start here\n\n- Use the manual trigger to initialize the Google Drive folder with existing invoices\n- The scheduled trigger will identify new invoices and will start automatically"
      },
      "typeVersion": 1
    },
    {
      "id": "ed831e1b-15fa-42c4-9deb-7a347745ab92",
      "name": "Mailgun",
      "type": "n8n-nodes-base.mailgun",
      "position": [
        688,
        144
      ],
      "parameters": {
        "html": "=<h3>New invoices were scanned</h3>\n\n{{ $json.table }}",
        "subject": "New scanned invoices",
        "toEmail": "user@example.com",
        "fromEmail": "=n8n@example.com"
      },
      "credentials": {
        "mailgunApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0f1ad01f-96c7-4c49-a346-040088f40b6c",
      "name": "Prepare a table",
      "type": "n8n-nodes-base.html",
      "position": [
        528,
        144
      ],
      "parameters": {
        "options": {
          "capitalize": true,
          "customStyling": true
        },
        "operation": "convertToHtmlTable"
      },
      "typeVersion": 1.2
    },
    {
      "id": "8ba5e5ab-b8e8-4ec5-96fd-c9b541832b66",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        16
      ],
      "parameters": {
        "width": 340,
        "height": 112,
        "content": "# End here\n"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save base64": {
      "main": [
        [
          {
            "node": "Switch Mime Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Image": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch 50 docs": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Re-set base64": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Analyze Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Prepare a table",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set file identity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare a table": {
      "main": [
        [
          {
            "node": "Mailgun",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Mime Type": {
      "main": [
        [
          {
            "node": "Analyze Image",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Identify CamScanner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Set file identity": {
      "main": [
        [
          {
            "node": "Save base64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Identify CamScanner": {
      "main": [
        [
          {
            "node": "Re-set base64",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check for new invoices": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking 'Execute workflow'": {
      "main": [
        [
          {
            "node": "Fetch 50 docs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}