A robust e-commerce REST API built with Django REST Framework featuring user authentication, product management, shopping cart functionality, order processing, and payment integration.
- User Authentication and Authorization
- Product Catalog Management
- Shopping Cart System
- Order Processing
- Wishlist Management
- Payment Integration
- Category Management
- Admin Dashboard
- Python 3.8+
- Django 4.0+
- Django REST Framework
- SQLite/PostgreSQL
- Token Authentication
- Python 3.8 or higher
- pip (Python package manager)
- Virtual environment (recommended)
- Clone the repository
git clone https://github.com/yourusername/ecommerce-api.git
cd ecommerce-api
- Create and activate virtual environment
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python3 -m venv venv
source venv/bin/activate
- Install dependencies
pip install -r requirements.txt
- Set up environment variables
Create a
.env
file in the root directory:
DEBUG=True
SECRET_KEY=your-secret-key
DATABASE_URL=your-database-url
- Run migrations
python manage.py makemigrations
python manage.py migrate
- Create superuser
python manage.py createsuperuser
- Run development server
python manage.py runserver
The API uses token-based authentication. Include the token in the Authorization header:
Authorization: Token your-token-here
# Register new customer
POST /api/customers/
# Get customer profile
GET /api/customers/{id}/
# Update customer profile
PUT /api/customers/{id}/
# List all products
GET /api/products/
# Get product details
GET /api/products/{id}/
# Filter products by category
GET /api/products/by_category/?category_id={id}
# Get user's cart
GET /api/cart/
# Add item to cart
POST /api/cart/{cart_id}/add_item/
{
"product_id": "integer",
"quantity": "integer"
}
# Remove item from cart
POST /api/cart/{cart_id}/remove_item/
{
"product_id": "integer"
}
# Create order from cart
POST /api/orders/create_from_cart/
# Get order details
GET /api/orders/{id}/
POST /api/customers/
{
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
"phone_number": "1234567890",
"address": "123 Main St",
"dob": "1990-01-01"
}
Response:
{
"id": 1,
"user": {
"username": "john",
"email": "[email protected]"
},
"first_name": "John",
"last_name": "Doe",
"phone_number": "1234567890",
"address": "123 Main St",
"dob": "1990-01-01"
}
-
Cart Creation:
- Automatic cart creation for new users
- One active cart per user
-
Adding Items:
- Add products with specified quantity
- Automatic price calculation
- Quantity validation
-
Cart to Order Conversion:
- Create order from cart contents
- Cart items transfer to order items
- Cart cleared but preserved for future use
- Token Authentication
- Permission-based access control
- Admin-only endpoints protection
- Input validation and sanitization
- Secure password handling
The API uses standard HTTP status codes and returns detailed error messages:
{
"error": "string",
"detail": "string",
"status_code": "integer"
}
Common status codes:
400
: Bad Request401
: Unauthorized403
: Forbidden404
: Not Found500
: Server Error
class customers(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
email = models.EmailField()
phone_number = models.IntegerField()
address = models.CharField(max_length=256)
dob = models.DateField()
class products(models.Model):
name = models.CharField(max_length=200)
price = models.IntegerField()
description = models.CharField(max_length=200)
summary = models.CharField(max_length=200)
SKU = models.IntegerField()
category_type = models.ForeignKey(category)
- Automatic cart creation
- Real-time total calculation
- Quantity management
- Empty cart validation
- Cart to order conversion
- Order status management
- Payment integration
- Order history tracking
- Payment Gateway Integration
- Product Reviews & Ratings
- User Dashboard
- Order Tracking
- Email Notifications
- Inventory Management
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE.md file for details
TalibY22(https://github.com/TalibY22)