← projects

$ OCR Expense Tracker

[2025]
TypeScriptNext.jsNode.jsExpressAWS TextractCognitoDynamoDBDocker
code ↗
OCR Expense Tracker

$ cat README.md· github/jntbatra/ocr-aws

AI-Powered Expense Tracker with OCR

Cloud-native expense management application built on AWS using managed AI services, Infrastructure as Code, and containerized deployment.


Badges

AWS Docker Node.js Next.js License


Table of Contents


Overview

The AI-Powered Expense Tracker with OCR is a production-ready cloud-native web application built on Amazon Web Services (AWS).

Users can:

The project demonstrates real-world implementation of:


Problem Statement

Manual expense tracking requires repetitive data entry and often leads to errors.

This project automates receipt digitization using AWS Textract AnalyzeExpense API, reducing manual effort while maintaining scalability and security.


Architecture

The system is designed using a layered cloud-native architecture.

1️⃣ Cloud Architecture (Component View)

Cloud Architecture Diagram


2️⃣ Data Flow Pipeline (Sequence View)

Workflows include:

Data Flow Diagram


3️⃣ Infrastructure Deployment (IaC View)

All AWS resources are provisioned through:

infrastructure.yaml

Infrastructure Diagram


4️⃣ API Diagram

The backend is a single Express app (backend/server.js, container port 3001). Public routes handle Cognito auth; every other route goes through authenticateToken, which verifies the Cognito-issued RS256 JWT against the user pool's JWKS and sets req.user = { id: sub, email, username }.

flowchart LR
    FE["Next.js frontend"] -->|"JSON over HTTP<br/>Authorization: Bearer JWT"| API

    subgraph EC2["EC2 · Docker · Express :3001"]
        API["Express app"]
        API --> PUB["Public routes"]
        API --> MW{"authenticateToken<br/>RS256 + Cognito JWKS"}
        MW -->|"valid"| PROT["Protected routes"]
        MW -->|"missing: 401<br/>invalid: 403"| FE
    end

    PUB -->|"signup / confirm / login<br/>forgot-password"| COG["Cognito User Pool"]
    PROT -->|"POST /upload-url"| S3["S3 receipts bucket"]
    PROT -->|"POST /process"| TX["Textract AnalyzeExpense"]
    TX -.->|"reads object"| S3
    PROT -->|"GET/POST /expenses<br/>POST /save-expense<br/>GET /summary"| DDB[("DynamoDB<br/>ExpenseTable")]
    PROT -->|"POST /trigger-summary"| SNS["SNS topic<br/>(email)"]
    PROT -->|"scan all users"| DDB
    MW -.->|"fetch signing keys"| COG

Endpoint reference

Method Path Auth Request Response Backed by
GET /health no none 200 {status:"ok"} Express
POST /auth/signup no {email, password, username} Cognito result Cognito SignUp
POST /auth/confirm no {email, confirmationCode} Cognito result Cognito ConfirmSignUp
POST /auth/login no {email, password} tokens Cognito InitiateAuth, falls back to AdminInitiateAuth
POST /auth/forgot-password no {email} Cognito result Cognito ForgotPassword
POST /auth/confirm-forgot-password no {email, code, newPassword} Cognito result Cognito ConfirmForgotPassword
POST /upload-url JWT {contentType, filename?} (image types only) {uploadUrl, key} S3 presigned PUT, expires in 300 s, key receipts/<uuid>.<ext>
POST /process JWT {key} {extracted:{merchant,total,date,category,rawText}, receiptUrl}. Nothing is saved. Textract AnalyzeExpense
POST /save-expense JWT {merchant, total, date, category?, receiptUrl?, rawText?} 201 {expenseId, expense} DynamoDB PutItem
POST /expenses JWT {amount, merchant, category, description?, date?} 201 {expenseId} DynamoDB PutItem (manual entry)
GET /expenses JWT none {expenses:[...]} for the caller DynamoDB Query on userId
GET /summary?month=YYYY-MM JWT query month {month,totalAmount,count,categoryBreakdown} DynamoDB Query, aggregated in the app
POST /trigger-summary JWT none {message} DynamoDB Scan + SNS Publish

Status codes used: 400 validation, 401 no token, 403 bad token, 201 created, 500 unexpected.

Receipt flow, call by call

