CORTEXFLOW · FREE FOREVER

Automation

Invoice Auto-Pay Guardian — 3-Way Match, Caps & Audit Trail

↓ Download workflow JSON

Import in n8n: Workflows → ⋯ → Import from file, then add your credentials. Free download with a free account.

How it works

Most teams either pay every invoice by hand (slow) or let a workflow pay anything that arrives (dangerous). This template draws the line the n8n community converged on: match-then-release with guardrails. Money moves only when the invoice, the purchase order, and the receiving record agree line by line — and only inside hard caps.

  1. STEP 1 — Invoice intake — a Gmail Trigger watches for emails with subject:invoice and an attachment. The PDF is downloaded, its text extracted, and a Code node parses the invoice number, supplier, PO number, amount, currency, due date, and bank account last-4.
  2. STEP 2 — Log, dedupe + kill-switch — the invoice is appended to the PayablesLog sheet as RECEIVED. The workflow then reads the whole log: if the same invoice number + supplier is already there, it’s a duplicate. If any row is BLOCKED, or a PAID row has lost its settlement status, the kill-switch fires — the invoice is marked BLOCKED, a Telegram alert goes out, and autopay pauses until you clear the log. Nothing is paid.
  3. STEP 3 — Supplier check — the supplier is looked up in the Suppliers allowlist. A new supplier, a supplier marked blocked, or bank details that changed since the stored record (last-4 mismatch) all route the invoice to the REVIEW queue. This is the single most important fraud control in the workflow: changed bank details never auto-pay.
  4. STEP 4 — 3-way match (AI) — an AI Agent (gpt-4o-mini + Structured Output Parser) compares the invoice against the PurchaseOrders and Receiving sheets line by line — flagging short quantities, substitutions, price changes, missing lines, and credit memos — and returns a structured verdict (EXACT_MATCH / PARTIAL_MATCH / MISMATCH) with per-line evidence quotes.
  5. STEP 5 — Caps & approval gate — the invoice is auto-approved only when the match is exact, the supplier is known, the amount is at or under your per-payment cap, and today’s paid + approved total stays under your daily cap. Anything else goes to the REVIEW queue with a Telegram message and an approve/reject form. A human approval sends the invoice straight into the payment step; a rejection marks it REJECTED.
  6. STEP 6 — Payment + audit — the payment is released via HTTP Request to your payment provider with an Idempotency-Key header built from the invoice number, supplier, and amount — so a retried execution can never pay the same invoice twice. The workflow verifies the settlement response, then updates the log to PAID (or QUEUED_FOR_PAYMENT with UNSETTLED if settlement can’t be confirmed — which trips the kill-switch next run) with the payment reference and policy version. A Telegram summary closes the run.
  7. STEP 7 — Review queue & alerts — review items, blocked duplicates, and paid batches each get their own Telegram message, so finance always knows what was paid, what is waiting, and what was stopped.

Status flow in Sheets: RECEIVED → MATCHING → APPROVED → QUEUED_FOR_PAYMENT → PAID / REVIEW / BLOCKED / REJECTED

Set up steps

You’ll need:

  • n8n (self-hosted or cloud) — n8n.io
  • A Gmail account that receives supplier invoices — n8n Credentials → Gmail OAuth2
  • An OpenAI API key — platform.openai.com → API keys
  • Google Sheets OAuth2 credentials — n8n Credentials → Google Sheets OAuth2
  • A Telegram bot — message @BotFather, send /newbot, copy the token; get your chat ID from @userinfobot
  • A payment provider with API access — e.g. Stripe (use test mode first) or Wise — you need an API key and the provider’s base URL

