DEV Community

Cover image for How I Built an AI Invoice Risk Predictor with Aurora PostgreSQL + Vercel in 4 Days
Yaswanth
Yaswanth

Posted on

How I Built an AI Invoice Risk Predictor with Aurora PostgreSQL + Vercel in 4 Days

Every freelancer knows the sinking feeling: you delivered the work, sent the invoice, and now you're just... waiting. Will they pay on time? In two weeks? Ever?

I built Clariva to answer that question before the due date arrives.

The Problem

$3.1 trillion in B2B invoices are overdue globally at any given moment. Freelancers and small businesses have zero tools to predict which clients will pay late. They chase every invoice the same way — polite email, wait, follow up, wait, panic. It's reactive, stressful, and expensive.

What Clariva Does

Clariva is an AI-powered invoice intelligence platform. When you create an invoice, three things happen automatically:

AI Extraction — Paste raw invoice text from an email or PDF, and Groq's LLM pulls out the client name, amount, due date, and line items. No manual data entry.

Risk Scoring — Every invoice gets a 0–100 risk score instantly, calculated from the client's payment history: how many invoices they've paid, how many were late, and their average days-to-pay. The AI then writes a plain-English explanation of why the score is what it is.

Smart Reminders — One click generates a personalized follow-up email for any invoice. The AI adjusts the tone based on how overdue the invoice is — polite nudge for a day late, firmer notice for 30 days. You can even bulk-generate reminders for all your overdue invoices at once.

The Stack

I wanted the "zero stack" — zero idle cost, zero ops, zero infrastructure management. Here's what I chose:

Next.js 14 (App Router) on Vercel — serverless functions for every API route, automatic edge deployment
Amazon Aurora PostgreSQL Serverless v2 — scales to zero ACUs when idle, scales up instantly under load. My 5-table schema (users, clients, invoices, reminders, payment_history) runs real SQL aggregations for the dashboard analytics
Groq API (llama-3.3-70b-versatile) — handles extraction, risk explanations, reminder generation, business insights, and client assessments
NextAuth v5 — JWT-based auth with credentials provider
Drizzle ORM — type-safe database queries
v0 — generated the initial UI components

Why Aurora Serverless v2

This was a deliberate choice. I needed a real relational database — not a document store — because invoice data is inherently relational: users have clients, clients have invoices, invoices have payment histories. Foreign keys, JOINs, and indexed queries matter when you're computing risk scores from aggregated payment patterns.

Aurora Serverless v2 gave me three things:

Scale-to-zero — During development and between demos, I pay almost nothing. The cluster idles at 0.5 ACUs.
Production-grade performance — The same database engine that powers Amazon.com. When judges hit the demo, it scales up seamlessly.
PostgreSQL compatibility — I used COALESCE, CASE WHEN, GROUP BY with date_trunc, and JSONB columns for line items. Standard Postgres, zero vendor lock-in.

The connection to Vercel is a single DATABASE_URL environment variable with SSL enabled. The Drizzle ORM schema maps directly to Aurora tables, and I use a global connection pool (max 1 connection) optimized for serverless cold starts.

The AI Features That Set Clariva Apart

Beyond basic CRUD, I built five distinct AI features:

AI Extract — Paste unstructured invoice text, get structured data back
AI Risk Scoring — 0–100 score with a written explanation
AI Reminders — Personalized collection emails (single or bulk)
AI Business Insights — Dashboard card with 3 actionable insights based on your invoice data
AI Client Assessment — Per-client reliability analysis with recommended payment terms

Each one calls Groq with the user's real data from Aurora — it's not a generic chatbot wrapper. The AI sees actual payment patterns and gives specific, actionable advice.

What I Learned

Building a full-stack SaaS in 4 days forced some hard choices. The biggest lesson: NextAuth v5 on Vercel requires trustHost: true and explicit secret passing to middleware. I lost hours debugging why authenticated users were bouncing back to the login page — the session cookie was being set with one secret and decoded by middleware with another. If you're using Auth.js v5 on Vercel, save yourself the pain: set trustHost: true in your config and pass the same secret to getToken() in your middleware.

Try It

Live demo: https://clariva-delta.vercel.app (click "View demo" to skip registration)
Source: https://github.com/yaswanthme007/clariva

I created this content for the H0: Hack the Zero Stack hackathon. #H0Hackathon

Top comments (0)