Python Banking API for Beginners: Flask + SQLite, Deposits, Withdrawals and History
Build a banking REST API with Flask and SQLite: account, deposit, withdrawal and history endpoints, money stored as integer cents. Free source code.
In this project you build a complete financial REST API with Python, Flask and SQLite. A client request enters Flask, updates SQLite, and a JSON response comes back. I test it with Postman, but any web app, mobile app or curl command can call it.
The full source code is free on GitHub: https://github.com/rainyreaksmey/python-banking-api
▶ WATCH ON YOUTUBEWatch the video
▶ WATCH ON YOUTUBEFull video
This demo uses one account and no login. It is for learning. Real financial systems need strong security.
What the API does
| Method | Endpoint | What it does |
|---|---|---|
| GET | / |
Confirms the API is running and lists the endpoints |
| GET | /api/account |
Returns owner, currency and balance |
| POST | /api/deposit |
Adds money and records a transaction |
| POST | /api/withdraw |
Removes money if the balance allows it |
| GET | /api/transactions |
Returns the history, newest first |
The exact route names are in the repo; check them with flask routes.
Set up the environment
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install flaskThe virtual environment keeps this project's packages apart from the rest of your system. pip also installs Flask's own dependencies: Werkzeug, Jinja, Click and ItsDangerous. The requirements file pins Flask 3.1.3 so everyone installs the same version.
The database
Everything lives in one file, app.py, with a SQLite database beside it.
get_db()opens one SQLite connection per request and keeps it on Flask'sgobject, so the same request reuses it.- A row factory lets you read columns by name.
- Foreign-key checking is switched on.
- A teardown function closes the connection after every request.
Two tables are created on first start with executescript:
| Table | Columns |
|---|---|
accounts |
account ID (primary key), owner name, currency, balance in cents, with a check that blocks negative values |
transactions |
auto ID, account ID (foreign key), type, amount, balance after, creation time |
INSERT OR IGNORE creates the demo account ACC-1001 only once, even if the server restarts many times. It starts with 100,000 cents, which is $1,000.
Handle money safely
This is the most important idea in the project.
- Never store money as a float. The balance is an integer number of cents.
- Incoming amounts are read with
Decimaland rounded to two places withROUND_HALF_UP, the rounding people expect from a bank. - Zero, negative, infinite and invalid values are rejected.
InvalidOperation,TypeErrorandValueErrorare caught, so bad input returns a clean error and not a crash.
A second helper turns cents back into a two-decimal string for the JSON response.
The routes
Account. Selects the demo account with a parameterized query. The ? placeholder keeps values apart from the SQL text, so the query is never built by joining strings.
Deposit. Reads the JSON body, converts the amount to cents (invalid input returns 400), loads the balance, adds the amount, saves it with UPDATE, records the transaction and returns 201.
Withdrawal. Same shape, but it compares the amount with the balance first. If the withdrawal is larger, the API returns an insufficient-balance error, so the account can never go negative.
Transactions. Selects the rows for ACC-1001 with ORDER BY id DESC and returns a success flag, the count and the list.
Both money routes share one save_transaction helper. It inserts the row, commits, then reads the new row back with cursor.lastrowid.
Run and test
flask routes
python3 app.pyflask routes prints every registered endpoint with its method. The server listens on http://127.0.0.1:5000. If a route is missing, save app.py and restart.
In Postman:
- GET the root URL: message and endpoint list.
- GET the account: owner, currency, balance.
- POST a JSON amount to deposit: new balance and transaction.
- POST an amount to withdraw: balance goes down.
- GET the transactions: newest first.
Also send a bad amount and a withdrawal that is too large, to see the validation errors.
Before production
Add authentication, authorization, secure configuration, tests, logging and monitoring. The next lesson adds the first of those: Flask JWT authentication.
Links
- Full video: https://youtu.be/y01R1Aq6UKw
- Source code: https://github.com/rainyreaksmey/python-banking-api
- The same idea in FastAPI: FastAPI JWT authentication
Try changing one feature. It is the fastest way to check that you understand the whole flow.


