🧾 Invoice processing: Email to PostgreSQL database with GPT-4o & Discord alerts

⚑ 614 views · 🧾 Invoice Processing

πŸ’‘ Pro Tip β€” HTTP Request scraping tends to break when sites update their markup. If you’re scraping a major platform, check if ScraperNode covers it β€” it has maintained scrapers for LinkedIn, Instagram, TikTok, YouTube, and 20+ other platforms that return structured data.

View All Scrapers

Description

AI-Powered Invoice Processing: from Email to Database & Chat Notifications

Automatically process PDF invoices directly from your email inbox. This workflow uses AI to extract key data, saves it to a PostgreSQL database, and instantly notifies you about the new document in your preferred chat application.

The workflow listens for new emails, fetches PDF attachments, and then passes their content to a Large Language Model (LLM) for intelligent recognition and data extraction. Finally, the information is securely archived in the database, and a summary of the invoice is sent as a notification.

> πŸ“ This workflow is highly customizable.
> It uses PostgreSQL, OpenAI (GPT), and Discord by default, but you can easily swap these components.
> Feel free to use a different database like MySQL or Airtable, another AI model provider, or send notifications to Slack, MS Teams, or any other chat platform.

> ⚠️ Note: If the workflow fails to extract data correctly from invoices issued by certain companies, you may need to adjust the prompt used in the Basic LLM Chain node to improve parsing accuracy.


Use Case


Features


Setup Instructions

⚠️ API Access & Costs

To use the AI extraction feature, you need an API key from a provider like OpenAI.
Most providers charge for access to language models. You’ll likely need a billing account.


1. PostgreSQL Database Configuration

Ensure your database has the following tables:

-- Table for companies (invoice issuers)
CREATE TABLE company (
    id SERIAL PRIMARY KEY,
    tax_number VARCHAR(255) UNIQUE NOT NULL,
    name VARCHAR(255),
    address TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Table for invoices
CREATE TABLE invoice (
    id SERIAL PRIMARY KEY,
    company_id INTEGER REFERENCES company(id),
    invoice_number VARCHAR(255) NOT NULL,
    -- Add other fields: total_to_pay, currency, due_date
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(company_id, invoice_number)
);

Then, in n8n, create a credential for your PostgreSQL DB.


2. Email (IMAP) Configuration

In n8n, add credentials for the email account that receives invoices:


3. AI Provider Configuration


4. Chat Notification (Discord)


Placeholders and Fields to Fill

PlaceholderDescriptionExample
YOUR_EMAIL_CREDENTIALSYour IMAP email account in n8nMy Invoice Mailbox
YOUR_OPENAI_CREDENTIALSAPI credentials for AI modelMy OpenAI Key
YOUR_POSTGRES_CREDENTIALSYour PostgreSQL DB credentials in n8nMy Production DB
YOUR_DISCORD_WEBHOOKWebhook URL for your chat systemhttps://discord.com/api/webhooks/…

Testing the Workflow

  1. Send a test invoice to the inbox as a PDF attachment
  2. Run the workflow manually in n8n and check if the IMAP node fetches the message
  3. Verify AI Extraction – inspect the LLM output (e.g., GPT node) and confirm structured JSON
  4. Check the DB – ensure new rows appear in company and invoice
  5. Check the chat – verify the invoice summary appears in the chosen channel

Customization Tips

πŸ”— Nodes Used

Email Trigger (IMAP), Postgres, Discord, Basic LLM Chain, OpenAI Chat Model, Structured Output Parser

πŸ“₯ Import

Download workflow.json and import into n8n: Workflow menu β†’ Import from File

πŸ“– Importing guide Β· πŸ”‘ Credential setup