Skip to content

Nuel36c/E-commerce-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI E-commerce API

A beginner-friendly e-commerce API built with FastAPI, featuring user management, product catalog, shopping cart, and order processing.

πŸ“ Project Structure

ecommerce-api/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ database.py          # Database configuration
β”‚   β”œβ”€β”€ models.py            # SQLAlchemy models
β”‚   β”œβ”€β”€ schemas.py           # Pydantic schemas
β”‚   └── routers/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ users.py         # User endpoints
β”‚       β”œβ”€β”€ products.py      # Product endpoints
β”‚       β”œβ”€β”€ cart.py          # Cart endpoints
β”‚       └── orders.py        # Order endpoints
β”‚
β”œβ”€β”€ main.py                  # Application entry point
β”œβ”€β”€ requirements.txt         # Python dependencies
└── README.md               # This file

πŸš€ Setup Instructions

1. Create Project Directory

mkdir ecommerce-api
cd ecommerce-api

2. Create Virtual Environment

python -m venv venv

# Activate on Windows
venv\Scripts\activate

# Activate on Mac/Linux
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Create Folder Structure

mkdir app
mkdir app/routers
touch app/__init__.py
touch app/routers/__init__.py

5. Run the Application

uvicorn main:app --reload

The API will be available at: http://127.0.0.1:8000

πŸ“š API Documentation

Once running, visit:

πŸ”‘ API Endpoints

Users

  • POST /api/v1/users - Register a new user
  • GET /api/v1/users - Get all users
  • GET /api/v1/users/{user_id} - Get user by ID

Products

  • POST /api/v1/products - Create a product
  • GET /api/v1/products - Get all products (with optional category filter)
  • GET /api/v1/products/{product_id} - Get product by ID
  • PUT /api/v1/products/{product_id} - Update a product
  • DELETE /api/v1/products/{product_id} - Delete a product

Cart

  • POST /api/v1/users/{user_id}/cart - Add item to cart
  • GET /api/v1/users/{user_id}/cart - Get user's cart
  • PUT /api/v1/users/{user_id}/cart/{cart_item_id} - Update cart item quantity
  • DELETE /api/v1/users/{user_id}/cart/{cart_item_id} - Remove item from cart
  • DELETE /api/v1/users/{user_id}/cart - Clear entire cart

Orders

  • POST /api/v1/users/{user_id}/orders - Create order from cart
  • GET /api/v1/users/{user_id}/orders - Get user's orders
  • GET /api/v1/orders/{order_id} - Get order by ID
  • PUT /api/v1/orders/{order_id}/status - Update order status

πŸ’‘ Example Usage

1. Register a User

curl -X POST "http://127.0.0.1:8000/api/v1/users" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "username": "johndoe",
    "full_name": "John Doe",
    "password": "securepassword123"
  }'

2. Create a Product

curl -X POST "http://127.0.0.1:8000/api/v1/products" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Laptop",
    "description": "High-performance laptop",
    "price": 999.99,
    "stock": 50,
    "category": "Electronics"
  }'

3. Add to Cart

curl -X POST "http://127.0.0.1:8000/api/v1/users/1/cart" \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": 1,
    "quantity": 2
  }'

4. Create Order

curl -X POST "http://127.0.0.1:8000/api/v1/users/1/orders"

πŸŽ“ Learning Points

This API demonstrates:

  1. FastAPI Basics: Routes, dependencies, and request/response models
  2. Database Operations: SQLAlchemy ORM with SQLite
  3. Data Validation: Pydantic schemas for request/response validation
  4. REST API Design: Proper HTTP methods and status codes
  5. Relationships: One-to-many and many-to-many database relationships
  6. Business Logic: Cart management, order processing, stock tracking
  7. Security: Password hashing with bcrypt

πŸ”§ Customization

Change Database

To use PostgreSQL instead of SQLite, modify app/database.py:

SQLALCHEMY_DATABASE_URL = "postgresql://user:password@localhost/dbname"
engine = create_engine(SQLALCHEMY_DATABASE_URL)

Add Authentication

Consider adding JWT authentication with python-jose and python-jose[cryptography].

πŸ“ Next Steps

  • Add authentication with JWT tokens
  • Implement pagination for list endpoints
  • Add search functionality for products
  • Create product reviews and ratings
  • Add payment integration
  • Implement email notifications
  • Add API rate limiting
  • Create admin dashboard

πŸ› Troubleshooting

Database not created?

  • Delete ecommerce.db if it exists and restart the app

Import errors?

  • Make sure all __init__.py files exist
  • Check that you're in the virtual environment

Port already in use?

  • Change port: uvicorn main:app --reload --port 8001# decentralized-app

decentralized-app

E-commerce-API

E-commerce-API

E-commerce-API

E-commerce-API

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages