This is an E-Commerce API built using FastAPI. The project provides endpoints to manage an e-commerce platform, including user authentication, product management, and order processing.
- Installation
- Environment Variables
- Database Setup
- Running the Server
- Application Flow
- Models
- Functions and Endpoints
- Static Files
- License
- Python 3.8 or higher
- PostgreSQL
- Git
git clone https://github.com/majumdersubhanu/e_commerce_api.git
cd e_commerce_apipython -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`pip install -r requirements.txtCreate a .env file in the root directory of the project and add the following variables:
DB_USERNAME='your_db_username'
DB_PASSWORD='your_db_password'
DB_HOST='localhost'
DB_PORT='5432'
DB_NAME='ecommerce'
USERNAME='your_username'
PASSWORD='your_password'
SECRET='your_secret_key'
BASE_URL='http://127.0.0.1:8000'Make sure PostgreSQL is running and create a database named ecommerce:
CREATE DATABASE ecommerce;uvicorn main:app --reloadThe server will start at http://127.0.0.1:8000.
- Users must be authenticated to access most endpoints.
- Token-based authentication is used.
- Users can obtain a token by providing valid credentials.
- Admin users can create, update, and delete products.
- All users can view product listings and details.
- Authenticated users can create orders.
- Users can view their own orders.
Represents a user in the system.
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)Represents a product available for purchase.
class Product(Base):
__tablename__ = "products"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
description = Column(String, index=True)
price = Column(Float)
inventory_count = Column(Integer)Represents an order placed by a user.
class Order(Base):
__tablename__ = "orders"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
product_id = Column(Integer, ForeignKey("products.id"))
quantity = Column(Integer)
order_date = Column(DateTime, default=datetime.utcnow)- POST
/token- Obtain a token- Function:
create_token - Request Body:
username,password - Response:
access_token
- Function:
-
POST
/users- Create a new user- Function:
create_user - Request Body:
username,email,password - Response:
User
- Function:
-
GET
/users/me- Get current user details- Function:
read_users_me - Header:
Authorization: Bearer <token> - Response:
User
- Function:
-
GET
/products- List all products- Function:
get_products - Response: List of
Product
- Function:
-
POST
/products- Create a new product- Function:
create_product - Request Body:
name,description,price,inventory_count - Header:
Authorization: Bearer <token> - Response:
Product
- Function:
-
GET
/products/{product_id}- Get product details- Function:
get_product - Path Parameter:
product_id - Response:
Product
- Function:
-
POST
/orders- Create a new order- Function:
create_order - Request Body:
product_id,quantity - Header:
Authorization: Bearer <token> - Response:
Order
- Function:
-
GET
/orders- List all orders for the current user- Function:
get_orders - Header:
Authorization: Bearer <token> - Response: List of
Order
- Function:
The static/images/ folder is used to store product images. Make sure this directory exists in your project structure:
mkdir -p static/images/This project is licensed under the MIT License. See the LICENSE file for more details.