Overview
The Orders API provides endpoints for creating and managing customer orders. All order endpoints require authentication.
GET /api/orders
Retrieve all orders for the authenticated user.
Request
Method: GET
Path: /api/orders
Authentication: JWT token required
Headers:
Authorization: Bearer <access_token>
Response
Array of order objects for the current userOrder’s unique identifier
User ID who placed the order
Order status (pending, paid, shipped, delivered, cancelled)
Order subtotal before tax and discounts
Discount code used (if any)
Array of order itemsorders[].items[].order_id
Associated order ID
orders[].items[].product_id
Product ID
orders[].items[].product_name
Product name
orders[].items[].quantity
Quantity ordered
orders[].items[].unit_price
Price per unit at time of order
orders[].items[].total_price
Total price for this item (unit_price × quantity)
Example
curl -X GET http://localhost:5000/api/orders \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Response (200 OK):
{
"orders": [
{
"id": 1,
"user_id": 1,
"status": "paid",
"subtotal": 1299.99,
"tax": 104.0,
"discount_amount": 0.0,
"total": 1403.99,
"discount_code": null,
"created_at": "2024-01-15T10:00:00",
"updated_at": "2024-01-15T10:05:00",
"items": [
{
"id": 1,
"order_id": 1,
"product_id": 1,
"product_name": "Laptop Pro 15",
"quantity": 1,
"unit_price": 1299.99,
"total_price": 1299.99
}
]
}
],
"count": 1
}
Error Responses
401 Unauthorized - Missing or invalid token:
{
"msg": "Missing Authorization Header"
}
GET /api/orders/<id>
Retrieve a specific order by ID.
Request
Method: GET
Path: /api/orders/<id>
Authentication: JWT token required
Headers:
Authorization: Bearer <access_token>
Path Parameters:
Response
Order object with all details including items (same structure as orders array above)
Example
curl -X GET http://localhost:5000/api/orders/1 \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Response (200 OK):
{
"order": {
"id": 1,
"user_id": 1,
"status": "paid",
"subtotal": 1299.99,
"tax": 104.0,
"discount_amount": 0.0,
"total": 1403.99,
"discount_code": null,
"created_at": "2024-01-15T10:00:00",
"updated_at": "2024-01-15T10:05:00",
"items": [
{
"id": 1,
"order_id": 1,
"product_id": 1,
"product_name": "Laptop Pro 15",
"quantity": 1,
"unit_price": 1299.99,
"total_price": 1299.99
}
]
}
}
Error Responses
401 Unauthorized - Missing or invalid token:
{
"msg": "Missing Authorization Header"
}
404 Not Found - Order not found or doesn’t belong to user:
{
"error": "Order not found"
}
POST /api/orders
Create a new order.
Request
Method: POST
Path: /api/orders
Authentication: JWT token required
Headers:
Authorization: Bearer <access_token>
Content-Type: application/json
Body Parameters:
Array of order items. Must include at least one item.
Optional discount code to apply
Response
Created order object with all details including items
Example
curl -X POST http://localhost:5000/api/orders \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
-H "Content-Type: application/json" \
-d '{
"items": [
{
"product_id": 1,
"quantity": 1
},
{
"product_id": 2,
"quantity": 2
}
],
"discount_code": "SAVE10"
}'
Response (201 Created):
{
"order": {
"id": 2,
"user_id": 1,
"status": "pending",
"subtotal": 1359.97,
"tax": 108.8,
"discount_amount": 135.99,
"total": 1332.78,
"discount_code": "SAVE10",
"created_at": "2024-01-15T14:30:00",
"updated_at": "2024-01-15T14:30:00",
"items": [
{
"id": 2,
"order_id": 2,
"product_id": 1,
"product_name": "Laptop Pro 15",
"quantity": 1,
"unit_price": 1299.99,
"total_price": 1299.99
},
{
"id": 3,
"order_id": 2,
"product_id": 2,
"product_name": "Wireless Mouse",
"quantity": 2,
"unit_price": 29.99,
"total_price": 59.98
}
]
}
}
Error Responses
400 Bad Request - Missing items:
{
"error": "Order must include at least one item"
}
400 Bad Request - Insufficient stock:
{
"error": "Insufficient stock for Laptop Pro 15. Available: 5"
}
401 Unauthorized - Missing or invalid token:
{
"msg": "Missing Authorization Header"
}
404 Not Found - Product not found:
{
"error": "Product 999 not found"
}
Order Processing
When an order is created:
- Validation: The system validates that all products exist and have sufficient stock
- Calculation: Subtotal, tax, and discounts are calculated automatically
- Stock Reduction: Product stock is reduced by the ordered quantity
- Status: Order is created with
pending status
- Payment: Use the Payments API to complete the order
The total is calculated as:
total = subtotal + tax - discount_amount