{
  "name": "Bookkeeping-Inbound-Invoices-Xero-ForAccountants",
  "nodes": [
    {
      "parameters": {
        "url": "=https://api.xero.com/connections",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {}
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -4480,
        2120
      ],
      "id": "6431e564-8033-45de-9265-7fdfeddeaa7f",
      "name": "Xero - Search Supplier",
      "credentials": {
        "oAuth2Api": {
          "id": "SXGZrsXKDHn9KD6c",
          "name": "Xero - Oauth2"
        }
      }
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "hasAttachments": true
        },
        "options": {
          "downloadAttachments": false
        }
      },
      "type": "n8n-nodes-base.microsoftOutlookTrigger",
      "typeVersion": 1,
      "position": [
        -6500,
        2140
      ],
      "id": "1574952b-ebd6-4787-8b67-59e9c5281bd4",
      "name": "Incoming E-mail",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "id": "7LLt9rtTdJtM9TIG",
          "name": "Microsoft Outlook - accounts@yourdomain.com"
        }
      }
    },
    {
      "parameters": {
        "resource": "messageAttachment",
        "operation": "getAll",
        "messageId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -6280,
        2140
      ],
      "id": "acec9486-3120-490e-9621-22b27f0a576e",
      "name": "List Attachments",
      "webhookId": "ad67df26-c118-4f20-b9ba-8fcd826ce1ca",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "id": "7LLt9rtTdJtM9TIG",
          "name": "Microsoft Outlook - accounts@yourdomain.com"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -6060,
        2140
      ],
      "id": "edea3f60-9f4d-401c-839f-ce5d1d1b987f",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {
          "maxPages": 6
        }
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        -5400,
        2140
      ],
      "id": "f4bfbfbd-7f5b-436e-b21d-76fa7ce65ca9",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "text": "={{ $('Extract from File').first().json.text }}",
        "attributes": {
          "attributes": [
            {
              "name": "reference",
              "description": "Invoice  Reference"
            },
            {
              "name": "invoice_issueing_company",
              "description": "Company who is seeking payment i.e. the Creditor\n",
              "required": true
            },
            {
              "name": "tax",
              "type": "number",
              "description": "Amount of tax included in invoice",
              "required": true
            },
            {
              "name": "total",
              "type": "number",
              "description": "total amount paid or due",
              "required": true
            },
            {
              "name": "issue_date",
              "type": "date",
              "description": "date invoice was issued"
            },
            {
              "name": "due_date",
              "type": "date",
              "description": "date payment is due.\n\nIf not found default to 20th of the month",
              "required": true
            },
            {
              "name": "invoice_number",
              "description": "Invoice Number",
              "required": true
            },
            {
              "name": "issued_to",
              "description": "Company the invoice is issued to i.e. the debtor",
              "required": true
            },
            {
              "name": "currency",
              "description": "3 letter currency code, e.g. NZD, GBP",
              "required": true
            },
            {
              "name": "tax_rating",
              "description": "=This will be either NONE (no tax) or INPUT2 (15% GST)",
              "required": true
            },
            {
              "name": "summary",
              "description": "1 line summary of all invoice contents. e.g. \"M365 Licenses\"",
              "required": true
            },
            {
              "name": "account_code",
              "type": "number",
              "description": "=Do not invent a new account code - Only use a code from the Chart of Accounts\n\nChart of Accounts:\n\n{{ \n  $('Xero - Get Chart of Accounts').item.json.Accounts\n    .filter(account => account && account.Code && Number(account.Code) >= 300 && Number(account.Code) <= 495)\n    .map(account => `${account.Code} - ${account.Name} - ${account.Description || \"\"}`)\n    .join('\\n')\n}}\n\n---\nDo not use a code that is not in the above list.\n\n",
              "required": true
            },
            {
              "name": "sub_total",
              "type": "number",
              "description": "Pre Tax Subtotal",
              "required": true
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1,
      "position": [
        -3400,
        2120
      ],
      "id": "17a0ab74-ea02-4bb3-8b5b-497c505a37f2",
      "name": "Information Extractor"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "anthropic/claude-3.5-sonnet",
          "mode": "list",
          "cachedResultName": "ANTHROPIC/CLAUDE-3.5-SONNET"
        },
        "messages": {
          "values": [
            {
              "content": "={{ $json.text }}"
            },
            {
              "content": "=You are a document classification assistant. Your job is to:\n\nLabel whether the document is an invoice (isInvoice: true or false).\n\nIdentify the debtor — the company being invoiced (i.e. the customer who is expected to pay).\n\nIdentify the creditor — the company issuing the invoice (i.e. the supplier who expects to be paid).\n\nIf the word Customer or Client appears next to a company name that will be hte debtor\n\nResponse format:\n{\n  \"isInvoice\": true,\n  \"debtor\": \"company who must pay\",\n  \"creditor\": \"company who issued the invoice\"\n}\n",
              "role": "system"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        -5180,
        2140
      ],
      "id": "3a069b61-d469-4dbe-aeb8-f5a39d78da88",
      "name": "OpenAI",
      "credentials": {
        "openAiApi": {
          "id": "RHB5CrOHTdF9v4cW",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "organizationId": "={{ $('Xero Selector Agent').first().json.output.tenantId }}",
        "type": "ACCPAY",
        "contactId": "={{ $json.ContactID !== undefined ? $json.ContactID : \"e2468fdf-40eb-4185-a75f-aaa6135238b2\" }}",
        "lineItemsUi": {
          "lineItemsValues": [
            {
              "description": "={{ $('Information Extractor').first().json.output.summary }}",
              "unitAmount": "={{ $('Information Extractor').first().json.output.sub_total }}",
              "itemCode": "={{ null }}",
              "accountCode": "={{ $('Information Extractor').first().json.output.account_code }}",
              "taxType": "={{ $('Information Extractor').item.json.output.tax_rating }}",
              "taxAmount": "={{ $('Information Extractor').first().json.output.tax }}",
              "lineAmount": "={{ $('Information Extractor').first().json.output.sub_total }}"
            }
          ]
        },
        "additionalFields": {
          "currency": "={{ $('Information Extractor').first().json.output.currency }}",
          "date": "={{ $('Information Extractor').first().json.output.issue_date }}",
          "dueDate": "={{ $('Information Extractor').first().json.output.due_date }}",
          "invoiceNumber": "={{ $('Information Extractor').first().json.output.invoice_number }}",
          "reference": "={{ $('Information Extractor').first().json.output.reference ?? null }}\n",
          "status": "DRAFT"
        }
      },
      "type": "n8n-nodes-base.xero",
      "typeVersion": 1,
      "position": [
        -2080,
        2100
      ],
      "id": "20de9fb9-473d-4920-986f-9fcd43f91f12",
      "name": "Create Invoice",
      "credentials": {
        "xeroOAuth2Api": {
          "id": "nYHsp3h8Rglldupz",
          "name": "Xero account"
        }
      }
    },
    {
      "parameters": {
        "resource": "messageAttachment",
        "operation": "download",
        "messageId": {
          "__rl": true,
          "value": "={{ $('Incoming E-mail').item.json.id }}",
          "mode": "id"
        },
        "attachmentId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -5620,
        2140
      ],
      "id": "28c1517a-6e62-454f-b4d0-4a75fa4faba6",
      "name": "Download Attachment",
      "webhookId": "b39fb235-999a-4aa5-bd9b-4f71e474e936",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "id": "7LLt9rtTdJtM9TIG",
          "name": "Microsoft Outlook - accounts@yourdomain.com"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.xero.com/api.xro/2.0/Invoices/{{ $('Create Invoice').first().json.InvoiceID }}/Attachments/{{ $('Download Attachment').first().json.name.replace(/_/g, '-').replace(/[^a-zA-Z0-9.-]/g, '') }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "xero-tenant-id",
              "value": "={{ $('Xero Selector Agent').first().json.output.tenantId }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "data",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -1580,
        2100
      ],
      "id": "4dc931cd-76ad-4aa3-b3be-ad654ee2852b",
      "name": "Xero - Upload Attachment",
      "credentials": {
        "oAuth2Api": {
          "id": "SXGZrsXKDHn9KD6c",
          "name": "Xero - Oauth2"
        }
      }
    },
    {
      "parameters": {
        "resource": "messageAttachment",
        "operation": "download",
        "messageId": {
          "__rl": true,
          "value": "={{ $('Incoming E-mail').first().json.id }}",
          "mode": "id"
        },
        "attachmentId": {
          "__rl": true,
          "value": "={{ $('Download Attachment').first().json.id }}",
          "mode": "id"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -1800,
        2100
      ],
      "id": "9d24a92c-6d47-4fee-83f3-e72850c5a53f",
      "name": "Download Attachment for Xero",
      "webhookId": "b3089834-a948-4912-a63f-534da36308da",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "id": "7LLt9rtTdJtM9TIG",
          "name": "Microsoft Outlook - accounts@yourdomain.com"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "b0c37d98-59c3-4958-b575-082e032db89f",
              "leftValue": "={{ $json.message.content.isInvoice }}",
              "rightValue": "true",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -4800,
        2140
      ],
      "id": "80a36388-c4a1-4215-8f00-041fdaafc2d5",
      "name": "If Invoice"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "37878341-99ff-46b6-a199-7b060a667a1d",
              "leftValue": "={{ $json.name }}",
              "rightValue": ".pdf",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            },
            {
              "id": "6dc0f57d-de5e-4521-b256-ed9900f414c5",
              "leftValue": "={{ $json.name }}",
              "rightValue": ".PDF",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -5840,
        2140
      ],
      "id": "95edddb3-4f40-4589-bfca-dbea2a82b414",
      "name": "If PDF"
    },
    {
      "parameters": {
        "url": "=https://api.xero.com/api.xro/2.0/Accounts",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "xero-tenant-id",
              "value": "={{ $('Xero Selector Agent').first().json.output.tenantId }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -3640,
        2120
      ],
      "id": "5e86ae80-50d7-4f70-bf68-621d5c738dda",
      "name": "Xero - Get Chart of Accounts",
      "credentials": {
        "oAuth2Api": {
          "id": "SXGZrsXKDHn9KD6c",
          "name": "Xero - Oauth2"
        }
      }
    },
    {
      "parameters": {
        "content": "## Validate Attachment is an Invoice\n",
        "height": 880,
        "width": 2000,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -6580,
        1740
      ],
      "id": "60850013-a716-4205-b227-947e488a3bc3",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## Process the Invoice In Xero\n\n### Notes:\n - New Contact should not be created - if contact doesn't exist should create a bill without a contact\n - Name of file that gets attached in Xero should match naming convention in SharePoint\n\n\n### Questions\nWhat supplier lookup method is prefered, e-mail is more accurate, but if you can't gaurantee e-mail listed in Xero matches the email invoice comes from then we need to use the search ora hybrid approach\n",
        "height": 880,
        "width": 2640,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -3740,
        1740
      ],
      "id": "a177012d-74d0-4381-ad0b-d15f331dcfe8",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Select Xero Tenant\n\n\n## Questions/Discussion\n- The company an invoice is addressed to will be the target tenant?\n- As this is set by the supplier issueing the invoice we are relying on what ever they have setup in their invoicing platform so might not always be an exact match to the tenant name setup in Xero? What is the expected level of variance?\n- I'm leaning towards an AI agent here making a \"soft match\" or \"fuxzy match\"\n\n- Could possibly match on Address - explore this option\n",
        "height": 880,
        "width": 760,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -4540,
        1740
      ],
      "id": "9c7756c4-07db-4f1b-9625-6a1705af7b96",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n  \"tenantName\":\"The Big Company\",\n  \"tenantId\":\"sdfs-a45asd46-a4s56d4a4-asd\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [
        -3940,
        2340
      ],
      "id": "20afd87f-c0b1-49b8-a588-e0f3ab5cae61",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "model": "openai/gpt-4.1-mini",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        -4200,
        2340
      ],
      "id": "f32c0dd3-358a-4db1-b736-9f180feaeaec",
      "name": "OpenRouter Chat Model",
      "credentials": {
        "openRouterApi": {
          "id": "OFIzkoABYhsKSgBL",
          "name": "OpenRouter account - PKFR"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Transform multiple tenant records into a single item with tenant objects\nconst tenants = $input.all().map(item => item.json);\n\n// Extract just the tenant names and IDs\nconst simplifiedTenants = tenants.map(tenant => ({\n  tenantName: tenant.tenantName,\n  tenantId: tenant.tenantId\n}));\n\n// Return as a single item to prevent multiple node executions\nreturn [{\n  json: {\n    tenants: simplifiedTenants\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -4300,
        2120
      ],
      "id": "677ed3b6-cb21-4a3f-9efa-e74211866de8",
      "name": "Flattened Tenant Data"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=The invoice is addressed to: {{ $('OpenAI').first().json.message.content.debtor }}\n",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "=You are an agent that selects the correct Xero tenant based on a Companies Name as listed on an invoice.\n\nThe name might not match exactly.\n\ne.g. the Company might be \"XYZ Company\" but the Xero Tenant may have the name listed as \"XYZ Company LTD\"\n\nThe available Xero Tenants are listed in an arrar of objects below:\n\n{{ JSON.stringify($('Flattened Tenant Data').first().json) }}\n\nReturn a JSON Object only"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.9,
      "position": [
        -4120,
        2120
      ],
      "id": "ad4f7864-50e7-4e01-a287-42cb6209189d",
      "name": "Xero Selector Agent"
    },
    {
      "parameters": {
        "resource": "contact",
        "organizationId": "={{ $('Xero Selector Agent').first().json.output.tenantId }}",
        "name": "={{ $('Information Extractor').first().json.output.invoice_issueing_company }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.xero",
      "typeVersion": 1,
      "position": [
        -2400,
        2300
      ],
      "id": "846c022f-99f2-47cf-81b6-4a9f4dd259cc",
      "name": "Create Contact - No Email",
      "credentials": {
        "xeroOAuth2Api": {
          "id": "nYHsp3h8Rglldupz",
          "name": "Xero account"
        }
      }
    },
    {
      "parameters": {
        "url": "=https://api.xero.com/api.xro/2.0/Invoices?ContactIDs={{ $('Xero - Search Supplier2').first().json.Id }}&Status=PAID&page=1&limit=1",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "xero-tenant-id",
              "value": "={{ $('Xero Selector Agent').first().json.output.tenantId }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -2480,
        2020
      ],
      "id": "287a685b-8089-4442-8d83-31dae1f06302",
      "name": "Xero - Check Invoice Coding",
      "credentials": {
        "oAuth2Api": {
          "id": "SXGZrsXKDHn9KD6c",
          "name": "Xero - Oauth2"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d1d8f409-98ea-41b6-a8ff-7fd82c1fb804",
              "name": "ContactID",
              "value": "={{ $('Xero - Search Supplier2').first().json.Contacts[0].ContactID }}",
              "type": "string"
            },
            {
              "id": "b1e9b07b-b31e-4d1e-a14a-f6ec197e7014",
              "name": "account_code",
              "value": "={{ ($json.Invoices?.[0]?.LineItems?.[0]?.AccountCode !== undefined) ? $json.Invoices[0].LineItems[0].AccountCode : null }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -2280,
        2020
      ],
      "id": "b49ceab9-29c5-4580-81cd-22a989789abb",
      "name": "Set Contact ID & Account Code"
    },
    {
      "parameters": {
        "url": "=https://api.xero.com/api.xro/2.0/Contacts?SearchTerm={{ $('Information Extractor').first().json.output.invoice_issueing_company }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "xero-tenant-id",
              "value": "={{ $('Xero Selector Agent').first().json.output.tenantId }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -3020,
        2120
      ],
      "id": "39f8d44d-e9bb-447a-8951-345da0252c7f",
      "name": "Xero - Search Supplier2",
      "credentials": {
        "oAuth2Api": {
          "id": "SXGZrsXKDHn9KD6c",
          "name": "Xero - Oauth2"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "7985a3a7-80be-4256-81a3-1e3b547f23a8",
              "leftValue": "={{ $('Xero - Search Supplier2').first().json.Contacts[0].ContactID }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -2820,
        2120
      ],
      "id": "fc43bff4-61f4-4de6-8f53-64c33d1d4aaa",
      "name": "If Contact Exists (Name Match)1"
    },
    {
      "parameters": {
        "content": "## Setup Notes\n### Authentications to setup\n\n- Mailbox\n   - If on hosted n8n simply connect to mailbox and provide credentials\n    - If Self hosted n8n will need to setup an App registration first with a Client secret. The App ID and secret are required and then should be able to complete a sign in workflow\n\n- Open Router API Key\n\n- Xero Application\n    - Need to setup Xero Application and complete authentication in one of the Xero nodes (first).\n    - Make sure during the Xero Auth flow you allow access to all relevant tenants\n    - Create a custom Oauth2 credential for Xero custom http nodes\n\n### Update wording/parameters\n\n- Update the e-mail addresses 'if non specific e-mail node, I sometimes forward invoices to myself or they come from generic addresses like xero-mail\n\n- In the information Extractor node I specifically instruct it not to set my own business (HybrIT) as the issueing company (as I am the recieving company) update to match your busienss\n",
        "height": 900,
        "width": 580,
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -7200,
        1740
      ],
      "id": "7ed258ae-ea90-4439-b518-3d65910d5cb4",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "operation": "move",
        "messageId": {
          "__rl": true,
          "value": "={{ $('Incoming E-mail').first().json.id }}",
          "mode": "id"
        },
        "folderId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_MAILBOX_FOLDER_ID=",
          "mode": "list",
          "cachedResultName": "processed_invoices",
          "cachedResultUrl": "https://outlook.office365.com/mail/REPLACE_WITH_YOUR_MAILBOX_FOLDER_ID%3D"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -1360,
        2100
      ],
      "id": "632c36c0-1f3f-4a1b-938d-30f688967a50",
      "name": "Move E-mail to Processed",
      "webhookId": "89c7fac6-89f3-4d9b-a62a-3f694e8ff543",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "id": "7LLt9rtTdJtM9TIG",
          "name": "Microsoft Outlook - accounts@yourdomain.com"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "anthropic/claude-3.5-sonnet",
          "mode": "list",
          "cachedResultName": "anthropic/claude-3.5-sonnet"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        -3480,
        2340
      ],
      "id": "c8ad2c8b-ae63-4c0a-a6a1-bae8ddb852da",
      "name": "Claude Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "RHB5CrOHTdF9v4cW",
          "name": "OpenAi account"
        }
      }
    }
  ],
  "pinData": {
    "Incoming E-mail": [
      {
        "json": {
          "id": "REPLACE_WITH_YOUR_MAILBOX_FOLDER_ID",
          "conversationId": "AAQkADM0OGNkZmNjLTQzNzEtNGQ4Yy04Y2M1LTExZDEyYTIwMzAxOQAQAOZdzvZbr5JIqczSCf2TnjQ=",
          "subject": "Invoice INV-0304 from HybrIT Services NZ Limited for Timecraft AI Ltd",
          "bodyPreview": "CAUTION: This email originated from outside of HybrIT Services. Do not click links or open attachments unless you recognise the sender and know the content is safe.\r\n\r\n\r\nHybrIT Services NZ Limited\r\n$1,150.00 NZD\r\nDue 30 May 2025\r\nInvoice #: INV-0304\r\nView",
          "from": "messaging-service@post.xero.com",
          "to": [
            "accounts@yourdomain.com"
          ],
          "categories": [],
          "hasAttachments": true
        }
      }
    ]
  },
  "connections": {
    "Xero - Search Supplier": {
      "main": [
        [
          {
            "node": "Flattened Tenant Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Incoming E-mail": {
      "main": [
        [
          {
            "node": "List Attachments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List Attachments": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "If PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Xero - Search Supplier2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "If Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Invoice": {
      "main": [
        [
          {
            "node": "Download Attachment for Xero",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Attachment": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Attachment for Xero": {
      "main": [
        [
          {
            "node": "Xero - Upload Attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Invoice": {
      "main": [
        [
          {
            "node": "Xero - Search Supplier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If PDF": {
      "main": [
        [
          {
            "node": "Download Attachment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Xero - Get Chart of Accounts": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Xero Selector Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Xero Selector Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Flattened Tenant Data": {
      "main": [
        [
          {
            "node": "Xero Selector Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Xero Selector Agent": {
      "main": [
        [
          {
            "node": "Xero - Get Chart of Accounts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Xero - Check Invoice Coding": {
      "main": [
        [
          {
            "node": "Set Contact ID & Account Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Xero - Search Supplier2": {
      "main": [
        [
          {
            "node": "If Contact Exists (Name Match)1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Contact Exists (Name Match)1": {
      "main": [
        [
          {
            "node": "Xero - Check Invoice Coding",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Contact - No Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Contact ID & Account Code": {
      "main": [
        [
          {
            "node": "Create Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contact - No Email": {
      "main": [
        [
          {
            "node": "Create Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Xero - Upload Attachment": {
      "main": [
        [
          {
            "node": "Move E-mail to Processed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8a4d248c-af09-4488-8954-4fd2abecd80a",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": ""
  },
  "id": "wFVcdodrVIUiF4UK",
  "tags": []
}