Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Personal Budget & Expense Management API

A FastAPI + PostgreSQL REST API for tracking income/expense transactions and retrieving budget summaries. Built to satisfy the project brief: FastAPI + SQLAlchemy, persistent PostgreSQL storage, Pydantic validation, and graceful error handling (no crashes on bad input).

Project structure

budget_api/
├── main.py             # FastAPI app + all endpoints
├── models.py           # SQLAlchemy Transaction model (+ TransactionType enum)
├── schemas.py          # Pydantic request/response schemas
├── database.py         # Engine, session factory, get_db dependency
├── config.py           # Loads DB credentials from .env
├── init.sql            # Reference SQL schema, auto-run by Postgres container on first start
├── Dockerfile          # Builds the API image
├── docker-compose.yml  # Runs API + PostgreSQL together
├── .env                # Local DB credentials (safe defaults for coursework/dev)
├── .env.example        # Template if you need to reset .env
├── .dockerignore
└── requirements.txt

Run with Docker (recommended — no local PostgreSQL install needed)

This spins up both the API and a PostgreSQL database together, fully networked.

docker compose up --build

That's it — Docker Compose will:

  1. Build the API image from the Dockerfile.
  2. Start a postgres:16-alpine container (db) with a named volume so data survives container restarts.
  3. Wait until PostgreSQL reports healthy (via pg_isready) before starting the API container (api), so the app never tries to connect too early.
  4. On first start (empty volume only), run init.sql automatically to create the transactions table, enum type, and indexes directly in Postgres — this is a reference/explicit copy of the schema. The API also creates the table itself via SQLAlchemy on startup, so it works correctly with or without init.sql running.
  5. Expose the API on http://localhost:8000 and PostgreSQL on localhost:5432.

Then open http://localhost:8000/docs for Swagger UI, exactly as in local dev.

To stop everything:

docker compose down

To stop and also wipe the database volume (fresh start):

docker compose down -v

Note on credentials: the default docker-compose.yml uses budget_user / budget_pass / budget_db directly in the compose file (not .env) so docker compose up works out of the box. For anything beyond local development, override these with a .env file passed via env_file: in the compose file, or via docker compose --env-file, and never commit real credentials.

Manual setup (without Docker)

  1. Install dependencies

    pip install -r requirements.txt
  2. Set up PostgreSQL Create a database and user (adjust names/passwords as you like):

    CREATE DATABASE budget_db;
    CREATE USER budget_user WITH PASSWORD 'budget_pass';
    GRANT ALL PRIVILEGES ON DATABASE budget_db TO budget_user;
  3. Configure environment variables Copy the example file and edit it with your real credentials:

    cp .env.example .env
  4. Run the server

    uvicorn main:app --reload

    On startup, the app connects to PostgreSQL and automatically creates the transactions table if it doesn't exist yet.

  5. Open the interactive docs Visit http://127.0.0.1:8000/docs to test every endpoint via Swagger UI.

Endpoints

Method Path Description
POST /transactions/ Add a new income or expense transaction
GET /transactions/ List transactions (optional type, start_date, end_date filters)
GET /budget/summary Get total income, total expense, and balance

Example requests

Create a transaction

curl -X POST http://127.0.0.1:8000/transactions/ \
  -H "Content-Type: application/json" \
  -d '{"amount": 1500.00, "type": "income", "category": "salary"}'

Filter transactions

curl "http://127.0.0.1:8000/transactions/?type=expense&start_date=2026-01-01&end_date=2026-01-31"

Get budget summary

curl http://127.0.0.1:8000/budget/summary

Validation & error handling

  • amount must be a positive number — invalid values return HTTP 422 with a Pydantic validation message.
  • type must be exactly "income" or "expense" — anything else returns HTTP 422.
  • An invalid date range (start_date after end_date) returns HTTP 400.
  • All database errors are caught and surfaced as proper HTTPException responses rather than crashing the server.

Notes on design choices

  • type is modeled as a native PostgreSQL enum (TransactionType) rather than a free-text string, so invalid values are rejected at both the Pydantic and database layers.
  • id uses PostgreSQL's UUID type instead of an auto-incrementing integer, which avoids exposing sequential record counts and works well if you later shard or merge data from multiple sources.
  • amount uses Numeric(12, 2) (fixed-point) rather than Float, since floating point is not safe for currency arithmetic.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages