A robust and feature-rich RESTful API for e-commerce product management built with FastAPI, featuring advanced data validation, filtering capabilities, and an interactive Streamlit admin dashboard.
- CRUD Operations: Complete product management (Create, Read, Update, Delete)
- Advanced Filtering: Filter products by name, category, price, with sorting and pagination
- Data Validation: Robust Pydantic models with custom validators
- Business Logic Validation:
- SKU format validation
- Stock and availability rules
- Seller email domain restrictions
- Rating and discount validation
- Computed Fields: Automatic calculation of final price and product volume
- HTTP Middleware: Request/response lifecycle logging
- Dependency Injection: Clean architecture with dependency management
- Environment Variables: Configuration via
.envfile
- Interactive UI: Beautiful admin dashboard for product management
- Product Catalog: Browse, search, and filter products
- Add Products: User-friendly form to add new products
- Update/Delete: Manage existing products with ease
- Real-time API Integration: Direct communication with FastAPI backend
- UUID-based product identification
- SKU validation with custom format requirements
- Multi-dimensional product attributes (dimensions, ratings, tags)
- Seller information with email domain validation
- Image URL support
- Timestamp tracking (created_at)
- Python 3.8 or higher
- pip (Python package manager)
-
Clone the repository
git clone <your-repository-url> cd fastapi_ecomerce
-
Create a virtual environment
python -m venv venv
-
Activate the virtual environment
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies
pip install -r requirements.txt pip install streamlit # For the frontend dashboard -
Set up environment variables
Create a
.envfile in theapp/directory:base_url=./app/data/products.json
-
Create the data directory
mkdir -p app/data
-
Initialize the products database
Create an empty
products.jsonfile:echo [] > app/data/products.json
uvicorn app.mainn:app --reloadThe API will be available at: http://127.0.0.1:8000
API Documentation (auto-generated by FastAPI):
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
In a new terminal (with the backend running):
streamlit run streamlit_app.pyThe dashboard will open in your browser at: http://localhost:8501
GET /Returns a welcome message with configuration details.
GET /products?name={name}&category={category}&price={max_price}&order={asc|desc}&limit={limit}Query Parameters:
name(optional): Filter by product name (case-insensitive)category(optional): Filter by category (laptops, mobiles, accessories, electronics)price(optional): Maximum price filterorder(optional): Sort order - "asc" or "desc" (default: "asc")limit(optional): Number of items to return (1-100, default: 10)
Example:
curl "http://127.0.0.1:8000/products?category=laptops&price=50000&order=desc&limit=5"GET /products/{product_id}Example:
curl "http://127.0.0.1:8000/products/123e4567-e89b-12d3-a456-426614174000"POST /products
Content-Type: application/jsonRequest Body Example:
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"sku": "REAL-135GB-001",
"name": "Realme 9 Pro 5G",
"description": "6GB RAM, 128GB Storage, Midnight Black",
"category": "mobiles",
"brand": "Realme",
"price": 17999.0,
"currency": "INR",
"discount_percent": 15,
"stock": 50,
"is_active": true,
"rating": 4.5,
"tags": ["5G", "Android", "Smartphone"],
"image_urls": ["https://example.com/image1.jpg"],
"dimensions_cm": {
"length": 16.4,
"width": 7.59,
"height": 0.82
},
"seller": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Realme Official Store",
"email": "support@mistore.in",
"website": "https://www.realme.com"
},
"created_at": "2026-01-16T12:00:00Z"
}PATCH /product/{product_id}
Content-Type: application/jsonRequest Body Example (partial update):
{
"price": 16999.0,
"stock": 45,
"discount_percent": 20
}DELETE /products/{product_id}fastapi_ecomerce/
βββ app/
β βββ __init__.py
β βββ mainn.py # Main FastAPI application & routes
β βββ .env # Environment variables
β βββ data/
β β βββ products.json # JSON database
β βββ schema/
β β βββ product.py # Pydantic models for validation
β βββ service/
β βββ products.py # Business logic & data operations
βββ scripts/
βββ streamlit_app.py # Streamlit admin dashboard
βββ requirements.txt # Python dependencies
βββ LICENSE # MIT License
βββ .gitignore
βββ README.md # This file
- Product: Main product model with comprehensive validation
- DimensionCm: Product dimensions validation
- Seller: Seller information with email domain validation
- productUpdate: Model for partial updates
- Custom validators for SKU format, business rules, and seller emails
get_all_products(): Retrieve all productsadd_product(): Create a new product with SKU uniqueness checkremove_product(): Delete a product by IDchange_product(): Update product with partial data
- FastAPI application setup
- HTTP middleware for request logging
- Dependency injection examples
- Route handlers for all CRUD operations
- Query parameter validation and filtering logic
- Home page with overview
- Product catalog with search and filters
- Add new product form
- Update and delete operations
- Must contain a hyphen (
-) - Must end with exactly 3 digits (e.g.,
-001) - Example:
REAL-135GB-001
- If
stockis 0,is_activemust befalse - Products with
discount_percent > 0must haverating > 0
Only these domains are allowed:
- lenovostore.in
- samsungindia.in
- mistore.in
- asusexclusive.in
- hpworld.in
- oneplusstore.in
- Name: 6-80 characters
- Description: Max 200 characters
- Price: Must be >= 0
- Discount: 0-90%
- Rating: 0-5 stars
- Stock: Must be >= 0
- Tags: Maximum 10 tags
Get all products:
curl http://127.0.0.1:8000/productsFilter by category:
curl "http://127.0.0.1:8000/products?category=mobiles&limit=5"Get product by ID:
curl http://127.0.0.1:8000/products/{product_id}- Start the FastAPI server
- Open
http://127.0.0.1:8000/docsin your browser - Try out the endpoints interactively
Create an app/.env file with:
base_url=./app/data/products.json- fastapi: Modern web framework for building APIs
- uvicorn: ASGI server for running FastAPI
- python-dotenv: Load environment variables from
.env - pydantic: Data validation using Python type annotations
- streamlit: Frontend dashboard framework
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Krushna Sonawane
- FastAPI documentation and community
- Pydantic for excellent data validation
- Streamlit for the beautiful dashboard framework
- Database integration (PostgreSQL/MongoDB)
- User authentication and authorization
- Image upload functionality
- Order management system
- Payment gateway integration
- Email notifications
- Advanced analytics dashboard
- API rate limiting
- Caching layer (Redis)
- Unit and integration tests
For issues, questions, or suggestions, please open an issue in the repository.
Happy Coding! π