Skip to content

Achal13jain/Ecommerce_backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›’ E-commerce Backend API

FastAPI-powered RESTful backend with JWT authentication, product management, and order processing

Python FastAPI PostgreSQL

Features β€’ Tech Stack β€’ Getting Started β€’ API Documentation β€’ Project Structure


πŸ“‹ Overview

A production-ready e-commerce backend system built with FastAPI that provides secure user authentication, comprehensive product management, shopping cart functionality, and order processing capabilities. Designed with scalability, security, and modularity at its core.

🎯 Key Highlights

  • Secure Authentication: JWT-based authentication with OAuth2 password flow and bcrypt password hashing
  • Role-Based Access Control: Separate admin and user permissions for resource management
  • Complete E-commerce Flow: From product browsing to order placement
  • Database Migrations: Alembic integration for seamless schema versioning
  • RESTful API Design: Clean, intuitive endpoints following REST conventions

✨ Features

πŸ” Authentication & Authorization

  • User registration and login with hashed password storage
  • JWT access token generation and validation
  • Protected routes with role-based access control

πŸ“¦ Product Management

  • Full CRUD operations for products (admin only)
  • Public product catalog with search and filtering capabilities
  • Detailed product information retrieval

πŸ›’ Shopping Cart

  • Add, update, and remove cart items
  • Price tracking at time of addition
  • Persistent cart across sessions

πŸ“Š Order Processing

  • Place orders directly from cart
  • Order status tracking and history
  • User-specific order management

πŸ›  Tech Stack

Component Technology
Framework FastAPI
Database PostgreSQL
ORM SQLAlchemy
Authentication JWT + OAuth2
Password Hashing Passlib (bcrypt)
Migrations Alembic
Configuration Pydantic Settings
Environment Python 3.9+ Virtual Environment

πŸš€ Getting Started

Prerequisites

Ensure you have the following installed on your system:

  • Python 3.9 or higher
  • PostgreSQL 12 or higher
  • pip (Python package manager)
  • virtualenv or venv

Installation

  1. Clone the repository
git clone https://github.com/Achal13jain/Ecommerce_backend.git
cd Ecommerce_backend
  1. Create and activate virtual environment

Windows

python -m venv venv
venv\Scripts\activate

Linux/MacOS

python3 -m venv venv
source venv/bin/activate
  1. Install dependencies
pip install -r requirements.txt
  1. Configure environment variables

Create a .env file in the root directory:

DATABASE_URL=postgresql://username:password@localhost:5432/ecommerce_db
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
  1. Initialize database

Create database

createdb ecommerce_db

Run migrations

alembic upgrade head
  1. Run the application
uvicorn app.main:app --reload

The API will be available at http://localhost:8000.


πŸ“š API Documentation

Once the server is running, access the interactive API documentation:

πŸ”‘ Core Endpoints

Authentication

  • POST /auth/signup - Register new user
  • POST /auth/signin - User login (returns JWT token)

Products

  • GET /products - List all products (public)
  • GET /products/{id} - Get product details (public)
  • POST /products - Create product (admin only)
  • PUT /products/{id} - Update product (admin only)
  • DELETE /products/{id} - Delete product (admin only)

Cart

  • GET /cart - View cart items
  • POST /cart - Add item to cart
  • PUT /cart/{id} - Update cart item quantity
  • DELETE /cart/{id} - Remove item from cart

Orders

  • POST /orders - Place order from cart
  • GET /orders - View order history
  • GET /orders/{id} - Get order details

πŸ“‚ Project Structure

Ecommerce_backend/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ init.py
β”‚ β”œβ”€β”€ main.py # Application entry point
β”‚ β”œβ”€β”€ config.py # Configuration settings
β”‚ β”œβ”€β”€ database.py # Database connection
β”‚ β”œβ”€β”€ models/ # SQLAlchemy models
β”‚ β”‚ β”œβ”€β”€ user.py
β”‚ β”‚ β”œβ”€β”€ product.py
β”‚ β”‚ β”œβ”€β”€ cart.py
β”‚ β”‚ └── order.py
β”‚ β”œβ”€β”€ schemas/ # Pydantic schemas
β”‚ β”‚ β”œβ”€β”€ user.py
β”‚ β”‚ β”œβ”€β”€ product.py
β”‚ β”‚ β”œβ”€β”€ cart.py
β”‚ β”‚ └── order.py
β”‚ β”œβ”€β”€ routers/ # API route handlers
β”‚ β”‚ β”œβ”€β”€ auth.py
β”‚ β”‚ β”œβ”€β”€ products.py
β”‚ β”‚ β”œβ”€β”€ cart.py
β”‚ β”‚ └── orders.py
β”‚ └── utils/ # Utility functions
β”‚ β”œβ”€β”€ auth.py # JWT & password hashing
β”‚ └── dependencies.py # Dependency injection
β”œβ”€β”€ alembic/ # Database migrations
β”‚ β”œβ”€β”€ versions/
β”‚ └── env.py
β”œβ”€β”€ tests/ # Unit and integration tests
β”œβ”€β”€ .env.example # Environment variables template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ alembic.ini # Alembic configuration
β”œβ”€β”€ requirements.txt # Python dependencies
└── README.md

πŸ”’ Security Features

  • Password hashing using bcrypt algorithm
  • JWT token-based authentication with expiration
  • Protected routes with dependency injection
  • SQL injection prevention via SQLAlchemy ORM
  • Environment-based configuration management

πŸš€ Future Enhancements

  • Payment gateway integration (Stripe/PayPal)
  • Email notification system
  • Product image upload and storage
  • Advanced filtering and search
  • Rate limiting and throttling
  • Redis caching layer
  • Docker containerization
  • CI/CD pipeline setup

🀝 Contributing

Contributions are welcome! Please follow these steps:

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

Please ensure your code follows the existing style and includes appropriate tests.


πŸ‘€ Author

Achal Jain


πŸ™ Acknowledgments

  • FastAPI documentation and community
  • SQLAlchemy ORM framework
  • JWT authentication best practices

⭐ Star this repository if you find it helpful!

About

FastAPI-powered e-commerce backend with JWT auth, product catalog, cart & order APIs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors