Skip to content

Latest commit

ย 

History

History
510 lines (413 loc) ยท 25.8 KB

File metadata and controls

510 lines (413 loc) ยท 25.8 KB

Django 6.0 DRF 3.16 Stripe Payment JWT Auth PostgreSQL Python 3.14

๐Ÿ›’ E-Commerce REST API

A production-ready, fully-featured E-Commerce RESTful API built with Django REST Framework. This project demonstrates clean architecture, secure authentication, real payment processing with Stripe, and follows industry best practices for building scalable backend services.

๐Ÿ’ก Project idea & description from roadmap.sh โ€” E-Commerce API

๐Ÿ”— Full API Documentation โ†’


๐Ÿ“‹ Table of Contents


๐Ÿ”Ž Overview

This project is a backend API for an e-commerce platform that handles the full shopping lifecycle โ€” from user registration and product browsing, to cart management, order placement, and payment processing via Stripe.

It is designed with a service-oriented architecture that separates business logic from API views and data access, making the codebase maintainable, testable, and easy to extend.

What Can This API Do?

Capability Description
๐Ÿ” User Authentication Register, login, logout, change password with JWT (access + refresh tokens)
๐Ÿช Cookie-Based Auth Optional HTTP-only cookie delivery for frontend SPAs
๐Ÿ“ฆ Product Management Full CRUD for products, categories, and product images
๐Ÿ›’ Shopping Cart Add/update/remove items, automatic price snapshots
๐Ÿ“ Order Processing Place orders from cart with stock validation and deduction
๐Ÿ’ณ Stripe Payments Create payment intents, confirm payments, handle webhooks
๐Ÿ”„ Webhook Handling Secure Stripe webhook integration for real-time payment status updates
๐Ÿ“„ Auto-Generated Docs Interactive Swagger UI via drf-spectacular
๐Ÿ” Filtering & Search Filter products by category, search by name, order by price/date
๐Ÿ“‘ Pagination Consistent paginated responses across all list endpoints

โœจ Key Features

๐Ÿ—๏ธ Clean Architecture

  • Service Layer Pattern โ€” All business logic lives in dedicated services.py files, not in views
  • Selector Layer Pattern โ€” Data retrieval logic is separated into selectors.py files
  • Thin Views โ€” API views only handle request/response, delegating logic to services
  • Modular Apps โ€” Each domain (accounts, products, cart, orders, payments) is a self-contained Django app

๐Ÿ” Security

  • JWT Authentication with access/refresh token rotation
  • Token Blacklisting โ€” Refresh tokens are blacklisted after rotation to prevent reuse
  • HTTP-Only Cookie Support โ€” Secure cookie-based token delivery for browser clients
  • Password Validation โ€” Django's built-in password validators (similarity, minimum length, common passwords, numeric)
  • Stripe Webhook Signature Verification โ€” All webhook events are cryptographically verified
  • CORS Configuration โ€” Separate dev/prod CORS settings
  • Environment Variables โ€” All secrets are loaded from .env files, never hardcoded

๐Ÿ’ณ Payment Processing

  • Stripe Integration โ€” Full payment flow with PaymentIntent API
  • Idempotency Keys โ€” Prevents duplicate charges on retry
  • Multi-Currency Support โ€” Handles both standard and zero-decimal currencies
  • Automatic Stock Restoration โ€” Stock is restored if payment fails or is cancelled
  • Webhook-Driven Status Updates โ€” Payment status is synced in real-time via Stripe webhooks

๐Ÿ“ฆ Product & Inventory

  • Category-Based Organization โ€” Products are organized by categories with slug-based URLs
  • Stock Management โ€” Automatic stock deduction on order placement
  • Product Images โ€” Support for multiple images per product with primary image flag
  • Active/Inactive Products โ€” Soft visibility control for products

