{
  "id": "Invzq7tJ1X0LQ3rb",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Send Invoice to clients automatically",
  "tags": [
    {
      "id": "j0SNhalFSbPAhdWo",
      "name": "n8n creator",
      "createdAt": "2025-09-02T01:40:23.866Z",
      "updatedAt": "2025-09-02T01:40:23.866Z"
    }
  ],
  "nodes": [
    {
      "id": "43908db1-0163-41c4-8c0b-a127e575c058",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        -688,
        144
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3
    },
    {
      "id": "e10c62a4-8c0c-49e6-9672-68a0aee51b5a",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -2320,
        464
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "5a37b5e5-40e8-4888-ac24-66367e65969a",
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1744,
        464
      ],
      "parameters": {
        "url": "https://api.ocr.space/parse/image",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "multipart-form-data",
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "language",
              "value": "eng"
            },
            {
              "name": "file",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "data"
            }
          ]
        },
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "9e72ec12-aba0-474e-97f3-7d7958905e70",
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -2016,
        464
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Google Drive Trigger').item.json.id }}"
        },
        "options": {},
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "73e4f62d-80c4-483f-8c00-f5fc7c8b9882",
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        -1024,
        688
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1UDlHvLgcZn714etZxtgm7bJCATvDcutVyugrvqM2kaM/edit#gid=0",
          "cachedResultName": "Feuille 1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1UDlHvLgcZn714etZxtgm7bJCATvDcutVyugrvqM2kaM",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1UDlHvLgcZn714etZxtgm7bJCATvDcutVyugrvqM2kaM/edit?usp=drivesdk",
          "cachedResultName": "list of company names and emails"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "550dbd6b-b691-43eb-afbd-b3a16a7f59d0",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1120,
        864
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ebb1c8f5-cbf0-4d00-b171-1f6040c38300",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "onError": "continueRegularOutput",
      "position": [
        -1088,
        480
      ],
      "parameters": {
        "text": "={{ $json.company }}",
        "options": {
          "systemMessage": "=You will receive a company name as plain text.\n\nYour task is to:\n\t1.\tCross-reference the given name against a Google Sheet database.\n\t2.\tThe database contains two columns: Company Name and Email.\n\t3.\tPerform a case-insensitive search to find an exact match for the company name.\n\t4.\tIf a match is found, return a JSON object \n{\n  \"company_name\": \"Matched Company Name\",\n  \"email\": \"matched@email.com\"\n}\n\n5. If no match is found, return a JSON object with an error message:\n{\n  \"error\": \"Company not found in the database.\"\n}",
          "returnIntermediateSteps": false
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": true,
      "typeVersion": 1.7,
      "waitBetweenTries": 5000
    },
    {
      "id": "dd8d6365-6a12-4150-82d3-a3ee0bbb74d5",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -1536,
        464
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "32dbf33f-9c71-4d84-ab7c-cdb1df189210",
              "name": "text",
              "type": "string",
              "value": "={{ $json.ParsedResults[0].ParsedText}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a901f0d8-f5a5-4d01-9e86-73980cd74e47",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "onError": "continueErrorOutput",
      "position": [
        -1328,
        464
      ],
      "parameters": {
        "jsCode": "const inputText = $json[\"text\"];\nconst lines = inputText.split(/\\r?\\n/);\n\nlet factureIndex = lines.findIndex(line => line.toLowerCase().includes(\"billed to\"));\nlet name = null;\n\nif (factureIndex >= 0) {\n  const lineAfter = lines[factureIndex + 1]?.trim();\n  const nextLine = lines[factureIndex + 2]?.trim();\n\n  const isProbablyName = (line) => (\n    /[a-z]/.test(line) &&                  // has lowercase\n    line.length >= 5 &&                   // not too short\n    line !== line.toUpperCase()           // not all uppercase\n  );\n\n  if (isProbablyName(lineAfter)) {\n    name = lineAfter;\n  } else if (isProbablyName(nextLine)) {\n    name = nextLine;\n  }\n}\n\nif (!name) {\n  throw new Error(\"No company name found after 'Billed to'\");\n}\n\nreturn [{ company: name }];"
      },
      "typeVersion": 2
    },
    {
      "id": "a7ad5822-78d6-48cb-b9d8-b60017a52a30",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -2016,
        128
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "999cb1c3-3ce1-44b1-bcdd-b6a7399e903a",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        -1504,
        816
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3
    },
    {
      "id": "944af94f-6a2a-4cd2-9334-5548e8645303",
      "name": "No Operation, do nothing1",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -1728,
        128
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "14ea1466-6532-42f8-a717-c716a92647dd",
      "name": "Error",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -512,
        816
      ],
      "parameters": {
        "sendTo": "Email Here",
        "message": "The error in the workflow pertains to the attached document.  Please check if it has not been sent and manually send it.  Also check the database to make sure the name of the corresponding company is in the database. ",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {}
            ]
          },
          "appendAttribution": false
        },
        "subject": "Error in Workflow",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "28926b1c-ce6a-4d1d-a42e-199d11d302f7",
      "name": "Send Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -160,
        816
      ],
      "parameters": {
        "sendTo": "Email Here",
        "message": "The error in the workflow pertains to the attached document.  Please check if it has not been sent and manually send it.  Also check the database to make sure the name of the corresponding company is in the database. ",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {}
            ]
          },
          "appendAttribution": false
        },
        "subject": "Error in Workflow",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "b0fcd4e0-2875-419e-b3e8-23082150e0a0",
      "name": "Error1",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -1328,
        816
      ],
      "parameters": {
        "sendTo": "Email Here",
        "message": "The error in the workflow pertains to the attached document.  Please check if it has not been sent and manually send it.  Also check the database to make sure the name of the corresponding company is in the database. ",
        "options": {},
        "subject": "Error in workflow",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "00a3bd30-62c6-40e5-ad29-89f9ec811aa5",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -512,
        144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "dcfbe432-fc35-438e-8e4b-4fa109107376",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $('AI Agent').item.json.output }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "5b9ce018-3103-4c30-ab83-bdb440eb9ae1",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        -896,
        704
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\n\t\"email\":\"email of recipient\",\n  \"name\":\"name of recipient\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "c34d9478-0acb-492b-807e-a57bfb269a23",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2608,
        272
      ],
      "parameters": {
        "width": 176,
        "height": 352,
        "content": "### Here we set a google drive folder, where any time an invoice is added, the automation will be triggered automatically"
      },
      "typeVersion": 1
    },
    {
      "id": "8f83d6d0-bc12-4cd9-9a3c-cc291400016a",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2352,
        272
      ],
      "parameters": {
        "width": 160,
        "height": 352,
        "content": "### Here we loop over the items, because we may have more than 1 invoice placed in the folder"
      },
      "typeVersion": 1
    },
    {
      "id": "4e45b1c4-0051-4cb9-bee7-a19819da906d",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2048,
        272
      ],
      "parameters": {
        "width": 150,
        "height": 352,
        "content": "### Here we download the invoice to pass it on to the OCR from ocr space"
      },
      "typeVersion": 1
    },
    {
      "id": "47c4d010-9870-4514-8879-a6a37c174663",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3760,
        80
      ],
      "parameters": {
        "width": 1056,
        "height": 1232,
        "content": "# Setting up the workflow\n\n## 1. Connect google drive to your account, and create a folder that will serve as the input. \n\n## 2.  Connect your Gmail account to n8n. \n\n## 3. Create an OCR space account (free). For the authentication, choose Generic Credential Type, then Header Auth. In the Header Auth, create new credential, with the Name as apikey and the Value as YOUR-API-KEY\n\n## 4. Connect your preferred LLM credentials, and connect it to the AI Agent\n\n## 5. Create a copy of the Google Sheet Database, fill it with your list and connect it to the AI Agent\n\n# OCR Space: https://ocr.space\n\n# Google Sheet Database: https://docs.google.com/spreadsheets/d/1M0sS7KZzOn9Bu8Dy0lcrh2jWqVkLa0qFQfki1jecxeE/edit?usp=sharing (Make a copy)\n\n# Workflow Summary: This workflow takes in from a google drive folder, scanned pdf invoices. The workflow then extracts the text from the file using OCR space. Once the text is returned, the AI agent cross references the company name, with the database (excel sheet) to find the company name and the email of the recipient in the company. If it is found the email is sent with the invoice attached. "
      },
      "typeVersion": 1
    },
    {
      "id": "ae882eb4-d4a9-4ffa-a49f-95e4d78b5cd8",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1776,
        272
      ],
      "parameters": {
        "width": 150,
        "height": 352,
        "content": "### Here we are sending the downloaded file to OCR space, and getting the extracted information from the invoice"
      },
      "typeVersion": 1
    },
    {
      "id": "2948912f-1bd6-42a9-a630-e3e24a00a45a",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1568,
        272
      ],
      "parameters": {
        "width": 150,
        "height": 352,
        "content": "### Assigning name to the text. "
      },
      "typeVersion": 1
    },
    {
      "id": "518d1756-b601-4c9c-8b87-c5fc2d601370",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1360,
        272
      ],
      "parameters": {
        "width": 150,
        "height": 352,
        "content": "### Extracting company name. In this case after the billed to:"
      },
      "typeVersion": 1
    },
    {
      "id": "c7f521dd-dc76-4be8-92db-b3de9bf2946e",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1120,
        272
      ],
      "parameters": {
        "width": 320,
        "height": 352,
        "content": "### AI Agent cross references name with Database and returns the recipients email address"
      },
      "typeVersion": 1
    },
    {
      "id": "337d3670-0c2e-4507-8c75-eb235286e97c",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1360,
        688
      ],
      "parameters": {
        "width": 160,
        "height": 272,
        "content": "### If we have any errors, we send an email to the operator for manual review"
      },
      "typeVersion": 1
    },
    {
      "id": "e8c25552-0d00-4285-858f-cc30348f33d3",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -544,
        672
      ],
      "parameters": {
        "width": 160,
        "height": 256,
        "content": "### If we have any errors, we send an email to the operator for manual review"
      },
      "typeVersion": 1
    },
    {
      "id": "95716bac-97bd-4e1f-b7e6-9478cdf151dd",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -192,
        672
      ],
      "parameters": {
        "width": 150,
        "height": 256,
        "content": "### Send email to recipient with the invoice attached"
      },
      "typeVersion": 1
    },
    {
      "id": "46d6e0ac-7555-48a8-b57d-e97a42bec8b0",
      "name": "Google Drive Trigger1",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -2576,
        464
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1fb1901c-362e-41f9-8f14-2ac208b59826",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Error1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ],
        []
      ]
    },
    "Send Email": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          },
          {
            "node": "No Operation, do nothing1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Structured Output Parser1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation, do nothing1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}