The Cart API provides comprehensive cart management functionality for the e-commerce platform. It includes operations for adding/removing items, applying promotions, cart validation, and advanced features like cart merging and batch operations.
/api/v1/cart
Endpoint: GET /{user_id}
Description: Retrieve a user's cart with all items and totals.
Parameters:
user_id(path, required): User identifier
Response:
{
"success": true,
"cart": {
"user_id": "user123",
"items": [
{
"product_id": "prod001",
"product_name": "Laptop",
"quantity": 1,
"price": 999.99,
"discount_percentage": 0,
"total": 999.99,
"added_at": "2024-01-15T10:30:00"
}
],
"subtotal": 999.99,
"discount_amount": 0,
"total_price": 999.99,
"item_count": 1,
"coupon_code": null,
"last_updated": "2024-01-15T10:30:00"
},
"message": "Cart retrieved successfully",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Success404: Cart not found500: Server error
Endpoint: POST /{user_id}/items
Description: Add a single item to the user's cart.
Parameters:
user_id(path, required): User identifier
Request Body:
{
"product_id": "prod001",
"quantity": 2,
"price": 99.99
}Response: Updated cart (same format as Get Cart)
Status Codes:
200: Item added successfully400: Invalid request or operation failed500: Server error
Endpoint: PUT /{user_id}/items/{product_id}
Description: Update the quantity of an item in the cart.
Parameters:
user_id(path, required): User identifierproduct_id(path, required): Product identifier
Request Body:
{
"quantity": 5
}Response: Updated cart
Status Codes:
200: Quantity updated successfully400: Invalid quantity or operation failed500: Server error
Notes: If quantity is 0 or less, the item will be removed.
Endpoint: DELETE /{user_id}/items/{product_id}
Description: Remove a specific item from the user's cart.
Parameters:
user_id(path, required): User identifierproduct_id(path, required): Product identifier to remove
Response:
{
"success": true,
"cart": {...},
"message": "Product prod001 removed from cart",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Item removed successfully400: Operation failed500: Server error
Endpoint: DELETE /{user_id}
Description: Remove all items from the user's cart.
Parameters:
user_id(path, required): User identifier
Response:
{
"success": true,
"message": "Cart cleared successfully",
"user_id": "user123",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Cart cleared successfully400: Operation failed500: Server error
Endpoint: GET /{user_id}/summary
Description: Get a detailed summary of the cart with item breakdown.
Parameters:
user_id(path, required): User identifier
Response:
{
"success": true,
"user_id": "user123",
"item_count": 2,
"items": [
{
"product_id": "prod001",
"product_name": "Laptop",
"quantity": 1,
"unit_price": 999.99,
"total": 999.99,
"discount": 0
},
{
"product_id": "prod002",
"product_name": "Mouse",
"quantity": 2,
"unit_price": 29.99,
"total": 59.98,
"discount": 10
}
],
"subtotal": 1059.97,
"discount_amount": 106,
"total": 953.97,
"coupon": "SAVE10",
"coupon_discount": 106,
"last_updated": "2024-01-15T10:30:00",
"abandoned": false,
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Success404: Cart not found500: Server error
Endpoint: POST /{user_id}/validate
Description: Validate the cart for errors, warnings, and suggestions.
Parameters:
user_id(path, required): User identifier
Response:
{
"valid": true,
"errors": [],
"warnings": [
"Cart has many items (25) - consider splitting order"
],
"suggestions": [
"Large order detected - verify shipping address and timeline"
]
}Status Codes:
200: Validation completed404: Cart not found500: Server error
Validation Checks:
- Cart existence
- Item availability
- Price validity
- Duplicate items
- Cart size warnings
- Order value suggestions
- Abandoned cart warnings
Endpoint: POST /{user_id}/apply-coupon
Description: Apply a coupon or promotion code to the cart.
Parameters:
user_id(path, required): User identifiercoupon_code(query, required): Promotion code
Example:
POST /api/v1/cart/user123/apply-coupon?coupon_code=SAVE10
Response:
{
"success": true,
"coupon_code": "SAVE10",
"discount_amount": 50.00,
"new_total": 949.97,
"message": "Coupon applied successfully",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Coupon applied successfully400: Invalid coupon or operation failed500: Server error
Endpoint: POST /{user_id}/merge
Description: Merge another user's cart into this user's cart. Useful for guest-to-registered user conversion.
Parameters:
user_id(path, required): Target user ID (receives items)source_user_id(query, required): Source user ID (items will be moved)
Example:
POST /api/v1/cart/user123/merge?source_user_id=guest456
Response:
{
"success": true,
"merged_items": 3,
"target_user": "user123",
"source_user": "guest456",
"message": "Carts merged successfully",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Carts merged successfully400: Invalid request or operation failed500: Server error
Notes:
- Source cart will be cleared after merge
- Duplicate products will have quantities combined
Endpoint: GET /{user_id}/delivery-estimate
Description: Get estimated delivery date for the cart.
Parameters:
user_id(path, required): User identifierdays(query, optional): Delivery days (1-30, default: 3)
Example:
GET /api/v1/cart/user123/delivery-estimate?days=5
Response:
{
"success": true,
"estimated_delivery": "2024-01-20T10:35:00",
"days": 5,
"item_count": 2,
"total_value": 953.97,
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Success404: Cart not found500: Server error
Endpoint: POST /{user_id}/batch-add
Description: Add multiple items to cart in a single operation.
Parameters:
user_id(path, required): User identifier
Request Body:
[
{
"product_id": "prod001",
"quantity": 1,
"price": 99.99
},
{
"product_id": "prod002",
"quantity": 2,
"price": 29.99
}
]Response:
{
"total": 2,
"success": 2,
"failed": 0,
"errors": [],
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Batch operation completed400: Invalid request500: Server error
Endpoint: POST /{user_id}/recalculate
Description: Recalculate cart totals after price changes or updates.
Parameters:
user_id(path, required): User identifier
Response:
{
"success": true,
"message": "Cart totals recalculated",
"user_id": "user123",
"new_total": 953.97,
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Totals recalculated successfully400: Operation failed500: Server error
Endpoint: GET /{user_id}/items
Description: Get all items in cart with optional filtering.
Parameters:
user_id(path, required): User identifierproduct_id(query, optional): Filter by product ID
Example:
GET /api/v1/cart/user123/items?product_id=prod001
Response:
{
"success": true,
"user_id": "user123",
"item_count": 2,
"items": [
{
"product_id": "prod001",
"product_name": "Laptop",
"quantity": 1,
"price": 999.99,
"discount_percentage": 0,
"total": 999.99,
"added_at": "2024-01-15T10:30:00"
}
],
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Success404: Cart not found500: Server error
Endpoint: GET /{user_id}/total
Description: Get the total amount for the cart with breakdown.
Parameters:
user_id(path, required): User identifier
Response:
{
"success": true,
"user_id": "user123",
"subtotal": 1059.97,
"discount": 106.00,
"total": 953.97,
"item_count": 2,
"currency": "USD",
"timestamp": "2024-01-15T10:35:00"
}Status Codes:
200: Success404: Cart not found500: Server error
All errors are returned in the following format:
{
"detail": "Error description"
}Common Error Codes:
400 Bad Request: Invalid input or operation failed404 Not Found: Cart or resource not found500 Internal Server Error: Server-side error
- Validation: Always validate cart before checkout using the
/validateendpoint - Batch Operations: Use batch-add for multiple items to reduce API calls
- Cart Summary: Get summary before displaying to user to ensure up-to-date totals
- Error Handling: Check response success flag and handle errors appropriately
- Caching: Cache cart data client-side and refresh on user actions
# Add item
curl -X POST http://localhost:8000/api/v1/cart/user123/items \
-H "Content-Type: application/json" \
-d '{"product_id": "prod001", "quantity": 1, "price": 99.99}'
# Get summary
curl http://localhost:8000/api/v1/cart/user123/summary# Apply coupon
curl -X POST http://localhost:8000/api/v1/cart/user123/apply-coupon?coupon_code=SAVE10
# Get total
curl http://localhost:8000/api/v1/cart/user123/total# Batch add items
curl -X POST http://localhost:8000/api/v1/cart/user123/batch-add \
-H "Content-Type: application/json" \
-d '[
{"product_id": "prod001", "quantity": 1, "price": 99.99},
{"product_id": "prod002", "quantity": 2, "price": 29.99}
]'
# Validate cart
curl -X POST http://localhost:8000/api/v1/cart/user123/validate# Merge guest cart to user cart
curl -X POST http://localhost:8000/api/v1/cart/user123/merge?source_user_id=guest456{
"product_id": str, # Product identifier
"product_name": str, # Product name (optional)
"quantity": int, # Item quantity (minimum 1)
"price": float, # Unit price
"discount_percentage": float, # Discount percentage (0-100)
"total": float, # Total for this item
"added_at": datetime # When item was added
}{
"user_id": str, # User identifier
"items": List[CartItem], # List of cart items
"subtotal": float, # Subtotal before discounts
"discount_amount": float, # Total discount amount
"total_price": float, # Final total
"item_count": int, # Number of items
"coupon_code": str, # Applied coupon code
"coupon_discount": float, # Coupon discount amount
"estimated_delivery": datetime, # Estimated delivery date
"last_updated": datetime, # Last update timestamp
"abandoned": bool, # Whether cart is abandoned
"abandoned_at": datetime # When cart was abandoned
}{
"valid": bool, # Overall validation status
"errors": List[str], # List of errors
"warnings": List[str], # List of warnings
"suggestions": List[str] # List of suggestions
}Currently, no rate limiting is implemented. Future versions may include:
- Per-user request limits
- Global request throttling
- IP-based rate limiting
Current API Version: v1
Future versions will maintain backward compatibility where possible.