๐Ÿ›’ Cart & Orders

  • Price Snapshots โ€” Cart items and order items store the price at the time of addition (protects against price changes)
  • Cart-to-Order Conversion โ€” Seamless checkout flow from cart to order with stock validation
  • Order Status Tracking โ€” Full lifecycle: Pending โ†’ Processing โ†’ Shipped โ†’ Delivered / Cancelled
  • Unique Constraints โ€” Prevents duplicate products in the same cart or order

๐Ÿ› ๏ธ Developer Experience

  • Split Settings โ€” Separate base.py, dev.py, and prod.py settings for different environments
  • Swagger/OpenAPI Docs โ€” Auto-generated interactive API documentation at /api/docs/
  • Consistent Error Responses โ€” Standardized error format across all endpoints
  • Custom User Model โ€” Email-based authentication (no username)

๐Ÿ›๏ธ Architecture & Design Patterns

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   Client (Frontend / Mobile)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚  HTTP Request
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              Django REST Framework               โ”‚
โ”‚                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  Views   โ”‚โ†’ โ”‚ Services  โ”‚โ†’ โ”‚  Selectors   โ”‚  โ”‚
โ”‚  โ”‚ (API)    โ”‚  โ”‚ (Business โ”‚  โ”‚ (Data Access) โ”‚  โ”‚
โ”‚  โ”‚          โ”‚  โ”‚  Logic)   โ”‚  โ”‚              โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚       โ†‘              โ†“              โ†“            โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚Serializersโ”‚  โ”‚  Models   โ”‚โ†โ”€โ”‚  Database    โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ†•
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              Stripe Payment Gateway              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Layer Responsibility File
Views Handle HTTP requests/responses, authentication, permissions api/views.py
Serializers Validate input, serialize output api/serializers.py
Services Business logic, orchestration, transactions services.py
Selectors Read-only data queries and retrieval selectors.py
Models Database schema, field definitions models.py

๐Ÿงฐ Tech Stack

Technology Purpose
Python 3.14 Programming Language
Django 6.0 Web Framework
Django REST Framework 3.16 REST API Framework
PostgreSQL Relational Database
SimpleJWT JWT Authentication
Stripe Payment Processing
drf-spectacular OpenAPI/Swagger Documentation
django-filter Advanced Filtering
django-cors-headers Cross-Origin Resource Sharing
python-dotenv Environment Variable Management
psycopg2 PostgreSQL Adapter

๐Ÿ“ Project Structure

