{
  "id": "Lksl1EEVeT8xxSZR",
  "meta": {
    "builderVariant": "mcp",
    "aiBuilderAssisted": true
  },
  "name": "Archive documents for Japan Electronic Books Preservation Act with Gemini and Drive",
  "tags": [],
  "nodes": [
    {
      "id": "2541ccd9-9708-47d5-8a99-283671f2dc41",
      "name": "Fetch Document Emails",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        48,
        48
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "q": "has:attachment",
          "readStatus": "unread"
        },
        "options": {
          "downloadAttachments": true,
          "dataPropertyAttachmentsPrefixName": "attachment_"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyHour"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.4
    },
    {
      "id": "17341ae2-b8b7-4241-aa42-c80fc81ab233",
      "name": "Extract Document Text",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        272,
        48
      ],
      "parameters": {
        "options": {},
        "operation": "pdf",
        "binaryPropertyName": "attachment_0"
      },
      "typeVersion": 1.1
    },
    {
      "id": "cc184b46-ceea-41ae-8ff2-ae8153b4e80c",
      "name": "Extract Filing Metadata with Gemini",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "onError": "continueErrorOutput",
      "position": [
        704,
        48
      ],
      "parameters": {
        "text": "=You are a Japanese bookkeeping assistant. Japan's Electronic Books Preservation Act requires every stored document to be searchable by transaction date, counterparty and amount. Extract those three items plus the document type (invoice, receipt, contract or other) from the document text below. Use YYYY-MM-DD for the date and a plain number for the amount. Return empty values if you cannot find them.\n\nDocument text:\n{{ $json.text }}",
        "batching": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.9
    },
    {
      "id": "22167278-7e64-4658-bc35-cc59da104564",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        672,
        224
      ],
      "parameters": {
        "options": {
          "temperature": 0.1
        },
        "modelName": "models/gemini-3.1-flash-lite"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "4ee1b9f6-5a08-41a0-ba71-749ccf6cdc6f",
      "name": "Filing Metadata Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        864,
        240
      ],
      "parameters": {
        "jsonSchemaExample": "{\"transaction_date\":\"2026-07-01\",\"counterparty\":\"Acme KK\",\"amount\":11000,\"doc_type\":\"invoice\"}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "e614bf77-8684-44f9-89a2-bdb0d8409536",
      "name": "Build Compliant File Name",
      "type": "n8n-nodes-base.code",
      "position": [
        1264,
        32
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const src = $json.output || $json;\nconst mail = $('Fetch Document Emails').item.json;\nconst d = String(src.transaction_date || '').replace(/[^0-9]/g, '');\nconst party = String(src.counterparty || '').replace(/[\\\\/:*?\"<>|]/g, '').slice(0, 40);\nconst amt = String(src.amount || '').replace(/[^0-9]/g, '');\nconst complete = d.length === 8 && party.length > 0 && amt.length > 0;\nreturn {\n  fileName: (d || '+1234567890') + '_' + (party || 'unknown') + '_' + (amt || '0') + '.pdf',\n  transactionDate: src.transaction_date || '',\n  counterparty: src.counterparty || '',\n  amount: src.amount || '',\n  docType: String(src.doc_type || 'other').toLowerCase(),\n  complete: complete,\n  messageId: mail.id,\n  subject: mail.subject\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "724e2fa9-1ff9-436b-9049-3a49c0292afb",
      "name": "Check Required Metadata",
      "type": "n8n-nodes-base.if",
      "position": [
        1440,
        32
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.complete }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "00ffa301-9a64-4995-89cd-38ff63c349a0",
      "name": "Index for Legal Search",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1680,
        16
      ],
      "parameters": {
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "Document Index"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "Select your spreadsheet"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "ffd7188b-d606-4247-a4ff-9708e4d26f0d",
      "name": "Mark Email as Filed",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1904,
        16
      ],
      "parameters": {
        "messageId": "={{ $(\"Build Compliant File Name\").item.json.messageId }}",
        "operation": "markAsRead"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "0e70460d-3ebd-49f9-a896-ac064933f1f3",
      "name": "Route by Document Type",
      "type": "n8n-nodes-base.switch",
      "position": [
        2224,
        -16
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": false,
                  "typeValidation": "loose"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $(\"Build Compliant File Name\").item.json.docType }}",
                    "rightValue": "invoice"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": false,
                  "typeValidation": "loose"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $(\"Build Compliant File Name\").item.json.docType }}",
                    "rightValue": "receipt"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": false,
                  "typeValidation": "loose"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $(\"Build Compliant File Name\").item.json.docType }}",
                    "rightValue": "contract"
                  }
                ]
              }
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra",
          "renameFallbackOutput": "Other"
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "c84ababc-08a7-43ec-89b5-eff806d32afc",
      "name": "Archive to Invoices Folder",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2480,
        -32
      ],
      "parameters": {
        "name": "={{ $(\"Build Compliant File Name\").item.json.fileName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "Select your Invoices folder"
        },
        "inputDataFieldName": "attachment_0"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "fe85885e-35f2-4174-b4ee-86ec50796b9a",
      "name": "Archive to Receipts Folder",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2480,
        144
      ],
      "parameters": {
        "name": "={{ $(\"Build Compliant File Name\").item.json.fileName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "Select your Receipts folder"
        },
        "inputDataFieldName": "attachment_0"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "b8465772-337e-4e44-a1f8-b049e9382491",
      "name": "Archive to Contracts Folder",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2480,
        304
      ],
      "parameters": {
        "name": "={{ $(\"Build Compliant File Name\").item.json.fileName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "Select your Contracts folder"
        },
        "inputDataFieldName": "attachment_0"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "5031ac03-ed2c-4df5-860c-c62420831340",
      "name": "Flag Unclassified Document",
      "type": "n8n-nodes-base.slack",
      "position": [
        2480,
        480
      ],
      "parameters": {
        "text": "=*:mag: Document type could not be classified*\n*File:* {{ $(\"Build Compliant File Name\").item.json.fileName }}\nPlease file it in the right folder manually.",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "#accounting"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.5
    },
    {
      "id": "dcecfc09-de15-4ad4-86ed-205d808c47eb",
      "name": "Report Incomplete Metadata",
      "type": "n8n-nodes-base.slack",
      "position": [
        1680,
        224
      ],
      "parameters": {
        "text": "=*:warning: Document cannot be filed - missing required metadata*\n*Email:* {{ $json.subject }}\n*Date:* {{ $json.transactionDate }} *Counterparty:* {{ $json.counterparty }} *Amount:* {{ $json.amount }}\nThe Electronic Books Preservation Act requires all three. Please complete them manually.",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "#accounting"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.5
    },
    {
      "id": "795aaa7a-115c-4dab-8771-f6b89787f124",
      "name": "Report Extraction Failure",
      "type": "n8n-nodes-base.slack",
      "position": [
        1008,
        256
      ],
      "parameters": {
        "text": "=*:x: Could not read a document for archiving*\n*Email:* {{ $(\"Fetch Document Emails\").item.json.subject }}\nPlease file this document manually.",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "#accounting"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.5
    },
    {
      "id": "a291fd22-f8e7-4722-ba49-cd85670fc038",
      "name": "Overview Sticky",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -464,
        -176
      ],
      "parameters": {
        "width": 440,
        "height": 800,
        "content": "## Archive documents for Japan's Electronic Books Preservation Act with Gemini and Drive\n\n### How it works\nJapan's Electronic Books Preservation Act requires every electronically received invoice, receipt or contract to be stored so it can be searched by transaction date, counterparty and amount. This template picks up emailed attachments, extracts the PDF text, and uses a Basic LLM Chain with Google Gemini to read those three mandatory fields plus the document type. It builds a compliant file name, refuses to file anything with missing metadata, writes a searchable index row to Google Sheets, marks the email as filed so nothing is archived twice, and a Switch stores the file in the correct Drive folder per document type.\n\n### Setup\n1. Connect Gmail, Google Gemini (PaLM) API, Google Drive, Google Sheets and Slack.\n2. Create Drive folders for invoices, receipts and contracts and pick them in the three Drive nodes.\n3. Point the Sheets node at your index spreadsheet and choose your accounting Slack channel.\n\n### Customization tips\nAdd a timestamp service, or extend the index with a tax-rate column."
      },
      "typeVersion": 1
    },
    {
      "id": "e43769b8-6b94-4445-9d63-65711812e22a",
      "name": "S1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        -176
      ],
      "parameters": {
        "color": 7,
        "width": 500,
        "height": 880,
        "content": "## 1. Collect documents\nPoll Gmail for unread emails with attachments and extract the PDF text."
      },
      "typeVersion": 1
    },
    {
      "id": "e0189a6c-6a95-4522-92a4-4f9e619a739b",
      "name": "S2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        -176
      ],
      "parameters": {
        "color": 7,
        "width": 644,
        "height": 880,
        "content": "## 2. AI metadata extraction (Basic LLM Chain)\nGemini reads the three legally required fields - transaction date, counterparty and amount - plus the document type. Failures are reported to Slack."
      },
      "typeVersion": 1
    },
    {
      "id": "7837494b-daa7-4223-99d0-81402159ff82",
      "name": "S3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1200,
        -176
      ],
      "parameters": {
        "color": 7,
        "width": 940,
        "height": 880,
        "content": "## 3. Validate, index & de-duplicate\nBuild the compliant file name, block anything missing required metadata, write the searchable index row, and mark the email as filed so it is never archived twice."
      },
      "typeVersion": 1
    },
    {
      "id": "77d4a57e-1295-47e1-9f20-d69487b3f2bf",
      "name": "S4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2160,
        -176
      ],
      "parameters": {
        "color": 7,
        "width": 740,
        "height": 880,
        "content": "## 4. Store in the right folder\nA Switch files invoices, receipts and contracts into their own Drive folders. Anything unclassified is flagged for a human."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": true,
    "executionOrder": "v1"
  },
  "versionId": "c1e7a34f-221d-49cd-b714-480baf67b321",
  "nodeGroups": [],
  "connections": {
    "Mark Email as Filed": {
      "main": [
        [
          {
            "node": "Route by Document Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Document Text": {
      "main": [
        [
          {
            "node": "Extract Filing Metadata with Gemini",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Document Emails": {
      "main": [
        [
          {
            "node": "Extract Document Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filing Metadata Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Extract Filing Metadata with Gemini",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Index for Legal Search": {
      "main": [
        [
          {
            "node": "Mark Email as Filed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Document Type": {
      "main": [
        [
          {
            "node": "Archive to Invoices Folder",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Archive to Receipts Folder",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Archive to Contracts Folder",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Flag Unclassified Document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Required Metadata": {
      "main": [
        [
          {
            "node": "Index for Legal Search",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Report Incomplete Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Filing Metadata with Gemini",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Build Compliant File Name": {
      "main": [
        [
          {
            "node": "Check Required Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Filing Metadata with Gemini": {
      "main": [
        [
          {
            "node": "Build Compliant File Name",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Report Extraction Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}