Automatic Invoice Archiving & AI Data Extraction to Google Sheets - n8n Workflow

Use this powerful n8n workflow to automate invoice processing. Schedule checks on Gmail, archive files to Google Drive/SFTP, extract structured data using AI agents, and log everything to Google Sheets. Optimized n8n templates for finance.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Small business owners or bookkeepers needing to automate repetitive invoice data entry.
Users familiar with n8n seeking advanced AI data extraction n8n templates.
Finance teams who require structured data from PDFs for reconciliation.
Anyone wanting a robust, scheduled n8n trigger to monitor specific email accounts for attachments.

Overview

Managing incoming invoices via email can be time-consuming, involving manual downloading, archiving, and data entry. This specialized n8n workflow solves this pain point by providing a comprehensive, hands-free automation solution.

It leverages a scheduled n8n trigger to check a designated Gmail account, filters specifically for PDF invoices, securely archives those files to Google Drive (and optionally SFTP), and then uses a sophisticated AI agent via the n8n node to perform detailed, field-level data extraction—including complex line item validation. The extracted data is then neatly formatted and appended to a Google Sheet for reporting and tracking. This n8n automation eliminates manual errors and saves significant time, making it one of the most valuable n8n templates for financial operations.

How it Works

This automation is initiated by a schedule n8n trigger and executes the following steps:


  1. Schedule Trigger: The n8n workflow starts on a set schedule (e.g., hourly).

  2. Get many messages (Gmail n8n node): It queries Gmail for new messages, filtering specifically by a designated sender address.

  3. Filter - contains attachment: A Filter n8n node ensures that only emails containing binary attachments (the PDF invoice) proceed to the next step.

  4. Gmail - Get Invoice: The full email and attachments are downloaded.

  5. Google Drive - Upload: The invoice PDF is immediately archived to a specified Google Drive folder, using dynamic naming based on the sender and date.

  6. Download File (HTTP Request): This step retrieves the binary data necessary for subsequent processing and storage.

  7. FTP/SFTP - Upload (Optional): The file can be simultaneously uploaded to an external SFTP server for redundancy or compliance.

  8. Delete a file (Optional): An optional Google Drive n8n node is configured to delete the temporary file from Google Drive after processing, if desired.

  9. Delete a message (Optional): An optional Gmail n8n node deletes the processed email from the inbox.

  10. Extract from File: The PDF attachment is converted into raw, readable text, preparing it for AI analysis.

  11. AI Agent - Fields: The core logic. This n8n node uses a powerful system prompt via an OpenRouter connection to extract highly structured JSON data from the invoice text. Crucially, it includes logic to verify extraction completeness by ensuring the sum of all line item totals matches the invoice grand total.

  12. Code - Extract Fields: A custom JavaScript n8n node cleans the AI output, stripping unnecessary markdown and safely parsing the raw output into a reliable JSON object.

  13. Google Sheets - Save: The cleaned, extracted fields (Vendor, Date, Amount, Description) are appended as a new row in the target Google Sheet, finalizing the n8n workflow.

Installation Guide

To use this n8n workflow template, follow these steps:


  1. Import the JSON: Copy the provided JSON data and import it directly into your n8n instance.

  2. Configure Credentials:

Gmail: Set up your OAuth2 credentials for the Gmail n8n node and link them to the workflow. In the 'Get many messages' n8n node, replace SENDEREMAILADDRESS with the email address of your invoice sender.
Google Drive: Connect your Google Drive OAuth2 account and specify the target Folder ID for archiving in the 'GoogleDrive-upload-file' n8n node.
Google Sheets: Connect a Service Account credential. Update the Document ID and Sheet Name in the 'GoogleSheets_save' n8n node.
AI Provider (OpenRouter/OpenAI): Connect your preferred LLM provider API key to the 'OpenRouter Chat Model' n8n node.
* SFTP (Optional): If using the FTP n8n node, configure your SFTP credentials and update the destination path.

  1. Activate: Save the n8n workflow and activate the Schedule n8n trigger.

Node Details

Schedule Trigger (n8n trigger): Initiates the flow on a recurring hourly basis.
Gmail: Get many messages (n8n node): Fetches messages from a specific sender (e.g., sender: SENDEREMAILADDRESS).
Filter: containsattachment (n8n node): Uses a condition to check if the binary data object exists (=$('Get many messages').item.binary), ensuring only emails with attachments are processed.
Google Drive: upload-file (n8n node): Saves the PDF attachment. Key configuration includes using dynamic expressions for the file name: ={{ $json.from.value[0].name}}-{{ $json.date }}.
Extract from File (n8n node): Converts PDF binary content into text format, using the 'pdf' operation.
AI Agent - fields (n8n node): This LangChain n8n node is critical. It uses detailed system instructions to extract numerous structured fields (e.g., invoice
number, vendorname, totalamount, and validated lineitems). It enforces a strict JSON output format.
CodeextractFields (n8n node): A custom code node that robustly cleans up the AI's raw text response, safely parses the JSON, and handles potential formatting errors from the LLM, making the data reliable for the next n8n node.
GoogleSheetssave (n8n node): Appends the extracted structured data. Key configuration defines the column mapping: Date:={{ $json.invoicedate }}, Amount:={{ $json.totalamount }}, Vendor:={{ $json.vendorname }}, using the output from the previous n8n node.

Related n8n Workflows

Free

Nodes: 12 Nodes
Updated: December 26 2025
View all
Created by

Cybersecurity Analyst passionate about automation, cloud, and infrastructure. Since discovering n8n, I automate everything I can: workflows, processes, integrations, and repetitive tasks. I love building, optimizing, and making systems more secure, faster, and scalable.

Featured*