ecommerce-api-drf/
โ”‚
โ”œโ”€โ”€ config/                     # Project configuration
โ”‚   โ”œโ”€โ”€ settings/
โ”‚   โ”‚   โ”œโ”€โ”€ base.py             # Common settings
โ”‚   โ”‚   โ”œโ”€โ”€ dev.py              # Development settings (DEBUG, CORS *)
โ”‚   โ”‚   โ””โ”€โ”€ prod.py             # Production settings (security, SSL)
โ”‚   โ”œโ”€โ”€ urls.py                 # Root URL configuration
โ”‚   โ”œโ”€โ”€ wsgi.py
โ”‚   โ””โ”€โ”€ asgi.py
โ”‚
โ”œโ”€โ”€ apps/
โ”‚   โ”œโ”€โ”€ accounts/               # User authentication & management
โ”‚   โ”‚   โ”œโ”€โ”€ models.py           # CustomUser (email-based auth)
โ”‚   โ”‚   โ”œโ”€โ”€ services.py         # Registration, login, token logic
โ”‚   โ”‚   โ”œโ”€โ”€ selectors.py        # User queries
โ”‚   โ”‚   โ””โ”€โ”€ api/
โ”‚   โ”‚       โ”œโ”€โ”€ views.py
โ”‚   โ”‚       โ”œโ”€โ”€ serializers.py
โ”‚   โ”‚       โ””โ”€โ”€ urls.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ products/               # Products & categories
โ”‚   โ”‚   โ”œโ”€โ”€ models.py           # Product, Category, ProductImage
โ”‚   โ”‚   โ”œโ”€โ”€ services.py         # CRUD operations
โ”‚   โ”‚   โ”œโ”€โ”€ selectors.py        # Filtering, search queries
โ”‚   โ”‚   โ””โ”€โ”€ api/
โ”‚   โ”‚       โ”œโ”€โ”€ views.py
โ”‚   โ”‚       โ”œโ”€โ”€ serializers.py
โ”‚   โ”‚       โ””โ”€โ”€ urls.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ cart/                   # Shopping cart
โ”‚   โ”‚   โ”œโ”€โ”€ models.py           # Cart, CartItem
โ”‚   โ”‚   โ”œโ”€โ”€ services.py         # Add/update/remove items
โ”‚   โ”‚   โ”œโ”€โ”€ selectors.py        # Cart queries
โ”‚   โ”‚   โ””โ”€โ”€ api/
โ”‚   โ”‚       โ”œโ”€โ”€ views.py
โ”‚   โ”‚       โ”œโ”€โ”€ serializers.py
โ”‚   โ”‚       โ””โ”€โ”€ urls.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ orders/                 # Order management
โ”‚   โ”‚   โ”œโ”€โ”€ models.py           # Order, OrderItem
โ”‚   โ”‚   โ”œโ”€โ”€ services.py         # Order placement, status updates
โ”‚   โ”‚   โ”œโ”€โ”€ selectors.py        # Order queries
โ”‚   โ”‚   โ””โ”€โ”€ api/
โ”‚   โ”‚       โ”œโ”€โ”€ views.py
โ”‚   โ”‚       โ”œโ”€โ”€ serializers.py
โ”‚   โ”‚       โ””โ”€โ”€ urls.py
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ payments/               # Stripe payment processing
โ”‚       โ”œโ”€โ”€ models.py           # Payment model
โ”‚       โ”œโ”€โ”€ services.py         # Stripe integration, webhooks
โ”‚       โ”œโ”€โ”€ selectors.py        # Payment queries
โ”‚       โ””โ”€โ”€ api/
โ”‚           โ”œโ”€โ”€ views.py
โ”‚           โ”œโ”€โ”€ serializers.py
โ”‚           โ””โ”€โ”€ urls.py
โ”‚
โ”œโ”€โ”€ API_DOCUMENTATION.md        # Comprehensive API docs
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ manage.py
โ””โ”€โ”€ README.md

๐Ÿ—„๏ธ Database Schema

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  CustomUser  โ”‚     โ”‚   Category   โ”‚     โ”‚ ProductImage โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ email (PK)   โ”‚     โ”‚ name         โ”‚     โ”‚ image_url    โ”‚
โ”‚ first_name   โ”‚     โ”‚ slug (unique)โ”‚     โ”‚ is_primary   โ”‚
โ”‚ last_name    โ”‚     โ”‚ created_at   โ”‚     โ”‚ product (FK) โ”‚
โ”‚ password     โ”‚     โ”‚ updated_at   โ”‚     โ”‚ created_at   โ”‚
โ”‚ is_active    โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ is_staff     โ”‚            โ”‚ 1:N                โ”‚ N:1
โ”‚ date_joined  โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚   Product    โ”‚โ”€โ”€โ”€โ”€โ”€โ”‚   Product    โ”‚
       โ”‚             โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚             โ”‚ name         โ”‚
       โ”‚ 1:N         โ”‚ slug (unique)โ”‚
       โ”‚             โ”‚ description  โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚ price        โ”‚
โ”‚    Cart      โ”‚     โ”‚ stock_qty    โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ”‚ is_active    โ”‚
โ”‚ user (FK)    โ”‚     โ”‚ category(FK) โ”‚
โ”‚ created_at   โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ updated_at   โ”‚            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜            โ”‚
       โ”‚ 1:N                โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”            โ”‚
