This is a sample backend application built with FastAPI and MongoDB. It simulates an e-commerce platform like Flipkart or Amazon.
- Overview
- Features
- Tech Stack
- API Endpoints
- Getting Started
- Project Structure
- Database Models
- Running the Application
- API Documentation
The E-commerce Application is designed to provide a set of APIs for managing products, orders, and user data. It allows users to view available products, create orders, retrieve order details, update product quantities, and more.
- List available products with details.
- Create new orders with items and user address.
- Fetch all orders with pagination support.
- Retrieve a single order by Order ID.
- Update product quantities as needed.
- Store product data, order data, and user information in MongoDB.
- Python 3.10+
- FastAPI: A modern web framework for building APIs with Python.
- MongoDB: A NoSQL database for storing product and order data.
- PyMongo: A Python driver for MongoDB to interact with the database.
The application provides the following API endpoints:
-
List Available Products
- Endpoint: /products (GET)
- Description: Get a list of all available products in the system.
- Query Parameters: - skip (Optional): Skip N products (default: 0, minimum: 0). - limit (Optional): Limit the number of products returned (default: 10, maximum: 100).
- Example - GET /products?skip=0&limit=20
-
Create a New Order
- Endpoint:
/orders
(POST) - Description: Create a new order with timestamp, items, and user address.
- Endpoint:
-
Fetch All Orders
- Endpoint:
/orders
(GET) - Description: Fetch all orders with pagination support (using
limit
andoffset
). - Query Parameters:
- skip (Optional): Skip N products (default: 0, minimum: 0).
- limit (Optional): Limit the number of products returned (default: 10, maximum: 100).
- Example - GET /products?skip=0&limit=20
- Endpoint:
-
Fetch a Single Order
- Endpoint:
/orders/{order_id}
(GET) - Description: Get details of a specific order by Order ID.
- Endpoint:
-
Update Product Quantity
- Endpoint:
/api/products/{product_name}
(PUT) - Description: Update the available quantity for a product.
- Endpoint:
-
Fetch a Single Product
- Endpoint:
/products/{product_name}
(GET) - Description: Get details of a specific product by Product name.
- Endpoint:
To run the application locally, follow these steps:
-
Clone the repository: git clone https://github.com/yourusername/ecommerce-app-fastapi.git cd ecommerce-application
-
Create a virtual environment (optional but recommended): python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
-
Install the project dependencies:
-
Set up environment variables: Create a .env file in the project root directory with the following content: MONGODB_URI=mongodb://localhost:27017/ecommerce
Initialize the database:
python database/connection.py Run the FastAPI server:
uvicorn main:app --reload The application should now be accessible at http://localhost:8000.
The project follows a structured layout with key directories:
api: Contains FastAPI router files for defining API endpoints. models: Defines Pydantic models for request and response objects. utils: Includes Python scripts other utilities. database: Include Python scripts for initializing the database. main.py: The main application file where FastAPI is configured.
MongoDB is used to store product and order data. The data models include:
Product: Represents a product with attributes such as name, price, and available quantity. Order: Represents an order with properties like timestamp, items purchased, and user address details.
After following the installation steps, the application can be accessed at http://localhost:8000. You can use the API endpoints mentioned in the API Endpoints section to interact with the application.
API documentation is available at http://localhost:8000/docs. You can explore and test the API using the Swagger UI.