E-Commerce API Backend
A complete e-commerce backend API built with FastAPI and SQLite. This API provides all the essential features needed for an e-commerce platform including user management, product catalog, shopping cart, orders, and reviews.
Features
- User Management: Registration, authentication, profile management
- Product Catalog: Products with categories, search, and filtering
- Shopping Cart: Add, update, remove items, and checkout
- Order Management: Create orders, track status, and view history
- Reviews: Product ratings and comments
- Admin Functions: Manage products, categories, and order statuses
Tech Stack
- Framework: FastAPI
- Database: SQLite with SQLAlchemy ORM
- Authentication: JWT (JSON Web Tokens)
- Migrations: Alembic
- Validation: Pydantic
- Linting: Ruff
API Documentation
The API is self-documented with OpenAPI and provides interactive documentation at:
- Swagger UI:
/docs
- ReDoc:
/redoc
API Endpoints
Authentication
POST /api/v1/auth/register
- Register a new userPOST /api/v1/auth/login
- Login to get access token
Users
GET /api/v1/users/me
- Get current user profilePUT /api/v1/users/me
- Update current user profileGET /api/v1/users/{user_id}
- Get user by ID (admin or self only)
Products
GET /api/v1/products
- List products with filtering optionsPOST /api/v1/products
- Create a new product (admin only)GET /api/v1/products/{id}
- Get product by IDPUT /api/v1/products/{id}
- Update product (admin only)DELETE /api/v1/products/{id}
- Delete product (admin only)
Categories
GET /api/v1/categories
- List all categoriesPOST /api/v1/categories
- Create a new category (admin only)GET /api/v1/categories/{id}
- Get category by IDPUT /api/v1/categories/{id}
- Update category (admin only)DELETE /api/v1/categories/{id}
- Delete category (admin only)
Shopping Cart
GET /api/v1/cart
- Get current user's cartPOST /api/v1/cart/items
- Add item to cartPUT /api/v1/cart/items/{product_id}
- Update cart item quantityDELETE /api/v1/cart/items/{product_id}
- Remove item from cartDELETE /api/v1/cart
- Clear cart
Orders
GET /api/v1/orders
- List user's ordersPOST /api/v1/orders
- Create a new orderGET /api/v1/orders/{order_id}
- Get order by IDPUT /api/v1/orders/{order_id}
- Update order status (admin only)DELETE /api/v1/orders/{order_id}
- Cancel order (pending orders only)
Reviews
GET /api/v1/reviews/product/{product_id}
- Get reviews for a productPOST /api/v1/reviews
- Create a product reviewPUT /api/v1/reviews/{review_id}
- Update a reviewDELETE /api/v1/reviews/{review_id}
- Delete a review
Getting Started
Prerequisites
- Python 3.8+
- pip
Installation
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
Running the Application
uvicorn main:app --reload
Database Migrations
Initialize the database with:
alembic upgrade head
Project Structure
/
├── alembic.ini # Alembic configuration
├── main.py # FastAPI application entry point
├── requirements.txt # Project dependencies
├── app/ # Application package
│ ├── api/ # API endpoints
│ │ ├── deps.py # API dependencies
│ │ ├── router.py # Main API router
│ │ └── endpoints/ # API endpoint modules
│ ├── core/ # Core modules
│ │ ├── config.py # Configuration settings
│ │ └── security.py # Security utilities
│ ├── crud/ # CRUD operations
│ ├── db/ # Database setup
│ │ ├── base.py # Base model imports
│ │ ├── deps.py # Database dependencies
│ │ └── session.py # Database session
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic services
│ └── utils/ # Utility functions
└── migrations/ # Alembic migrations
├── env.py # Migration environment
├── script.py.mako # Migration script template
└── versions/ # Migration versions
Authentication
The API uses JWT for authentication. To authenticate:
- Register a user with
POST /api/v1/auth/register
- Get a token with
POST /api/v1/auth/login
- Include the token in the
Authorization
header of your requests:Authorization: Bearer {your_token}
Health Check
A health check endpoint is available at /health
to verify the API is running correctly.
Description
Languages
Python
99.2%
Mako
0.8%