A beginner-friendly e-commerce API built with FastAPI, featuring user management, product catalog, shopping cart, and order processing.
ecommerce-api/
β
βββ app/
β βββ __init__.py
β βββ database.py # Database configuration
β βββ models.py # SQLAlchemy models
β βββ schemas.py # Pydantic schemas
β βββ routers/
β βββ __init__.py
β βββ users.py # User endpoints
β βββ products.py # Product endpoints
β βββ cart.py # Cart endpoints
β βββ orders.py # Order endpoints
β
βββ main.py # Application entry point
βββ requirements.txt # Python dependencies
βββ README.md # This file
mkdir ecommerce-api
cd ecommerce-apipython -m venv venv
# Activate on Windows
venv\Scripts\activate
# Activate on Mac/Linux
source venv/bin/activatepip install -r requirements.txtmkdir app
mkdir app/routers
touch app/__init__.py
touch app/routers/__init__.pyuvicorn main:app --reloadThe API will be available at: http://127.0.0.1:8000
Once running, visit:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
POST /api/v1/users- Register a new userGET /api/v1/users- Get all usersGET /api/v1/users/{user_id}- Get user by ID
POST /api/v1/products- Create a productGET /api/v1/products- Get all products (with optional category filter)GET /api/v1/products/{product_id}- Get product by IDPUT /api/v1/products/{product_id}- Update a productDELETE /api/v1/products/{product_id}- Delete a product
POST /api/v1/users/{user_id}/cart- Add item to cartGET /api/v1/users/{user_id}/cart- Get user's cartPUT /api/v1/users/{user_id}/cart/{cart_item_id}- Update cart item quantityDELETE /api/v1/users/{user_id}/cart/{cart_item_id}- Remove item from cartDELETE /api/v1/users/{user_id}/cart- Clear entire cart
POST /api/v1/users/{user_id}/orders- Create order from cartGET /api/v1/users/{user_id}/orders- Get user's ordersGET /api/v1/orders/{order_id}- Get order by IDPUT /api/v1/orders/{order_id}/status- Update order status
curl -X POST "http://127.0.0.1:8000/api/v1/users" \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"username": "johndoe",
"full_name": "John Doe",
"password": "securepassword123"
}'curl -X POST "http://127.0.0.1:8000/api/v1/products" \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop",
"description": "High-performance laptop",
"price": 999.99,
"stock": 50,
"category": "Electronics"
}'curl -X POST "http://127.0.0.1:8000/api/v1/users/1/cart" \
-H "Content-Type: application/json" \
-d '{
"product_id": 1,
"quantity": 2
}'curl -X POST "http://127.0.0.1:8000/api/v1/users/1/orders"This API demonstrates:
- FastAPI Basics: Routes, dependencies, and request/response models
- Database Operations: SQLAlchemy ORM with SQLite
- Data Validation: Pydantic schemas for request/response validation
- REST API Design: Proper HTTP methods and status codes
- Relationships: One-to-many and many-to-many database relationships
- Business Logic: Cart management, order processing, stock tracking
- Security: Password hashing with bcrypt
To use PostgreSQL instead of SQLite, modify app/database.py:
SQLALCHEMY_DATABASE_URL = "postgresql://user:password@localhost/dbname"
engine = create_engine(SQLALCHEMY_DATABASE_URL)Consider adding JWT authentication with python-jose and python-jose[cryptography].
- Add authentication with JWT tokens
- Implement pagination for list endpoints
- Add search functionality for products
- Create product reviews and ratings
- Add payment integration
- Implement email notifications
- Add API rate limiting
- Create admin dashboard
Database not created?
- Delete
ecommerce.dbif it exists and restart the app
Import errors?
- Make sure all
__init__.pyfiles exist - Check that you're in the virtual environment
Port already in use?
- Change port:
uvicorn main:app --reload --port 8001# decentralized-app