Automation Flow — HUD Document Pipeline
End-to-end: Email Intake → AI Extraction → Database → ERP Export → Email Out
Process Overview — 11 Steps
📧 Email In  →  AI Extraction  →  Database  →  📤 Email Out
01
📧
Email
Received
Gmail API
02
📎
Detect PDF
Attachment
n8n / Inngest
03
☁️
Store PDF
to S3
Cloudflare R2
04
🗂️
Create DS1
Record
Supabase
05
🔄
Queue
Extract Job
Inngest
06
📄
Pre-process
PDF
pdfplumber
07
🤖
Claude AI
Extraction
Claude API
08
Validate
Schema
Pydantic
09
💾
Save DS1–DS4
to Database
Supabase PG
10
📊
Generate
Transaction CSV
Python / pandas
11
📤
Send Reply
Email
Gmail API
Swimlane Detail — by System
System / Actor
① Intake & Store
② AI Processing
③ Persist Data
④ Output & Notify
📧 Email
System
Gmail API
Step 01 — Receive
  • Watch inbox for new emails
  • Filter by subject keywords (HUD, ALTA, Settlement)
  • Detect PDF attachment
  • Extract sender, subject, date
Gmail API Watch
Step 11 — Send Reply
  • Reply to original sender
  • Attach original HUD PDF
  • Attach transactions CSV (DS4)
  • Include extraction summary
Gmail API Send
⚙️ Workflow
Orchestration
n8n / Inngest
Step 02 — Trigger
  • Gmail webhook fires on new email
  • Parse email metadata
  • Download PDF attachment
  • Trigger pipeline workflow
Webhook trigger n8n Gmail node
Step 05 — Queue Job
  • Enqueue async extraction job
  • Pass S3 file reference
  • Set retry policy (3× on fail)
  • Track job status
Inngest event
Step 08+ — Validate
  • Check confidence scores
  • Route low-confidence to review queue
  • Mark status: auto / needs_review
n8n if-node
Step 10+ — Compose
  • Assemble output files
  • Trigger email send node
  • Log completion event
n8n email node
☁️ File
Storage
Cloudflare R2 / S3
Step 03 — Store PDF
  • Upload original PDF (immutable)
  • Path: /originals/{doc_id}.pdf
  • Generate checksum (SHA-256)
  • Store signed URL reference
R2 PUT Zero egress fees
Step 10 — Store CSV
  • Save generated CSV
  • Path: /exports/{doc_id}_erp.csv
  • Generate download URL
R2 PUT
🤖 AI
Engine
Claude API + pdfplumber
Step 06 — Pre-process
  • pdfplumber extracts raw text
  • Detect & extract tables
  • Normalise whitespace & layout
  • Prepare prompt context
pdfplumber PyMuPDF
Step 07 — Extract
  • Send text to Claude claude-sonnet-4-6
  • Prompt: extract DS1–DS4 JSON
  • Returns structured JSON + confidence
  • Validate with Pydantic schema
Claude claude-sonnet-4-6 JSON output
🗄️ Database
Supabase / PostgreSQL
Step 04 — DS1 Record
  • Insert document registry row
  • Status: "ingested"
  • Store source_file, received_date
INSERT DS1
Step 09 — Save All
  • Upsert DS2 — transaction details
  • Bulk insert DS3 — line items (47 rows)
  • Bulk insert DS4 — ERP entries
  • Update DS1 status → "extracted"
UPSERT DS2 BULK DS3 BULK DS4
📤 Output
Generator
Python / pandas
Step 10 — Generate CSV
  • Query DS4 ERP entries by doc_id
  • Format: description, Dr/Cr, amount, date, acct code
  • Calculate totals & validate balance
  • Write transactions_{doc_id}.csv
  • Attach to outbound email
pandas to_csv ERP-ready format
Tools Used — Don't Build the Wheel
📧
Gmail API
Email trigger & reply
Watch inbox Read attachments Send with files Free tier
⚙️
n8n (self-hosted)
Workflow orchestration
Visual flows Open-source $5/mo VPS 200+ connectors
🤖
Claude API
AI extraction engine
claude-sonnet-4-6 JSON output Pay-per-use Native PDF
🗄️
Supabase
Managed PostgreSQL DB
Free tier JSONB fields pgvector Auto REST API
☁️
Cloudflare R2
PDF & file storage
S3-compatible Zero egress Free 10GB Signed URLs