Skip to main content
POST
/
orders
curl -X POST http://localhost:3000/orders \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {
        "productId": "507f1f77bcf86cd799439011",
        "quantity": 2,
        "customName": "Special Edition Product"
      },
      {
        "productId": "507f191e810c19729de860ea",
        "quantity": 1
      }
    ]
  }'
{
  "message": "✅ Orden creada con éxito",
  "order": {
    "_id": "65abc123def456789012345",
    "products": [
      {
        "product": "507f1f77bcf86cd799439011",
        "quantity": 2,
        "customName": "Special Edition Product"
      },
      {
        "product": "507f191e810c19729de860ea",
        "quantity": 1,
        "customName": null
      }
    ],
    "total": 149.99,
    "createdAt": "2024-01-15T10:30:00.000Z",
    "updatedAt": "2024-01-15T10:30:00.000Z"
  }
}
Creates a new order with one or more products. This endpoint automatically deducts the specified quantities from product stock.

Authentication

This endpoint requires authentication. Include a valid JWT token in the Authorization header.

Request Body

items
array
required
Array of order items to purchase
productId
string
required
MongoDB ObjectId of the product to order
quantity
number
required
Quantity to order (minimum: 1)
customName
string
Optional custom name for the product in this order

Response

message
string
Success message confirming order creation
order
object
The created order object
_id
string
Unique order identifier
products
array
Array of ordered products
product
string
Product ID reference
quantity
number
Quantity ordered
customName
string
Custom product name if provided
total
number
Total price of the order
createdAt
string
Timestamp when order was created
updatedAt
string
Timestamp when order was last updated
When an order is created, the system automatically deducts the ordered quantities from the stock of each product. If any product has insufficient stock, the entire order fails and no stock is deducted.

Error Responses

  • 400 Bad Request: Empty order or insufficient stock for one or more products
  • 401 Unauthorized: Missing or invalid authentication token
  • 500 Internal Server Error: Server error during order processing
curl -X POST http://localhost:3000/orders \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {
        "productId": "507f1f77bcf86cd799439011",
        "quantity": 2,
        "customName": "Special Edition Product"
      },
      {
        "productId": "507f191e810c19729de860ea",
        "quantity": 1
      }
    ]
  }'
{
  "message": "✅ Orden creada con éxito",
  "order": {
    "_id": "65abc123def456789012345",
    "products": [
      {
        "product": "507f1f77bcf86cd799439011",
        "quantity": 2,
        "customName": "Special Edition Product"
      },
      {
        "product": "507f191e810c19729de860ea",
        "quantity": 1,
        "customName": null
      }
    ],
    "total": 149.99,
    "createdAt": "2024-01-15T10:30:00.000Z",
    "updatedAt": "2024-01-15T10:30:00.000Z"
  }
}