See llms.txt for all machine-readable content.

Back to Templates

Extract and validate invoice data with Google Gemini and Google Sheets

Created by

Created by: Yogyata Mantri || yogyatamantri
Yogyata Mantri

Last update

Last update 17 hours ago

Categories

Share


Quick overview

This workflow accepts an invoice file via an n8n chat trigger, uses Google Gemini to extract structured invoice data, validates it against an existing Google Sheets log, appends valid line items to the sheet, and sends a formatted confirmation or failure alert via Gmail.

How it works

  1. Receives a chat message with an uploaded invoice document.
  2. Uses Google Gemini to extract the vendor, invoice number, date, total amount, and all line items as a clean JSON object.
  3. Converts the extracted JSON into per-line-item records, builds an HTML invoice summary for email, and loads existing rows from Google Sheets for comparison.
  4. Validates each item by checking for missing critical fields and whether the invoice number already exists in Google Sheets.
  5. If validation passes, appends each line item as a new row in Google Sheets and then prepares a summary of the inserted data.
  6. Sends a Gmail message with the formatted HTML invoice breakdown when processing succeeds, or sends a Gmail alert email explaining the validation failure.

Setup

  1. Add Google Gemini (PaLM) API credentials and ensure the chat trigger allows file uploads so the invoice is available as binary input.
  2. Connect Google Sheets OAuth credentials and update the target spreadsheet and sheet tab to match your invoice log and column headers.
  3. Connect Gmail OAuth2 credentials and update the recipient email address and subject/message templates as needed.

Requirements

  • n8n Version: n8n v1.0+ or n8n Cloud.
  • Google Cloud Account: Active account with access to Google Sheets and Gmail APIs.
  • Google Gemini API Key: An active API key from Google AI Studio (Gemini) to handle the text and data extraction from the invoices.
  • Google Sheets: A pre-configured Google Spreadsheet containing columns for your invoice log (e.g., Invoice Number, Vendor, Date, Total Amount, and Line Items) to run the duplicate and missing data checks.
  • Gmail Account: An active Gmail connection (OAuth2 or App Password) to send out the HTML success summaries and error alerts.
  • Testing Files: A few sample invoice documents (PDFs or images) to upload via the Chat Trigger for testing

Customization

  • Modify Extraction Fields: If you want to extract additional information (like Tax ID, Discount percentage, or Currency code), simply update the JSON schema inside the Google Gemini node system prompt to include these key-value pairs.
  • Custom Validation Rules: You can adjust the "Validation Code" or "IF" node to enforce custom logic, such as automatically flagging invoices with values exceeding $10,000, or routing specific vendors to different Google Sheets.
  • Email Template Branding: Update the Gmail node's HTML code to customize the layout, color palette, and logo of the email breakdown to match your company's official brand guidelines.
  • Alternative Triggers: Swap the n8n Chat Trigger with a Gmail Trigger (to automatically process incoming invoices from email attachments) or a Google Drive Trigger (to process files uploaded to a shared folder).

Additional info

Prompt Tuning: The accuracy of the data extraction depends on the quality of the invoice file. If you are using handwritten or poorly scanned invoices, you may want to update the Gemini node prompt to enforce stricter parsing rules.

Testing Tip: Before activating the production workflow, test it with 2-3 sample invoices using the Chat interface to ensure that the column names in your Google Sheets perfectly match the data keys generated by the Gemini node.

Error Handling: If an invoice fails validation (e.g., duplicate invoice number or missing totals), the workflow will automatically skip the Google Sheets step and immediately trigger the Gmail alert node, saving you from cluttered data logs.