๐ฐ Expense Tracker API
A secure, RESTful Expense Tracker API built with Flask, SQLite, and JWT authentication.
This system allows users to securely manage personal expenses, including adding, viewing, updating, deleting, and filtering financial records.
๐ Project Description
The Expense Tracker API is a backend web service designed to help users manage their personal finances in a structured and secure way.
Each user can create an account, log in securely, and manage their own set of expenses. Every expense is linked directly to the user who created it, ensuring complete data privacy and separation between users.
This project was built as part of the roadmap.sh Python Projects roadmap and demonstrates essential backend development concepts including:
- REST API design principles
- User authentication using JWT (JSON Web Tokens)
- Secure password hashing using Argon2
- Relational database design using SQLite
- CRUD (Create, Read, Update, Delete) operations
- Data filtering and pagination
- Input validation and error handling
๐ง What This API Does (Simple Explanation)
Think of this API as a digital notebook where:
- You can write down your expenses (like shopping, bills, etc.)
- You can log in securely so only you can see your data
- You can search your expenses by date or category
- You can update or delete any entry at any time
Everything is stored safely in a database and linked to your account only.
โจ Features
๐ค User System
- Register a new user account
- Secure login system
- Passwords are encrypted (not stored in plain text)
- JWT token authentication for secure access
๐ธ Expense Management
- Add a new expense
- View all your expenses
- Update existing expenses
- Delete expenses
- Each expense belongs only to its owner
๐ Filtering & Search
You can filter expenses by:
- A specific date
- A date range
- Last week, month, or 3 months
- Category (e.g. Groceries, Health, etc.) - You can insert whatever category you would like
๐ Pagination
Expenses are displayed in pages:
- You can choose how many results per page
- You can move between pages
- Helps manage large sets of data efficiently
๐ Technology Stack
| Technology |
Purpose |
| Python |
Core programming language |
| Flask |
Web framework for API |
| SQLite |
Lightweight database |
| PyJWT |
Token-based authentication |
| Argon2 |
Secure password hashing |
| python-dotenv |
Environment variable management |
๐ Project Structure
The project is organised into service-based modules:
Expense-Tracker-API/
|
โโโ instance/
โ โโโ expense.db # Creates on first launch
โโโ services/
โ โโโ auth.py
โ โโโ config.py
โ โโโ db.py
โ โโโ expenses.py
โ โโโ users.py
โ โโโ validator.py
โโโ app.py
โโโ .env # Creates on first launch
โโโ LICENSE
โโโ README.md
๐ฆ File Breakdown (Important Functions Explained)
๐น app.py (Main Application File)
This is the entry point of the API.
It:
- Starts the Flask server
- Defines all API routes (endpoints)
- Handles incoming requests
- Returns responses to users
๐น services/auth.py
Handles all authentication logic:
-
login_required()
Protects routes so only logged-in users can access them.
-
create_token(user_id)
Generates a JWT token for authenticated users.
-
decode_token(token)
Extracts user information from a JWT token.
-
hash_password(password)
Encrypts user passwords securely.
-
check_password(stored, entered)
Checks if a password is correct during login.
๐น services/users.py
Handles user management:
-
user_register(data)
Creates a new user account in the database.
-
find_user(data)
Finds a user by email.
-
user_login(data)
Authenticates a user and returns a JWT token.
๐น services/expenses.py
Handles all expense-related logic:
-
add_expense(data)
Adds a new expense to the database.
-
all_expenses(...)
Retrieves all expenses for a user with filtering and pagination.
-
update_expense(...)
Updates an existing expense.
-
delete_expense(...)
Deletes an expense from the database.
๐น services/db.py
Handles all database operations:
execute() โ Runs SQL queries that modify data
fetch_one() โ Retrieves a single record
fetch_all() โ Retrieves multiple records
delete_one() โ Deletes a record
๐น services/config.py
Handles system configuration:
- Creates
.env file if missing with the secret key or token generated
- Connects to SQLite database
- Creates required tables (
users, expenses)
๐น services/validator.py
Handles input validation:
- Validates email format
- Validates date format (DD-MM-YYYY)
- Ensures correct data input before saving to database
๐ Getting Started (Installation Guide)
1. Download the Project
Clone the repository:
git clone https://github.com/sheikh-h/expense-tracker-api.git
cd expense-tracker-api
2. Create Virtual Environment
Windows
python -m venv venv
venv\Scripts\activate
Mac / Linux
python -m venv venv
source venv/bin/activate
3. Install Dependencies
pip install -r requirements.txt
4. Run the Application
python app.py
OR
flask run
The API will run at:
http://127.0.0.1:5000
OR
localhost:5000
๐ Environment Variables
The .env file is created itself anyway and gets the port from your local machine on application run:
SECRET_KEY=your_secret_key_here
๐ฌ API Usage Guide (Postman + Curl)
For the projects, I have been using an application called postman which is opensourced and available to use online and would recommend that you download it and install it from their website to use with this project of mine for viewability and ease of use.
๐ 1. Register a New User
Endpoint:
Body (JSON):
{
"name": "Sheikh",
"email": "sheikh@example.com",
"password": "password123"
}
Using cURL:
curl -X POST http://127.0.0.1:5000/register \
-H "Content-Type: application/json" \
-d '{"name":"Sheikh","email":"sheikh@example.com","password":"password123"}'
Response:
{
"success": "your_jwt_token"
}
๐ 2. Login
Endpoint:
Body:
{
"email": "sheikh@example.com",
"password": "password123"
}
cURL:
curl -X POST http://127.0.0.1:5000/login \
-H "Content-Type: application/json" \
-d '{"email":"example@example.com","password":"example123!"}'
Response:
{
"success": "your_jwt_token"
}
๐ธ 3. Add Expense
Endpoint:
Headers:
Authorization: Bearer <your_token>
Body:
{
"title": "Groceries",
"category": "Groceries",
"amount": 45.99,
"date": "14-06-2026"
}
cURL:
curl -X POST http://127.0.0.1:5000/expenses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <YOUR_TOKEN>" \
-d '{"title":"Groceries","category":"Groceries","amount":45.99,"date":"14-06-2026"}'
๐ 4. Get Expenses
Endpoint:
Optional Filters:
- page
- limit
- category
- on
- from
- to
Example:
GET /expenses?page=1&limit=5
cURL:
curl http://127.0.0.1:5000/expenses \
-H "Authorization: Bearer YOUR_TOKEN"
โ๏ธ 5. Update Expense
Endpoint:
Example Body:
cURL:
curl -X PUT http://127.0.0.1:5000/expenses/1 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"amount":60}'
โ 6. Delete Expense
Endpoint:
cURL:
curl -X DELETE http://127.0.0.1:5000/expenses/1 \
-H "Authorization: Bearer YOUR_TOKEN"
โ Error Responses
Missing Fields
{
"error": "Missing fields in request"
}
Invalid Token
{
"denied": "Invalid Token"
}
Not Found
{
"error": "Unable to find email"
}
๐ Security Features
- Passwords hashed using Argon2
- JWT-based authentication
- Token expiry (1 hour)
- Protected routes
- User-specific data isolation
๐ฎ Future Improvements
- Add refresh tokens
- Improve validation layer structure
- Add logging system
- Add Swagger API documentation
- Docker support
- Move to PostgreSQL for scalability
๐ฏ Roadmap.sh Project
This project is part of the following roadmap.sh task:
๐ https://roadmap.sh/projects/expense-tracker-api
๐ Licence
This project is licensed under the MIT Licence โ see the LICENCE file for details.
MIT Licence
Copyright (c) 2026 Sheikh Hussain
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Footnote
๐ฃ๏ธ Feel free to follow, connect, and chat!
โญ If you like this project, please give it a star on GitHub!