A complete, production-ready ecommerce API built with FastAPI, SQLAlchemy, and Pydantic.
- User Authentication - JWT-based secure authentication
- Product Management - Full CRUD operations for products
- Category System - Organize products into categories
- Order Processing - Complete order management with stock control
- API Documentation - Auto-generated Swagger UI & ReDoc
- Data Validation - Automatic request/response validation
- Database - SQLite with SQLAlchemy ORM
- Error Handling - Comprehensive error responses
- Code Organization - Clean, modular structure
pip install -r requirements.txtcd FastAPI/app
cp .env.example .envUpdate .env with your settings if needed.
python init_db.pyThis creates the database tables and populates with sample data.
uvicorn main_new:app --reloadOutput:
INFO: Uvicorn running on http://127.0.0.1:8000
- API Documentation: http://localhost:8000/api/docs
- Alternative Docs: http://localhost:8000/api/redoc
- Health Check: http://localhost:8000/health
File: API_DOCUMENTATION.md
Complete API endpoint reference with:
- All available endpoints
- Request/response examples
- Query parameters
- Error responses
File: FASTAPI_LEARNING_GUIDE.md
Comprehensive FastAPI tutorial covering:
- Basic concepts
- Request/response handling
- Data validation
- Authentication
- Database integration
- Best practices
After running init_db.py, use these credentials:
Email: demo@example.com
Username: demouser
Password: password123
Or:
Email: john@example.com
Username: john_doe
Password: securepass456
FastAPI/
βββ app/
β βββ main_new.py β START HERE (refactored app)
β βββ main.py (original, kept for reference)
β βββ database.py (database configuration)
β βββ models.py (SQLAlchemy ORM models)
β βββ schemas.py (Pydantic validation schemas)
β βββ crud.py (database operations)
β βββ auth.py (authentication utilities)
β βββ init_db.py (database initialization)
β βββ routes/
β β βββ __init__.py
β β βββ products.py (product endpoints)
β β βββ categories.py (category endpoints)
β β βββ orders.py (order endpoints)
β βββ service/ (original service layer)
β βββ data/ (original data files)
β βββ ecommerce.db (SQLite database - auto-created)
β βββ API_DOCUMENTATION.md β READ THIS
β βββ FASTAPI_LEARNING_GUIDE.md β LEARN FROM THIS
β βββ .env.example (environment template)
βββ requirements.txt
Python classes that represent database tables. Defined in models.py.
Pydantic classes for data validation and API documentation. Defined in schemas.py.
Create, Read, Update, Delete functions for database operations. Defined in crud.py.
API endpoints organized by feature (products, categories, orders). Found in routes/ directory.
JWT-based authentication for secure API access. Utilities in auth.py.
POST /api/v1/auth/register - Create new user
POST /api/v1/auth/login - Login and get token
GET /api/v1/products - Get all products (with filtering/sorting)
GET /api/v1/products/{id} - Get specific product
POST /api/v1/products - Create product
PUT /api/v1/products/{id} - Update product
DELETE /api/v1/products/{id} - Delete product
GET /api/v1/categories - Get all categories
GET /api/v1/categories/{id} - Get specific category
POST /api/v1/categories - Create category
PUT /api/v1/categories/{id} - Update category
DELETE /api/v1/categories/{id} - Delete category
POST /api/v1/orders - Create order
GET /api/v1/orders/{id} - Get order details
GET /api/v1/orders/user/{id} - Get user's orders
PUT /api/v1/orders/{id}/status - Update order status
POST /api/v1/orders/{id}/cancel - Cancel order
- Open http://localhost:8000/api/docs
- Click endpoint to expand it
- Click "Try it out"
- Fill in parameters
- Click "Execute"
- See response
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "newuser@example.com",
"username": "newuser",
"password": "password123"
}'curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "demo@example.com",
"password": "password123"
}'Response includes token:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer",
"user": {...}
}curl http://localhost:8000/api/v1/products
curl http://localhost:8000/api/v1/products?sort_by_price=true&order=desc
curl "http://localhost:8000/api/v1/products?name=laptop"- Read API_DOCUMENTATION.md - Understand endpoints
- Read FASTAPI_LEARNING_GUIDE.md - Learn FastAPI concepts
- Explore main_new.py - See how it's organized
- Test with Swagger UI - Try endpoints
- Modify the code - Add your own features
cd FastAPI/app
uvicorn main_new:app --reloaduvicorn main_new:app --port 8001rm ecommerce.db # Delete old database
python init_db.py # Create newpython --versionpip install package_name
pip freeze > requirements.txt # Update requirementsSolution: Install dependencies
pip install -r requirements.txtSolution: Use a different port
uvicorn main_new:app --port 8001Solution: Reinitialize database
rm ecommerce.db
python init_db.pySolution: Ensure you're in the correct directory
cd FastAPI/app
python -c "import main_new" # Should not errorBy exploring this project, you'll understand:
- REST API Design - How to build scalable APIs
- FastAPI Framework - Modern Python web framework
- Database Design - Relationships and CRUD operations
- Authentication - JWT tokens and password hashing
- Code Organization - Professional project structure
- Error Handling - Proper HTTP status codes
- API Documentation - Auto-generation and examples
-
Extend Features
- Add reviews/ratings
- Implement cart functionality
- Add payment processing
-
Improve Database
- Add PostgreSQL support
- Implement migrations
- Add indexing
-
Deployment
- Deploy to AWS/Heroku/DigitalOcean
- Setup CI/CD pipeline
- Add monitoring
-
Testing
- Write unit tests
- Add integration tests
- Setup pytest
- API_DOCUMENTATION.md - Complete API reference
- FASTAPI_LEARNING_GUIDE.md - FastAPI concepts
- main_new.py - Application entry point
- models.py - Database structure
- routes/products.py - Example of endpoint organization
- Use Swagger UI - It's your best friend for testing
- Read comments - Every file has detailed comments
- Start small - Understand one endpoint before exploring others
- Experiment - Modify code and see what happens
- Use tools - Postman, curl, or Thunder Client for API testing
This project is educational. Use freely!
If you have questions:
- Check the learning guide - FASTAPI_LEARNING_GUIDE.md
- Review API documentation - API_DOCUMENTATION.md
- Look at code comments
- Try concepts in Swagger UI
Happy learning! Build amazing APIs with FastAPI! π