AI Invoice Processing Agent: Gmail to Google Sheets via LlamaParse & OpenAI
Integrates with:
Overview
Unlock Automated Invoice Data Entry with this AI Agent
Say goodbye to tedious manual invoice processing! This AI-driven agent transforms how you handle accounts payable. It diligently monitors your Gmail for incoming PDF invoices (e.g., from specific senders like invoices@paypal.com
as configured in the template). Once an invoice arrives, it's first processed by LlamaParse, an advanced service designed to accurately convert complex PDFs, including those with intricate tables and figures, into structured markdown.
Then, the OpenAI GPT-3.5-Turbo model steps in. Guided by a customizable extraction schema (via the Structured Output Parser node), it intelligently pulls out crucial details like invoice date, number, supplier information, line items, and totals from the markdown content. This extracted data is then automatically appended as a new row in your designated Google Sheet, ready for reconciliation or further analysis. The original email is tagged in Gmail (e.g., with 'invoice synced') to prevent duplicate processing, ensuring a smooth and efficient workflow.
Key Features & Benefits
- Automated Email Polling: Watches Gmail for new invoices with PDF attachments based on your filters (sender, attachment presence, not already processed).
- Advanced PDF Parsing: Utilizes LlamaParse to convert even complex PDF invoices (with tables and figures) into clean markdown, maximizing data accessibility for the LLM.
- AI-Powered Data Extraction: Leverages OpenAI's GPT-3.5-Turbo (configurable) and a Langchain Structured Output Parser to accurately identify and extract specific invoice fields according to a defined JSON schema.
- Customizable Extraction Schema: Easily define exactly which data points you need (e.g., invoice date, supplier name, line items, VAT, totals) by modifying the JSON schema in the 'Structured Output Parser' node.
- Direct Google Sheets Integration: Seamlessly appends extracted invoice data to your chosen Google Sheet for easy tracking, accounting, or reporting.
- Duplicate Prevention: Automatically labels processed emails in Gmail to avoid redundant operations and keep your inbox organized.
- Robust Processing Logic: Includes steps to check LlamaParse job status and implement waits to respect service rate limits, ensuring reliable operation.
Use Cases
- Automating data entry for supplier invoices into accounting reconciliation spreadsheets.
- Streamlining accounts payable by capturing all necessary invoice details without manual intervention.
- Building a real-time feed of incoming invoice data for financial dashboards and cash flow management.
- Reducing errors and delays in invoice processing for faster payment cycles and improved supplier relations.
- Freeing up valuable time for founders and finance teams by automating a repetitive, error-prone task.
Prerequisites
- An n8n instance (Cloud or self-hosted).
- OpenAI API Key with access to a suitable model (e.g.,
gpt-3.5-turbo-1106
orgpt-4
). - LlamaParse (LlamaCloud) API Key. You can sign up at
https://cloud.llamaindex.ai/
(their free tier typically allows a certain number of daily parses). - Gmail account with OAuth2 credentials configured in n8n for triggering email reads and applying labels.
- Google Sheets account with OAuth2 credentials configured in n8n for appending data.
- A pre-existing Google Sheet with columns named to match the data you intend to extract (see the 'Structured Output Parser' node's schema for field names).
- A Gmail label (e.g.,
invoice synced
) created in your Gmail account beforehand. This label is used by the workflow to mark processed emails.
Setup Instructions
- Download the n8n workflow JSON file.
- Import the workflow into your n8n instance.
- Configure Gmail Trigger ('Receiving Invoices' node):
- Authenticate your Gmail account using OAuth2.
- Adjust the
Filters
(e.g.,sender
,q
forhas:attachment
) to target your specific invoice emails. The template usesinvoices@paypal.com
as an example. - Ensure 'Download Attachments' is enabled.
- Configure LlamaParse ('Upload to LlamaParse' & other HTTP Request nodes):
- In the 'Upload to LlamaParse', 'Get Processing Status', and 'Get Parsed Invoice Data' nodes, select or create 'LlamaIndex API' credentials for 'HttpHeaderAuth' and enter your LlamaParse API Key as the Bearer Token (Header Name:
Authorization
, Header Value:Bearer YOUR_API_KEY
).
- In the 'Upload to LlamaParse', 'Get Processing Status', and 'Get Parsed Invoice Data' nodes, select or create 'LlamaIndex API' credentials for 'HttpHeaderAuth' and enter your LlamaParse API Key as the Bearer Token (Header Name:
- Configure OpenAI ('OpenAI Model' node):
- Select or create 'OpenAi account' credentials and enter your OpenAI API Key.
- The default model is
gpt-3.5-turbo-1106
. You can change this if desired (e.g., togpt-4-turbo
for more complex documents, keeping in mind cost and speed implications).
- Customize Data Extraction ('Structured Output Parser' node):
- Review and modify the
jsonSchema
parameter. This defines the structure and fields the AI will extract from the invoices. Match this to your Google Sheet columns.
- Review and modify the
- Refine AI Prompt ('Apply Data Extraction Rules' node):
- Inspect the
text
parameter (the prompt). While the default is robust, you can tailor it further for specific invoice layouts or nuances if needed.
- Inspect the
- Configure Google Sheets ('Append to Reconciliation Sheet' node):
- Authenticate your Google Sheets account.
- Enter the
Document ID
of your target Google Sheet. - Select the correct
Sheet Name
. - The
Columns
mapping mode is 'Auto-Map Input Data'; ensure your sheet column headers match the output keys from the 'Map Output' node (which directly passes the structured JSON from the AI).
- Configure Gmail Labeling ('Add "invoice synced" Label' node):
- Ensure it's configured with your Gmail account.
- Verify the
Label IDs
field correctly references the 'invoice synced' label you created in Gmail (you may need to refresh the list in n8n if you just created the label).
- Activate the workflow. It will now monitor for new emails matching your criteria, process the invoice PDFs, and populate your Google Sheet.
Want your own unique AI agent?
Talk to us - we know how to build custom AI agents for your specific needs.
Schedule a Consultation