The Expense Tracking and Budgeting API helps individuals manage their finances by tracking expenses, setting budgets, and generating real-time alerts. The API supports user authentication, secure data management, and easy integration with third-party services.
Managing personal finances can be overwhelming. This API simplifies the process by enabling users to:
- Track expenses: Categorize and store expenses with various attributes.
- Set budgets: Create, update, and monitor budgets, ensuring users stay within their spending limits.
- Automate alerts: Get notified when spending exceeds or approaches predefined limits.
- Visualize data: Gain insights into spending habits with analytics and visual breakdowns.
- User Management: Register, log in, and authenticate users with JWT-based authentication.
- Expense Tracking: Add, update, and categorize expenses.
- Budget Management: Set and adjust budgets, track expenses against limits, and receive alerts.
- Data Analytics: Summarize expenses, view detailed breakdowns by category, and export data.
- Real-time Notifications: WebSocket support for live notifications of budget alerts.
- Secure Access: JWT-based authentication and password hashing for secure access to the system.
- Group Expense Tracking: Track expenses for groups of users, with automatic splitting of expenses.
- Debt Notifications: Get notified when debts are owed within a group, with real-time notifications for updates and payment status.
- Backend Framework: FastAPI
- Database: PostgreSQL with SQLAlchemy ORM (SQLite also available as an alternative)
- Authentication: JWT, OAuth2
- Notifications: WebSocket-based live notifications
- Data Visualization: Integration with front-end for trends and expense tracking
- Python 3.12+ (if you're not using Docker)
- Docker (if you're using Docker to run the app)
Clone the repository to your local machine:
git clone https://github.com/Incognitol07/expense-tracker-api.git
cd expense-tracker-api
To isolate your project dependencies, set up a Python virtual environment:
-
Create the virtual environment:
python -m venv venv
-
Activate the virtual environment:
-
Command Prompt (Windows):
.\venv\Scripts\activate
-
PowerShell (Windows):
.\venv\Scripts\Activate.ps1
-
Bash (Linux/Mac):
source venv/bin/activate
-
-
Install dependencies:
pip install -r requirements.txt
-
Deactivate the virtual environment (when done):
deactivate
Create a .env
file by copying the provided example file:
- Mac/Linux:
cp .env.example .env
- Windows (Command Prompt):
copy .env.example .env
- Windows (PowerShell):
Copy-Item .env.example .env
Edit the .env
file and update the variables with your configuration:
ENVIRONMENT=development
DATABASE_URL=postgresql://<username>:<password>@<host>:<port>/<dbname>
JWT_SECRET_KEY=myjwtsecretkey
MASTER_KEY=master_key
By default, the project uses PostgreSQL for the database. However, you can easily switch to SQLite by making a simple change in the configuration file.
- Open the
app/config.py
file. - By default, the PostgreSQL database URL is set to be used:
DATABASE_URL: str = os.getenv("DATABASE_URL")
- If you prefer to use SQLite instead, uncomment the following line and comment out the PostgreSQL line:
# DATABASE_URL: str = os.getenv("DATABASE_URL") # Use PostgreSQL by default DATABASE_URL: str = 'sqlite:///expense.db' # Use SQLite instead
This allows you to quickly switch between PostgreSQL and SQLite based on your preference or environment.
-
Create and start the containers:
docker-compose up --build
This builds and starts the application and database services.
-
Access the application:
Once running, visithttp://127.0.0.1:8000
in your browser. -
Stop the containers:
When done, stop the services with:docker-compose down
- Activate the virtual environment (see instructions above).
- Run the application:
The app will be available at
uvicorn app.main:app --reload
http://127.0.0.1:8000
.
- WebSocket Endpoint:
http://127.0.0.1:8000/ws/notifications/{user_id}
- Connect to this WebSocket endpoint for live notifications about budget alerts and spending updates.
This feature allows users to stay updated with budget notifications in real-time, improving their ability to manage finances instantly.
- Group Creation: Users can create groups and invite others to join by email. Upon acceptance, group members can share and track expenses.
- Expense Splitting: Expenses paid by one member can be split among all group members, with real-time updates sent through notifications.
- Debt Notifications: When expenses are split, users will receive notifications about how much they owe or are owed within the group. These notifications include the status of debt payments.
The API includes robust logging functionality to ensure transparency and ease of debugging. The logging system is configured to capture critical events, including errors, user actions, and alerts, to help maintain a secure and reliable system.
- RotatingFileHandler: Logs are stored in files with a rotation mechanism to prevent excessive file size.
- Error Logging: Captures and records all error events.
- Audit Logging: Tracks important user activities, such as updates to budgets, expenses, and group actions.
- Alert Logging: Logs actions related to alerts, such as creation, updates, and thresholds being exceeded.
Logging is configured in the app/utils/logging_config.py
file and integrates with routers across the application.
Here is an example of a log entry:
[2024-11-20 14:32:15,123] - INFO - Budget alert triggered for user_id=101: Budget 'Monthly Groceries' exceeded 90% threshold.
- System Health: Continuous logging helps monitor system health and identify issues early.
- Audit Trail: Maintains a record of user and system actions for accountability.
- Debugging: Simplifies tracing issues and troubleshooting errors during development and production.
For detailed instructions on configuring or extending the logging system, refer to the app/utils/logging_config.py
file in the project structure.
expense-tracker-api/
├── app/
│ ├── main.py # Application entry point
│ ├── websocket_manager.py # WebSocket connection management
│ ├── routers/ # API endpoint routers
│ ├── schemas/ # Pydantic models for request validation
│ ├── utils/ # Utility functions (e.g., security, notifications)
│ ├── models/ # SQLAlchemy models
│ ├── database.py # Database connection and session handling
│ └── config.py # Configuration settings
├── requirements.txt # Versions of installed packages
├── docker-compose.yml # Docker Compose configuration for the app and database
├── Dockerfile # Dockerfile for building the web service image
├── .env # Environment variables
└── README.md # Project documentation
You can test the API using curl, Postman, Bruno, or FastAPI's interactive docs available at http://127.0.0.1:8000/docs
or http://127.0.0.1:8000/redoc
for a more comprehensive documentation.
To register a new user:
curl -X POST "http://127.0.0.1:8000/auth/register" -H "accept: application/json" -H "Content-Type: application/json" -d '{"username": "testuser", "email": "[email protected]", "password": "password123"}'
To test real-time notifications via WebSocket:
- Connect to
http://127.0.0.1:8000/ws/notifications/{user_id}
. - Upon spending updates or threshold alerts, the connected WebSocket will receive messages in real time.
The Expense Tracking and Budgeting API offers a robust system for managing personal finances, automating budget management, and visualizing financial data. With features like real-time alerts, secure authentication, group expense tracking, debt notifications, and comprehensive data tracking, this API helps users make informed decisions about their finances.
This project is licensed under the MIT License.