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 user
  • POST /api/v1/auth/login - Login to get access token

Users

  • GET /api/v1/users/me - Get current user profile
  • PUT /api/v1/users/me - Update current user profile
  • GET /api/v1/users/{user_id} - Get user by ID (admin or self only)

Products

  • GET /api/v1/products - List products with filtering options
  • POST /api/v1/products - Create a new product (admin only)
  • GET /api/v1/products/{id} - Get product by ID
  • PUT /api/v1/products/{id} - Update product (admin only)
  • DELETE /api/v1/products/{id} - Delete product (admin only)

Categories

  • GET /api/v1/categories - List all categories
  • POST /api/v1/categories - Create a new category (admin only)
  • GET /api/v1/categories/{id} - Get category by ID
  • PUT /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 cart
  • POST /api/v1/cart/items - Add item to cart
  • PUT /api/v1/cart/items/{product_id} - Update cart item quantity
  • DELETE /api/v1/cart/items/{product_id} - Remove item from cart
  • DELETE /api/v1/cart - Clear cart

Orders

  • GET /api/v1/orders - List user's orders
  • POST /api/v1/orders - Create a new order
  • GET /api/v1/orders/{order_id} - Get order by ID
  • PUT /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 product
  • POST /api/v1/reviews - Create a product review
  • PUT /api/v1/reviews/{review_id} - Update a review
  • DELETE /api/v1/reviews/{review_id} - Delete a review

Getting Started

Prerequisites

  • Python 3.8+
  • pip

Installation

  1. Clone the repository
  2. 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:

  1. Register a user with POST /api/v1/auth/register
  2. Get a token with POST /api/v1/auth/login
  3. 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
Project: E-commerce Backend API
Readme 64 KiB
Languages
Python 99.2%
Mako 0.8%