A RESTful API for tracking and managing personal expenses.
- 🔒 User Authentication: Secure authentication using
JWTandbcrypt - 💸 Expense Management: Create, update, or delete expenses
- 🔍 Data Retrieval: Fetch specific records by ID or list all transactions
- ⚡ Efficient Querying: Built with
SQLAlchemyORM for clean data access - ✅ Type Safety & Validation: Static type checking with
Pyrightand runtime data validation usingPydantic - 🗄️ Database & Migrations:
PostgreSQLsupport withAlembicfor schema versioning. - 🧪 Testing: Included pre-configured HTTP requests
- 📖 Interactive API Docs: Auto-generated documentation via
Swagger UI
- Language: Python
- Web Framework: FastAPI
- Database: PostgreSQL
- ORM: SQLAlchemy 2.0
- Migrations: Alembic
- Validation: Pydantic v2
- Type Checking: Pyright
All protected endpoints require a JWT token.
Authorization: Bearer <JWT_TOKEN>
| Method | Endpoint | Description |
|---|---|---|
| POST | /signup | Create a new user |
| POST | /login | Authenticate user |
| POST | /expenses | Create an expense |
| GET | /expenses | List all expenses |
| GET | /expenses/{id} | Fetch expense by ID |
| PATCH | /expenses/{id} | Update expense |
| DELETE | /expenses/{id} | Delete expense |
curl -X POST http://localhost:8000/expenses \
-H "Authorization: Bearer <JWT_TOKEN>" \
-H "Content-Type: application/json" \
-d '{ ... }'Interactive OpenAPI documentation is available at: http://localhost:8000/docs
- Python 3.10+ with
pip - PostgreSQL 14+ running locally
openssl(for generating JWT secrets)
1. Create the database and user
Connect to PostgreSQL and run:
CREATE USER expense_user WITH PASSWORD 'expense';
CREATE DATABASE expense_db OWNER expense_user;2. Generate a JWT secret
Generate a secure JWT secret key:
openssl rand -base64 32Copy the generated value — it will be used in the next step.
3. Configure environment variables
Copy the example environment file and update its values:
cp .env.example .envEdit .env with the following configuration:
DB_HOST=localhost
DB_PORT=5432
DB_USER=expense_user
DB_PASSWORD=expense
DB_NAME=expense_db
JWT_ALGORITHM=HS256
JWT_EXPIRATION_TIME=3600
JWT_SECRET_TOKEN=<your-generated-secret>4. Create a virtual environment and install dependencies
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt5. Run database migrations
Apply the database schema using Alembic:
alembic upgrade headWarning
If this step fails, ensure PostgreSQL is running and the database credentials in .env are correct.
6. Run the application
Start the FastAPI server:
fastapi runThe API should now be running locally.
7. Access the API and OpenAPI documentation
Once the server is running, you can interact with the API using either:
- Preconfigured HTTP requests (in test_main.http), or
- FastAPI’s built-in OpenAPI documentation
Open your browser and visit Swagger UI on:
http://localhost:8000/docs