Skip to content

Dhruv-Cmds/fastapi-banking-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

83 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🏦 FastAPI Banking System

A production-style Bank Account Management API built with FastAPI, SQLAlchemy, and MySQL.

Designed to simulate real-world fintech backend systems with secure authentication, transactional integrity, and rule-based financial operations.


πŸ–ΌοΈ UI Preview

Login Watch Full Demo:- https://youtu.be/iex0donzgtE


🌐 Live Demo


πŸš€ Key Features

πŸ” Authentication

  • JWT-based authentication
  • Secure password hashing (bcrypt)
  • Case-insensitive username handling
  • Protected routes using dependency injection

🏦 Account Management

  • Create multiple accounts per user
  • Unique account number enforcement
  • Accounts initialized with zero balance (system-controlled)
  • Fetch all user accounts securely

πŸ”„ Account Lifecycle (NEW)

Accounts are not physically deleted. Instead, they are marked as:

  • ACTIVE β†’ usable account
  • CLOSED β†’ hidden from UI and blocked from operations

This ensures:

  • Data integrity
  • Transaction history preservation
  • Real-world banking behavior

πŸ’Έ Transactions (Core System)

βœ” Supported Operations

  • Deposit funds
  • Withdraw funds with balance validation
  • Transfer money between accounts

βœ” Safety & Integrity

Prevent overdraft (no negative balance)

  • Prevent overdraft (no negative balance)
  • Prevent self-transfers
  • Only ACTIVE accounts can perform operations
  • Atomic database transactions using commit/rollback

🧠 Business Rules & Financial Constraints

πŸ’° Transaction Limits

  • Deposit limit per transaction
  • Withdraw limit per transaction
  • Transfer limit enforcement

Limits are centrally managed via a config module, making the system flexible and maintainable.


🧾 Transaction Ledger

  • Every operation (deposit, withdraw, transfer) is recorded

  • Enables audit tracking and system transparency

  • Foundation for future features like:

    • Statements
    • Analytics
    • Fraud detection

🧠 Validation Strategy

  • Strong input validation using Pydantic schemas

  • Backend-driven validation (never trusting frontend)

  • Ownership-based authorization

  • SClean separation of validation and business logic:

    • Data validation (schemas)
    • Business logic (routes/services)

🧱 Tech Stack

Layer Technology
Backend FastAPI
ORM SQLAlchemy
Database MySQL
Validation Pydantic
Auth JWT (python-jose)
Security Passlib (bcrypt)
Config python-dotenv

πŸ“ Project Structure

fastapi-banking-system/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routes/        # API endpoints    
β”‚       (auth, accounts, transactions)
β”‚   β”œβ”€β”€ models/        # Database models
β”‚   β”œβ”€β”€ schemas/       # Request/response validation
β”‚   β”œβ”€β”€ db/            # DB connection setup
β”‚   β”œβ”€β”€ core/          # Security, config (JWT, hashing, limits)
β”‚   └── dependencies/  # Auth middleware
β”‚
β”œβ”€β”€ main.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env
└── .gitignore

βš™οΈ Setup Guide

1️⃣ Clone Repository

git clone https://github.com/Dhruv-Cmds/fastapi-banking-system.git
cd fastapi-banking-system

2️⃣ Create Virtual Environment

python -m venv .venv

Activate:

Windows

.venv\Scripts\activate

Mac/Linux

source .venv/bin/activate

3️⃣ Install Dependencies

pip install -r requirements.txt

4️⃣ Configure Environment

Create a .env file:

DB_USER=root
DB_PASSWORD=your_password
DB_HOST=localhost
DB_NAME=bankaccountsystem

SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES = your_time

IF You Want to go live: 

MYSQL_PUBLIC_URL = your_url

SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES = your_time

5️⃣ Run Server

uvicorn main:app --reload (backend)
npm run dev (frontend)

πŸ“Œ API Endpoints

πŸ” Auth

  • POST /signup
  • POST /login

🏦 Accounts

  • POST /accounts
  • GET /accounts
  • DELETE /accounts/{id} β†’ closes account

πŸ’° Transactions

  • POST /accounts/{id}/deposit
  • POST /accounts/{id}/withdraw
  • POST /transfer

🧠 System Behavior

  • Tables auto-created at startup:
Base.metadata.create_all(bind=engine)
  • Account lifecycle:
Create account β†’ balance = 0  
Deposit β†’ increases balance  
Withdraw β†’ decreases balance  
Transfer β†’ moves funds safely 
Close account β†’ status = CLOSED (not deleted) 

πŸ” Security Highlights

  • JWT authentication
  • Password hashing (bcrypt)
  • Environment-based secrets
  • Ownership-based authorization checks
  • Safe transaction handling using DB locks
  • Protection against race conditions

πŸ’Ž Highlights

  • Clean, modular architecture
  • Real-world banking logic implementation
  • Soft-delete system (account lifecycle)
  • Transaction safety (race-condition prevention)
  • Rule-based financial system (limits + validation)
  • Scalable backend design

🏁 Conclusion

This project demonstrates how to build a real-world backend system with:

  • Authentication & authorization
  • Database design & ORM usage
  • Transaction safety & concurrency handling
  • Business rule enforcement
  • Clean architecture & scalability

It serves as a strong foundation for evolving into a full fintech platform πŸš€


⭐ Author

Dhruv Backend-focused developer building systems with strong fundamentals in API design, data integrity, and system thinking.

About

A FastAPI-based banking system API that supports account operations like deposits, withdrawals, transfer, and balance tracking, built with clean architecture principles using SQLAlchemy and Pydantic.

Topics

Resources

License

Stars

Watchers

Forks

Packages