{
  "name": "Expense receipt collector (Agent Routines)",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "receipt OR invoice OR \"order confirmation\""
        },
        "options": {
          "downloadAttachments": true
        }
      },
      "id": "ccb5cdbf-d963-58a8-a57c-c90ec54bb0cd",
      "name": "Receipt emails",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        250,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "Return JSON only. Keys: vendor, date (YYYY-MM-DD), amount (number), currency, category."
            },
            {
              "content": "=Subject: {{ $json.subject }}\nFrom: {{ $json.from.text }}\nBody: {{ $json.text }}\n\nWorkflow: Gmail trigger on new emails matching 'receipt OR invoice OR order confirmation' -> AI node extracts vendor, date, amount, currency, category -> append row to Google Sheet 'Expenses' -> save attachment PDF to a Drive folder named by month. Monthly: email the sheet link to yourself."
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "id": "8b38efaf-4687-5ad2-9641-282bb568b916",
      "name": "Extract expense",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        490,
        300
      ],
      "credentials": {
        "openAiApi": {
          "id": "REPLACE_ME",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map(i=>({json:i.json.message.content}));"
      },
      "id": "19f66d30-8bd6-5239-9677-86d84687f9e8",
      "name": "Flatten fields",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        730,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_SHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Expenses"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "4b20e5fe-44a0-5388-b6c0-cf7a85528280",
      "name": "Append to Expenses",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        970,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "REPLACE_ME",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "name": "={{ $('Receipt emails').item.json.subject }}.pdf",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_FOLDER_ID"
        },
        "inputDataFieldName": "attachment_0",
        "options": {}
      },
      "id": "c8d543cf-7667-5d27-a02a-fcf0896c5f17",
      "name": "Save receipt PDF",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        490,
        460
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "REPLACE_ME",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 1 * *"
            }
          ]
        }
      },
      "id": "642b80e9-6528-537f-9902-1206bbffa3d9",
      "name": "Monthly on the 1st",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        250,
        460
      ]
    },
    {
      "parameters": {
        "sendTo": "<YOUR_EMAIL>",
        "subject": "Monthly expenses",
        "emailType": "text",
        "message": "Expenses sheet: https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID",
        "options": {}
      },
      "id": "ce344440-fdea-5f10-b6b3-cc7014d9034c",
      "name": "Email sheet link",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        490,
        620
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "Receipt emails": {
      "main": [
        [
          {
            "node": "Extract expense",
            "type": "main",
            "index": 0
          },
          {
            "node": "Save receipt PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract expense": {
      "main": [
        [
          {
            "node": "Flatten fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten fields": {
      "main": [
        [
          {
            "node": "Append to Expenses",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Monthly on the 1st": {
      "main": [
        [
          {
            "node": "Email sheet link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "https://agentroutines.si/r/expense-receipt-collector/"
  },
  "tags": []
}