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).
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
This spins up both the API and a PostgreSQL database together, fully networked.
docker compose up --buildThat's it — Docker Compose will:
- Build the API image from the
Dockerfile. - Start a
postgres:16-alpinecontainer (db) with a named volume so data survives container restarts. - Wait until PostgreSQL reports healthy (via
pg_isready) before starting the API container (api), so the app never tries to connect too early. - On first start (empty volume only), run
init.sqlautomatically to create thetransactionstable, 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 withoutinit.sqlrunning. - Expose the API on
http://localhost:8000and PostgreSQL onlocalhost:5432.
Then open http://localhost:8000/docs for Swagger UI, exactly as in local dev.
To stop everything:
docker compose downTo stop and also wipe the database volume (fresh start):
docker compose down -vNote 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.
-
Install dependencies
pip install -r requirements.txt
-
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;
-
Configure environment variables Copy the example file and edit it with your real credentials:
cp .env.example .env
-
Run the server
uvicorn main:app --reload
On startup, the app connects to PostgreSQL and automatically creates the
transactionstable if it doesn't exist yet. -
Open the interactive docs Visit http://127.0.0.1:8000/docs to test every endpoint via Swagger UI.
| 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 |
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/summaryamountmust be a positive number — invalid values return HTTP 422 with a Pydantic validation message.typemust be exactly"income"or"expense"— anything else returns HTTP 422.- An invalid date range (
start_dateafterend_date) returns HTTP 400. - All database errors are caught and surfaced as proper
HTTPExceptionresponses rather than crashing the server.
typeis 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.iduses 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.amountusesNumeric(12, 2)(fixed-point) rather thanFloat, since floating point is not safe for currency arithmetic.