Steps:

  1. Import the workflow — in n8n, Workflows → Import from File → select invoice-auto-pay-guardian.json.
  2. CONFIG — Policy & Caps — open the Set node and fill in:
    • per_payment_cap — max auto-payable amount per invoice (default 5000)
    • daily_cap — max total auto-paid per day (default 20000)
    • currency — e.g. USD
    • policy_version — bump this (v1.2 → v1.3) whenever you change a rule, so the audit trail shows which policy paid each invoice
    • spreadsheet_id — paste your Google Sheet ID
    • payment_provider_base_url — e.g. https://api.stripe.com (no trailing slash)
    • payment_api_key — paste your provider API key (test-mode key first)
    • telegram_chat_id — paste your Telegram chat ID
    • finance_email — your finance inbox, for reference in review messages
  3. Create the Google Sheet — one spreadsheet with four tabs and these exact headers:
    • PayablesLog — Date Received | Invoice Number | Supplier | PO Number | Amount | Currency | Due Date | Status | Idempotency Key | Payment Reference | Settlement Status | Review Reason | Match Verdict | Policy Version
    • Suppliers — Supplier | Bank Account Last 4 | Bank Details Hash | Status (add your trusted suppliers here; Status = active or blocked)
    • PurchaseOrders — PO Number | Supplier | Line Description | Quantity | Unit Price | Line Total | Status
    • Receiving — PO Number | Line Description | Quantity Received | Received Date | Received By Then select your Google Sheets credential in every Sheets node (the spreadsheet is picked by the spreadsheet_id in the CONFIG node — select the same sheet in each node).
  4. Credentials — select your Gmail credential in Gmail Trigger: Invoice Email and Gmail: Download Attachment; your OpenAI credential in OpenAI Chat Model — 3-Way Match; your Telegram credential in the three Telegram nodes.
  5. Test before you activate — send yourself a test invoice email (subject contains invoice, PDF attached) with an amount above your per-payment cap: it must land in REVIEW, never in PAID. Then send one that matches a PO + receiving record exactly and is under the cap: it should pay (in test mode) and log PAID with a payment reference. Only switch the provider key to live mode after both tests pass.
  6. Activate the workflow. The Gmail Trigger polls every minute.

Customize it

  • Lower the caps to start — per_payment_cap: 500 and daily_cap: 2000 for the first month, then raise them once you trust the match verdicts. The caps are the cheapest insurance in the whole workflow.
  • Stricter matching — the AI agent only recommends AUTO_APPROVE on a perfect line-by-line match; if your POs use different units or descriptions than invoices, add a normalization step in Code — Normalize Invoice Fields (e.g. strip units, lowercase descriptions) before the agent sees them.
  • Partial matches — today a PARTIAL_MATCH goes to review. If you trust small variances, add an IF branch that auto-approves when total_variance is within a tolerance you set in the CONFIG node (match_tolerance_percent).
  • Approval via Slack or email instead of Telegram — swap the Telegram — Review Request node for a Slack or Gmail node; the Wait — Human Approval form works the same way, and the approval link in the message is {{ $execution.resumeUrl }}.
  • Reconciliation — add a scheduled run that pulls your bank statement and compares it against PayablesLog rows with Settlement Status = SETTLED; any PAID row that never appears on the statement should flip the kill-switch. (Our Daily Reconciliation Monitor covers the statement side of this.)
  • Never auto-pay these — new suppliers and changed bank details are hard-routed to review by design. If you ever feel tempted to relax that rule, don’t: it is the one control that stops the most common invoice fraud (a spoofed invoice with the attacker’s bank details).
  • Multi-currency — the workflow records the invoice currency and passes it to the provider, but the caps are compared in one currency. If you pay in several currencies, add a conversion step before Code — Build Match Decision or keep separate caps per currency.

Workflow nodes

This workflow is built from 35 nodes. Here's what each one does:

#NodeTypeWhat it does
1 Gmail Trigger: Invoice Email Gmail Trigger Starts the workflow when a new email arrives in Gmail.
2 CONFIG — Policy & Caps Set Holds editable configuration values — fill in your own settings here before activating.
3 Gmail: Download Attachment Gmail Sends or reads email through your Gmail account.
4 Extract from File — Invoice PDF Extract from File Extracts text content from uploaded files (PDF, docs).
5 Code — Normalize Invoice Fields Code Runs custom JavaScript to transform, filter, or compute data between steps.
6 Google Sheets — Log Invoice RECEIVED Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
7 Google Sheets — Read Payables Log Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
8 Code — Dedupe + Kill-Switch Check Code Runs custom JavaScript to transform, filter, or compute data between steps.
9 IF — Duplicate or Kill-Switch? IF Splits the flow into branches based on a condition.
10 Google Sheets — Read Suppliers Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
11 Code — Supplier Check Code Runs custom JavaScript to transform, filter, or compute data between steps.
12 IF — Supplier OK? IF Splits the flow into branches based on a condition.
13 Google Sheets — Read Purchase Orders Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
14 Google Sheets — Read Receiving Records Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
15 AI Agent — 3-Way Match Verdict AI Agent The AI brain — reasons over the task and calls tools to get it done.
16 OpenAI Chat Model — 3-Way Match OpenAI Chat Model The language model powering the AI agent.
17 Structured Output Parser — Match Verdict Structured Output Parser Forces the AI to return clean, structured JSON instead of free text.
18 Code — Build Match Decision Code Runs custom JavaScript to transform, filter, or compute data between steps.
19 IF — Auto-Approve? IF Splits the flow into branches based on a condition.
20 Merge — Approved Payments Merge Combines the outputs of two or more branches back into one flow.
21 Code — Prepare Payment Batch Code Runs custom JavaScript to transform, filter, or compute data between steps.
22 HTTP Request — Release Payment HTTP Request Calls an external API or webhook endpoint.
23 Code — Verify Settlement Code Runs custom JavaScript to transform, filter, or compute data between steps.
24 Google Sheets — Update Log PAID Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
25 Code — Build Payment Summary Code Runs custom JavaScript to transform, filter, or compute data between steps.
26 Telegram — Send Summary Telegram Sends you a Telegram message or alert.
27 Code — Flag BLOCKED Code Runs custom JavaScript to transform, filter, or compute data between steps.
28 Google Sheets — Update Log BLOCKED Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
29 Telegram — BLOCKED Alert Telegram Sends you a Telegram message or alert.
30 Code — Set Review Reason Code Runs custom JavaScript to transform, filter, or compute data between steps.
31 Google Sheets — Update Log REVIEW Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.
32 Telegram — Review Request Telegram Sends you a Telegram message or alert.
33 Wait — Human Approval Wait Pauses the workflow until a condition or time is met.
34 IF — Human Approved? IF Splits the flow into branches based on a condition.
35 Google Sheets — Update Log REJECTED Google Sheets Reads from or writes to a Google Sheet — this workflow's log and state store.

Setup instructions

1. Add your credentials

In n8n go to Credentials → New, create each credential below, then open the named node and select it:

NodeCredential to add
Gmail Trigger: Invoice Email Gmail OAuth2
Gmail: Download Attachment Gmail OAuth2
Google Sheets — Log Invoice RECEIVED Google Sheets OAuth2
Google Sheets — Read Payables Log Google Sheets OAuth2
Google Sheets — Read Suppliers Google Sheets OAuth2
Google Sheets — Read Purchase Orders Google Sheets OAuth2
Google Sheets — Read Receiving Records Google Sheets OAuth2
OpenAI Chat Model — 3-Way Match OpenAI API
Google Sheets — Update Log PAID Google Sheets OAuth2
Telegram — Send Summary Telegram Bot API
Google Sheets — Update Log BLOCKED Google Sheets OAuth2
Telegram — BLOCKED Alert Telegram Bot API
Google Sheets — Update Log REVIEW Google Sheets OAuth2
Telegram — Review Request Telegram Bot API
Google Sheets — Update Log REJECTED Google Sheets OAuth2

2. Fill in your config values

Open the config node listed below and replace each placeholder with your own value before activating the workflow:

NodeFieldWhat to paste
CONFIG — Policy & Caps spreadsheet_id PASTE_YOUR_SPREADSHEET_ID
Your spreadsheet id
CONFIG — Policy & Caps payment_provider_base_url PASTE_YOUR_PAYMENT_PROVIDER_BASE_URL
Your payment provider base url
CONFIG — Policy & Caps payment_api_key PASTE_YOUR_PAYMENT_API_KEY
Your payment api key
CONFIG — Policy & Caps telegram_chat_id PASTE_YOUR_TELEGRAM_CHAT_ID
Your telegram chat id
CONFIG — Policy & Caps finance_email PASTE_YOUR_FINANCE_EMAIL
Your finance email

How this workflow starts

  • Gmail Trigger: Fires automatically on its event — activate the workflow and connect the service.

← All templates