E-Commerce API
A full-featured e-commerce API built with FastAPI and SQLite.
Features
- User Management: Registration, authentication, and profile management
- Product Management: Products with categories, pricing, and inventory management
- Shopping Cart: Add, update, remove items in user's shopping cart
- Order Processing: Create orders from cart, track order status
- Authentication: Username-based JWT authentication with role-based access control
Tech Stack
- FastAPI: Modern, fast API framework
- SQLAlchemy: SQL toolkit and ORM
- Alembic: Database migration tool
- SQLite: Serverless database engine
- JWT: JSON Web Tokens for authentication
- Pydantic: Data validation and settings management
API Endpoints
Authentication
POST /api/v1/auth/login
: Login with username and password to get access tokenPOST /api/v1/auth/register
: Register a new user (requires username, email, and password)GET /api/v1/auth/me
: Get current user information
Users
GET /api/v1/users/
: List all users (admin only)POST /api/v1/users/
: Create a new user (admin only)GET /api/v1/users/me
: Get current user profilePUT /api/v1/users/me
: Update current user profileGET /api/v1/users/{user_id}
: Get user by IDPUT /api/v1/users/{user_id}
: Update user (admin only)
Categories
GET /api/v1/categories/
: List all categoriesPOST /api/v1/categories/
: Create a new category (admin only)GET /api/v1/categories/{category_id}
: Get category by IDPUT /api/v1/categories/{category_id}
: Update category (admin only)DELETE /api/v1/categories/{category_id}
: Delete category (admin only)
Products
GET /api/v1/products/
: List all products (filter by category optional)POST /api/v1/products/
: Create a new product (admin only)GET /api/v1/products/{product_id}
: Get product by IDPUT /api/v1/products/{product_id}
: Update product (admin only)DELETE /api/v1/products/{product_id}
: Delete product (admin only)
Cart
GET /api/v1/cart/
: Get current user's cartPOST /api/v1/cart/items
: Add item to cartPUT /api/v1/cart/items/{item_id}
: Update cart item quantityDELETE /api/v1/cart/items/{item_id}
: Remove item from cartDELETE /api/v1/cart/
: Clear cart
Orders
GET /api/v1/orders/
: List user's ordersPOST /api/v1/orders/
: Create new order from cartGET /api/v1/orders/{order_id}
: Get order by IDPUT /api/v1/orders/{order_id}
: Update order status (admin only)POST /api/v1/orders/{order_id}/cancel
: Cancel order (if pending)
Health Check
GET /health
: Application health check
Installation and Setup
Prerequisites
- Python 3.8+
- pip (Python package installer)
Installation
- Clone the repository
git clone <repository-url>
cd ecommerceapplication
- Install dependencies
pip install -r requirements.txt
- Run database migrations
alembic upgrade head
- Start the application
uvicorn main:app --reload
The API will be available at http://localhost:8000.
Authentication Note
This API uses username-based authentication rather than email-based. When registering and logging in:
- Username is the primary identifier for authentication
- Email is still required during registration but is not used for login
- Password must meet security requirements (minimum 8 characters)
Documentation
FastAPI provides automatic API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Development
Database Migrations
Create a new migration after model changes:
alembic revision --autogenerate -m "Description of changes"
Apply migrations:
alembic upgrade head
Recent Migrations
- 2a3b4c5d6e7f_add_username_field: Added username field to User model and migrated from email-based to username-based authentication
Running Tests
pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
Description
Languages
Python
99.2%
Mako
0.8%