โ”‚  CartItem    โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ N:1
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ cart (FK)    โ”‚
โ”‚ product (FK) โ”‚
โ”‚ quantity     โ”‚
โ”‚ price_snap   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Order     โ”‚     โ”‚  OrderItem   โ”‚     โ”‚   Payment    โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚     โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ user (FK)    โ”‚โ”€โ”€โ”  โ”‚ order (FK)   โ”‚     โ”‚ order (1:1)  โ”‚
โ”‚ status       โ”‚  โ””โ”€โ”€โ”‚ product (FK) โ”‚     โ”‚ stripe_id    โ”‚
โ”‚ total_amount โ”‚     โ”‚ quantity     โ”‚     โ”‚ status       โ”‚
โ”‚ ship_address โ”‚     โ”‚ price_snap   โ”‚     โ”‚ amount       โ”‚
โ”‚ created_at   โ”‚     โ”‚ created_at   โ”‚     โ”‚ currency     โ”‚
โ”‚ updated_at   โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚ client_secretโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                          โ”‚ created_at   โ”‚
                                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”Œ API Endpoints

Authentication (/auth/)

Method Endpoint Description Auth
POST /auth/register/ Create a new account โŒ
POST /auth/login/ Login & get JWT tokens โŒ
POST /auth/logout/ Blacklist refresh token โœ…
POST /auth/token/refresh/ Refresh access token โŒ
POST /auth/change-password/ Change password โœ…
GET /auth/me/ Get current user profile โœ…
PATCH /auth/me/ Update current user profile โœ…

Products (/products/)

Method Endpoint Description Auth
GET /products/ List all products (filterable, searchable) โŒ
POST /products/ Create a product โœ… Admin
GET /products/{slug}/ Get product details โŒ
PUT/PATCH /products/{slug}/ Update a product โœ… Admin
DELETE /products/{slug}/ Delete a product โœ… Admin
GET /products/categories/ List all categories โŒ
POST /products/categories/ Create a category โœ… Admin

Cart (/cart/)

Method Endpoint Description Auth
GET /cart/ Get current user's cart โœ…
POST /cart/items/ Add item to cart โœ…
PATCH /cart/items/{id}/ Update item quantity โœ…
DELETE /cart/items/{id}/ Remove item from cart โœ…
DELETE /cart/clear/ Clear the entire cart โœ…

Orders (/orders/)

Method Endpoint Description Auth
GET /orders/ List user's orders โœ…
POST /orders/ Place an order from cart โœ…
GET /orders/{id}/ Get order details โœ…
PATCH /orders/{id}/status/ Update order status โœ… Admin

Payments (/payments/)

Method Endpoint Description Auth
POST /payments/create-intent/ Create Stripe PaymentIntent โœ…
POST /payments/confirm/ Confirm a payment โœ…
GET /payments/{id}/ Get payment details โœ…
POST /payments/webhook/ Stripe webhook handler โŒ

๐Ÿ“„ For complete request/response examples, see the Full API Documentation


๐Ÿš€ Getting Started

Prerequisites

  • Python 3.12+
  • PostgreSQL
  • Stripe Account (for payments)

1. Clone the Repository

git clone https://github.com/your-username/ecommerce-api-drf.git
cd ecommerce-api-drf

2. Create a Virtual Environment

python -m venv venv

# Windows
venv\Scripts\activate

# macOS/Linux
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Set Up Environment Variables

Create a .env file in the project root:

# Django
SECRET_KEY=your-super-secret-key
DJANGO_SETTINGS_MODULE=config.settings.dev

# Database
DB_ENGINE=django.db.backends.postgresql
DB_NAME=ecommerce_db
DB_USER=postgres
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432

# Stripe
STRIPE_SECRET_KEY=sk_test_...
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
STRIPE_CURRENCY=usd

5. Run Migrations

python manage.py migrate

6. Create a Superuser

python manage.py createsuperuser

7. Start the Development Server

python manage.py runserver

8. Explore the API


๐Ÿ”‘ Environment Variables

Variable Description Required
SECRET_KEY Django secret key โœ…
DJANGO_SETTINGS_MODULE Settings module path (config.settings.dev or config.settings.prod) โœ…
DB_ENGINE Database engine โœ…
DB_NAME Database name โœ…
DB_USER Database user โœ…
DB_PASSWORD Database password โœ…
DB_HOST Database host โœ…
DB_PORT Database port (default: 5432) โŒ
STRIPE_SECRET_KEY Stripe secret API key โœ…
STRIPE_PUBLISHABLE_KEY Stripe publishable key โœ…
STRIPE_WEBHOOK_SECRET Stripe webhook signing secret โœ…
STRIPE_CURRENCY Default currency (default: usd) โŒ
ALLOWED_HOSTS Comma-separated allowed hosts (prod only) โœ… (prod)

โš ๏ธ Current Limitations

While this project demonstrates solid backend fundamentals, there are some known limitations:

# Limitation Details
1 No Automated Tests The project currently lacks unit tests and integration tests. The tests.py files exist but are empty.
2 No Email Verification Users can register with any email without verification. No email confirmation or "forgot password" flow.
3 No Rate Limiting API endpoints are not rate-limited, making them vulnerable to brute-force attacks.
4 No Caching Layer No Redis/Memcached caching is implemented, which could be a bottleneck at scale.
5 No Image Upload Product images are stored as URLs only โ€” no actual file upload handling (e.g., S3, Cloudinary).
6 No Logging to File Logging is configured at a basic level without structured logging or external log management.
7 No CI/CD Pipeline No GitHub Actions, Docker, or automated deployment setup.
8 No Docker Support Project does not include a Dockerfile or docker-compose.yml for containerized deployment.
9 No Permission Granularity Admin vs. regular user permissions are basic. No role-based access control (RBAC) system.
10 No Soft Deletes Deleting a product or order permanently removes it from the database.
11 Dev Endpoint Exposed The /auth/users/ list endpoint is for development only and should be removed before production.
12 No API Versioning Endpoints are not versioned (e.g., /api/v1/), which can complicate future breaking changes.

๐Ÿ”ฎ Future Work

Here's the roadmap for upcoming improvements:

  • ๐Ÿงช Add Comprehensive Tests โ€” Unit tests for services/selectors, integration tests for API endpoints using pytest and factory_boy
  • ๐Ÿ“ง Email Verification & Password Reset โ€” Implement email confirmation on registration and "forgot password" flow
  • ๐Ÿณ Docker & Docker Compose โ€” Containerize the application with PostgreSQL, Redis, and the Django app
  • โšก Redis Caching โ€” Add caching for product listings, categories, and frequently accessed data
  • ๐Ÿšฆ Rate Limiting โ€” Implement throttling with DRF's built-in throttle classes or django-ratelimit
  • ๐Ÿ”„ API Versioning โ€” Introduce /api/v1/ prefix for all endpoints
  • ๐Ÿ“ฆ Product Variants โ€” Support for sizes, colors, and other product variations
  • โญ Reviews & Ratings โ€” Allow users to review and rate products
  • ๐Ÿ” Elasticsearch Integration โ€” Full-text search with relevance scoring
  • ๐Ÿ“Š Admin Dashboard API โ€” Sales analytics, order statistics, revenue reports
  • ๐Ÿ–ผ๏ธ Image Upload to S3/Cloudinary โ€” Replace URL-based images with actual file uploads
  • ๐Ÿ“ฑ Push Notifications โ€” Order status change notifications

๐Ÿค Contributing

Contributions are welcome! If you'd like to contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Built with โค๏ธ using Django REST Framework

๐Ÿ“„ API Docs โ€ข ๐Ÿš€ Get Started โ€ข ๐Ÿ”ฎ Roadmap