sequenceDiagram
    autonumber
    actor U as User
    participant FE as Next.js
    participant API as Express API
    participant S3 as S3
    participant TX as Textract
    participant DB as DynamoDB

    U->>FE: pick receipt image
    FE->>API: POST /upload-url {contentType}
    API->>API: verify JWT, check image/*
    API->>S3: presign PutObject (300 s)
    API-->>FE: {uploadUrl, key}
    FE->>S3: PUT image to uploadUrl
    FE->>API: POST /process {key}
    API->>TX: AnalyzeExpense (S3 object)
    TX-->>API: ExpenseDocuments
    API->>API: parse TOTAL, date, vendor, category
    API-->>FE: extracted fields (not saved)
    U->>FE: review / edit
    FE->>API: POST /save-expense
    API->>DB: PutItem (userId, expenseId, ...)
    API-->>FE: 201 {expenseId}

5️⃣ Database Schema

Data is stored in a single DynamoDB table, ExpenseTable, defined in backend/infrastructure.yaml. Users are not stored in the app: identity lives in the Cognito user pool, and the JWT sub claim is the userId partition key.

erDiagram
    COGNITO_USER ||--o{ EXPENSE : "owns (userId = JWT sub)"
    EXPENSE }o--o| RECEIPT_OBJECT : "receiptUrl"

    COGNITO_USER {
        string sub PK "Cognito user id"
        string email "login, auto-verified"
    }

    EXPENSE {
        string userId PK "partition key (HASH)"
        string expenseId PK "sort key (RANGE)"
        string createdAt "ISO-8601 timestamp"
        number total "amount"
        string merchant
        string category "Food, Transport, Travel, Groceries, General"
        string date "YYYY-MM-DD"
        string receiptUrl "optional, OCR entries only"
        string rawText "optional, OCR entries only"
        string description "optional, manual entries only"
    }

    RECEIPT_OBJECT {
        string key PK "receipts/uuid.ext in S3"
        string contentType "image/*"
    }

Table definition

Property Value
Table ExpenseTable (CloudFormation AWS::DynamoDB::Table)
Partition key userId (S), the Cognito sub
Sort key expenseId (S)
Billing PAY_PER_REQUEST
Secondary indexes none

Attributes

Attribute Type Required Notes
userId S yes Cognito sub from the verified JWT, never taken from the request body
expenseId S yes OCR//save-expense entries: <ISO timestamp>-<uuid> (sorts by time). Manual /expenses entries: plain <uuid>
createdAt S yes ISO-8601
total N yes Parsed with parseFloat
merchant S yes Textract VENDOR_NAME, default Unknown
category S yes Keyword rules in utils/parser.js, default General
date S yes YYYY-MM-DD, falls back to today if Textract finds none
receiptUrl S no Set for OCR entries only
rawText S no Concatenated Textract summary fields
description S no Manual entries only

Example item:

{
  "userId": "b4a8e4c8-1001-70f3-...",
  "expenseId": "2025-11-21T09:14:02.511Z-3f6c1f0e-...",
  "createdAt": "2025-11-21T09:14:02.511Z",
  "merchant": "Burger Point",
  "total": 18.5,
  "category": "Food",
  "date": "2025-11-21",
  "receiptUrl": "https://<bucket>.s3.amazonaws.com/receipts/9d1e....jpg",
  "rawText": "Burger Point 18.50 11/21/2025"
}

Access patterns

Need Operation Cost
All expenses for a user Query userId = :id One partition, cheap
Monthly summary for one user Query by user, then filter date in the app Reads all of the user's items
Monthly email for all users Scan Reads the whole table
Expenses in a date range, by category not supported without a GSI (userId + date) Would need a new index

Related storage

Store Layout
S3 expense-tracker-receipts-<account>-<region> Objects under receipts/<uuid>.<ext>. Uploaded directly by the browser through a presigned PUT.
SNS topic monthly-expense-summary Plain-text monthly summaries, delivered by email subscription.
Cognito user pool expense-tracker-users Email as username, auto-verified email, password rule: 8+ chars, upper, lower, number.

Tech Stack

Frontend

Backend

Containerization

AWS Services


Core Workflows

Authentication Flow

Login Page Signup Page Cognito Console


Receipt Upload & OCR Pipeline

  1. User selects receipt

  2. Backend generates presigned S3 URL

  3. Frontend uploads directly to S3

  4. Backend calls Textract AnalyzeExpense

  5. Extracted fields returned:

    • Merchant Name
    • Total Amount
    • Date
  6. User confirms data

  7. Expense saved to DynamoDB

Receipt Upload OCR Preview


Expense Dashboard

Dashboard


Monthly Email Notifications

S3 Bucket CloudFormation Console


Infrastructure as Code

All AWS resources are defined in a single declarative file:

infrastructure.yaml

Resources created:

Benefits


Containerization & Deployment

Two containers:

Service Port
Backend 3001
Frontend 3000

Managed via docker-compose.

Benefits:


Security

Authentication

Data Protection

Network Isolation


Scalability

Current Limitation

Future Improvements


Testing

Functional Testing

Security Testing


Project Structure

.
├── backend/
├── frontend/
├── infrastructure.yaml
├── docker-compose.yml
└── README.md

Setup & Deployment

1. Clone Repository

git clone <repository-url>
cd expense-tracker

2. Deploy Infrastructure

aws cloudformation deploy \
  --template-file infrastructure.yaml \
  --stack-name expense-tracker-stack

3. Run Application

docker-compose up --build

Design Decisions

Decision Why Trade-off
DynamoDB with userId partition key and expenseId sort key The main access pattern is "all expenses for one user". One Query on one partition, no joins needed, on-demand billing suits low traffic. Other queries (by date range or category across users) need a GSI or a scan.
Cognito for identity, no user table Managed signup, email verification, password reset and token issuing. The JWT sub becomes userId. Auth is tied to AWS; the backend still proxies passwords through AdminInitiateAuth instead of using SRP in the browser.
JWT verified against the Cognito JWKS (RS256) Stateless verification on every request, with no secret shared between services. Tokens can't be revoked before expiry.
Presigned S3 PUT for uploads The image goes browser to S3 directly, so the Node server never handles file bytes. Short expiry (300 s). The key must be passed back to /process; the server has to check who owns it.
Two-step OCR (/process, then /save-expense) Textract output is imperfect, so the user reviews and edits before anything is stored. Two round trips.
Textract AnalyzeExpense over generic OCR Returns structured fields (TOTAL, VENDOR_NAME, date) instead of raw text to parse. Pay per page; category is still guessed with keyword rules.
Single EC2 instance with Docker Compose Cheapest setup for a demo. Single point of failure (see Scalability).

Known Limitations

Found while documenting the API and schema. Listed here so they are tracked, not hidden.


Future Enhancements