This backend is designed for shopkeepers to manage daily business operations:
- stock/inventory information
- sales tracking
- profit and analytics
- shop-level reporting
Build a secure and simple API so a shop owner can:
- add and manage products
- track stock in and stock out
- record every sale
- see total sales, total cost, gross profit, and net profit
- monitor low-stock items
Current codebase includes:
- FastAPI app bootstrap in
main.py - Basic user Pydantic model in
models/user.py
Current endpoint:
GET /returns home response
Suggested structure for scaling this project:
.
├── main.py
├── models/
│ ├── user.py
│ ├── product.py
│ ├── sale.py
│ └── inventory.py
├── routers/
│ ├── auth.py
│ ├── products.py
│ ├── sales.py
│ ├── inventory.py
│ └── reports.py
├── services/
│ ├── profit_service.py
│ └── inventory_service.py
├── db/
│ ├── base.py
│ ├── session.py
│ └── models/
└── schemas/
├── user.py
├── product.py
├── sale.py
└── report.py
- id
- name
- email (unique)
- password_hash
- is_active
- created_at
- id
- name
- sku (unique)
- category
- unit
- purchase_price
- selling_price
- current_stock
- reorder_level
- is_active
- id
- user_id
- customer_name (optional)
- total_amount
- total_cost
- gross_profit
- payment_method
- created_at
- id
- sale_id
- product_id
- quantity
- unit_selling_price
- unit_cost_price
- line_total
- line_profit
- id
- product_id
- movement_type (
IN,OUT,ADJUSTMENT) - quantity
- reason
- reference_id (sale/purchase/adjustment)
- created_at
- id
- title
- amount
- expense_type (rent, salary, utility, etc.)
- created_at
line_total = quantity * unit_selling_priceline_profit = (unit_selling_price - unit_cost_price) * quantitytotal_sales = sum(line_total)total_cost = sum(unit_cost_price * quantity)gross_profit = total_sales - total_costnet_profit = gross_profit - total_expenses
POST /auth/registerPOST /auth/loginGET /auth/me
POST /productsGET /productsGET /products/{product_id}PUT /products/{product_id}DELETE /products/{product_id}
POST /inventory/in(stock purchase/addition)POST /inventory/out(manual stock out)POST /inventory/adjust(correction)GET /inventory/low-stockGET /inventory/movements
POST /sales(create sale with sale items)GET /salesGET /sales/{sale_id}GET /sales/summary/dailyGET /sales/summary/monthly
GET /reports/dashboardGET /reports/profit?from=YYYY-MM-DD&to=YYYY-MM-DDGET /reports/top-productsGET /reports/slow-products
- today_sales
- today_profit
- monthly_sales
- monthly_profit
- total_products
- out_of_stock_count
- low_stock_count
- top_selling_products
- FastAPI for API layer
- Pydantic for request/response validation
- SQLAlchemy + Alembic for DB and migrations
- PostgreSQL (recommended) or SQLite (for local)
- JWT authentication
- Uvicorn for local server
cd C:/Users/Lenovo/Desktop/FastApi.\env\Scripts\Activate.ps1python -m pip install -r requirements.txtpython -m uvicorn main:app --reload- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
- Add database configuration and models.
- Implement authentication (register/login/JWT).
- Implement products CRUD.
- Implement inventory movement APIs.
- Implement sales transaction API with stock deduction.
- Implement reports and profit analytics.
- Add validation, logging, and tests.
- Keep business logic in service layer, not directly in route functions.
- Never store raw passwords; always store password hashes.
- Wrap sale creation in a DB transaction to avoid stock mismatch.
- Validate stock before confirming a sale.