A simple FastAPI-based financial transaction API built as part of a crash-course project to demonstrate API design, layered architecture, database persistence, validation, and testing.
This project provides a basic financial transaction system that supports:
- Creating accounts
- Preventing duplicate accounts
- Depositing funds
- Withdrawing funds
- Transferring funds between accounts
- Basic idempotency protection for transfers
- Persistent data storage using SQLite
- Automated testing with Pytest
The goal of this project is to showcase clean API structure, separation of concerns, and core financial transaction behaviors in a beginner-friendly but professional way.
- Python 3
- FastAPI
- Uvicorn
- SQLAlchemy
- SQLite
- Pydantic
- Pytest
financial_transaction_api/
│
├── app/
│ ├── main.py
│ ├── controller/
│ ├── service/
│ ├── repository/
│ ├── schema/
│ ├── model/
│ └── database/
│
├── tests/
├── financial_api.db
├── requirements.txt
├── README.md
└── .gitignore- Create new financial accounts
- Prevent duplicate account creation
- Deposit money into an account
- Withdraw money from an account
- Prevent withdrawals that exceed available balance
- Transfer money from one account to another
- Persist data in SQLite so records remain after restart
- Support idempotent transfer behavior to avoid accidental duplicate processing
The test suite covers core API behavior, including:
- Health check
- Create account
- Duplicate account handling
- Deposit
- Withdrawal
- Insufficient funds
- Transfer workflow
- Transfer idempotency
git clone https://github.com/YOUR-USERNAME/financial_transaction_api.git
cd financial_transaction_apipython3 -m venv venv
source venv/bin/activatepython -m venv venv
venv\Scripts\activatepip install -r requirements.txtuvicorn app.main:app --reloadThe API should now be available locally at:
http://127.0.0.1:8000Interactive Swagger docs:
http://127.0.0.1:8000/docsRun the test suite with:
pytest -vTypical usage flow:
- Create an account
- Deposit funds into the account
- Withdraw funds if balance allows
- Transfer funds between accounts
- Verify correct responses through the API or tests
This project follows a layered architecture to keep responsibilities separated:
- Controller layer handles incoming API requests and responses
- Service layer contains business logic
- Repository layer handles database access
- Schema layer defines request and response models
- Model layer defines database tables
This structure makes the project easier to maintain, test, and expand.
Some key concepts practiced in this project include:
- Building RESTful endpoints with FastAPI
- Structuring a Python API project into clear layers
- Using Pydantic for validation
- Persisting data with SQLite and SQLAlchemy
- Writing automated tests with Pytest
- Handling duplicate requests and transaction edge cases
Common files and folders to exclude:
venv/
__pycache__/
*.pyc
*.db
.env
.DS_Store
.pytest_cache/Possible next enhancements:
- Authentication and authorization
- Transaction history endpoint
- Account balance lookup endpoint
- Improved error handling and standardized responses
- PostgreSQL integration for production readiness
- Docker support
- CI/CD pipeline integration
Jeremy Cheeseborough
GitHub: https://github.com/jcheese54 LinkedIn: www.linkedin.com/in/jcheeseborough