
- Setup project structure and FastAPI application - Create SQLAlchemy models for users, products, carts, and orders - Implement Alembic migrations - Add CRUD operations and endpoints for all resources - Setup authentication with JWT - Add role-based access control - Update documentation
3.5 KiB
3.5 KiB
Simple Ecommerce API
A simple ecommerce API built with FastAPI and SQLite.
Features
- User authentication (register, login)
- Product management (CRUD)
- Shopping cart functionality
- Order management
- Role-based access control (admin/regular users)
Tech Stack
- Python 3.9+
- FastAPI
- SQLAlchemy ORM
- Alembic for database migrations
- SQLite for database
- JWT for authentication
API Endpoints
Authentication
POST /api/v1/auth/register
: Register a new userPOST /api/v1/auth/login
: Login and get access token
Products
GET /api/v1/products
: List all productsGET /api/v1/products/{id}
: Get a specific productPOST /api/v1/products
: Create a new product (admin only)PUT /api/v1/products/{id}
: Update a product (admin only)DELETE /api/v1/products/{id}
: Delete a product (admin only)
Cart
GET /api/v1/cart
: Get current user's cartPOST /api/v1/cart/items
: Add an item to cartPUT /api/v1/cart/items/{id}
: Update cart item quantityDELETE /api/v1/cart/items/{id}
: Remove an item from cartDELETE /api/v1/cart
: Clear the cart
Orders
GET /api/v1/orders
: List user's orders (or all orders for admin)POST /api/v1/orders
: Create a new order from cartGET /api/v1/orders/{id}
: Get a specific order with detailsPUT /api/v1/orders/{id}/status
: Update order status (admin only)DELETE /api/v1/orders/{id}
: Cancel an order
Users
GET /api/v1/users
: List all users (admin only)GET /api/v1/users/me
: Get current user detailsPUT /api/v1/users/me
: Update current user detailsGET /api/v1/users/{id}
: Get a specific user (admin only)PUT /api/v1/users/{id}
: Update a user (admin only)DELETE /api/v1/users/{id}
: Delete a user (admin only)
Project Structure
.
├── alembic.ini # Alembic configuration
├── app/ # Main application package
│ ├── api/ # API endpoints
│ │ └── v1/ # API version 1
│ ├── core/ # Core functionality
│ ├── crud/ # Database CRUD operations
│ ├── db/ # Database session and models
│ ├── models/ # SQLAlchemy models
│ └── schemas/ # Pydantic schemas
├── main.py # Application entry point
├── migrations/ # Database migrations
└── requirements.txt # Project dependencies
Getting Started
Prerequisites
- Python 3.9+
Installation
-
Clone the repository:
git clone <repository-url> cd simple-ecommerce-api
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
export SECRET_KEY="your-secret-key" export API_URL="http://localhost:8000" # Optional, default is http://localhost:8000
-
Run database migrations:
alembic upgrade head
-
Start the server:
uvicorn main:app --reload
-
Open your browser and navigate to
http://localhost:8000/docs
to see the interactive API documentation.
Environment Variables
Variable | Description | Default |
---|---|---|
SECRET_KEY | JWT secret key | "supersecretkey" |
API_URL | Base URL for the API | "http://localhost:8000" |
FIRST_SUPERUSER_EMAIL | Email for initial superuser | None |
FIRST_SUPERUSER_PASSWORD | Password for initial superuser | None |
License
This project is licensed under the MIT License.