All projects

N9/05FintechComplexity: Light

Finance Tracking API

A FastAPI backend for recording income and expenses, with roles that decide who can read, add or summarise.

Status
Prototype
Year
2026
Built for
Backend engineering
Languages
Python
Team
Sujal Negi
01

Overview

A compact REST API for managing financial records. It stores transactions in SQLite, validates every input, and gives three roles (viewer, analyst and admin) different powers.

02

Problem and aim

Problem

Financial data needs rules about who may change it and who may only look. Without them, anyone with access can add, delete or misread the books.

Aim

A small, testable backend that shows the core ideas of a finance service: validation, storage, summaries and role-based access.

03

How it works

01 / 05

Request

A client calls an endpoint and sends its role in the X-User-Role header: viewer, analyst or admin.

02 / 05

Check the role

FastAPI dependencies check the role before the endpoint runs. An unknown role gets 400; a role without permission gets 403.

03 / 05

Validate

Pydantic checks the body: amount must be greater than zero, type must be income or expense, and the date must be a real date.

04 / 05

Store or query

SQLAlchemy writes to or reads from SQLite. Lists can be filtered by type and paged with skip and limit.

05 / 05

Summarise

GET /summary/ adds up income and expenses and returns the current balance (analysts and admins only).

04

Architecture

A classic three-layer API

AClient

  • Any HTTP clientcurl, a front end, or the automatic docs page.
  • X-User-Roleviewer, analyst or admin.

BFastAPI app main.py

  • Routes/transactions/, /summary/
  • Role checksDependencies run before each route.
  • ValidationPydantic schemas in schemas.py.

CData

  • SQLAlchemySessions and queries (database.py).
  • SQLiteA single database file.

Request flow

  1. A request arrives with a role header and, for writes, a JSON body.
  2. FastAPI runs the role dependency: unknown role → 400, wrong role → 403.
  3. Pydantic validates the body (amount above zero, known type, real date) → 422 if not.
  4. SQLAlchemy reads or writes SQLite; the response is shaped by the output schema.
05

Languages and code

  • Python100.0%

Repository

7 files190 linesTap any file to read its code

  • 4 lines
  • Database engine, session factory and the get_db dependency. 41 lines
  • The app: role dependencies and the four endpoints (create, list with filters, delete, summary). 60 lines
  • 12 lines
  • Python packages needed. 8 lines
  • Pydantic models for input, output and the summary. 27 lines
  • pytest tests for the endpoints. 38 lines
06

Methodology

  1. Define the data first: a Transaction model and request/response schemas.
  2. Express permissions as reusable dependencies, not copy-pasted checks.
  3. Return the right HTTP status for every outcome.
  4. Cover the endpoints with pytest.
07

Technology

APIFastAPIFast Python web framework with automatic docs.
PydanticValidates request and response data.
DataSQLAlchemyMaps Python classes to database tables.
SQLiteA file-based database, ideal for a prototype.
QualitypytestAutomated tests.
08

Demo

09

Concepts

01What is role-based access control (RBAC)?

Permissions are attached to roles, not people. Here a viewer can list transactions, an analyst can also see the summary, and only an admin can add or delete.

02What do the status codes mean?

201 created, 204 deleted with no body, 400 bad request, 403 forbidden (valid user, wrong permission), 404 not found and 422 the body failed validation.

03What is an ORM?

An object-relational mapper lets you work with database rows as Python objects instead of writing SQL by hand.

04Is a role header secure?

Not on its own. Anyone can send any header. In production the role comes from a verified login token; the header here keeps the prototype simple so the access rules are easy to see.

10

Team