Overview
This endpoint allows administrators to create a new product in the catalog. It requires admin authentication and a product image upload.
Endpoint
Authentication
Required: Admin role
This endpoint requires a valid JWT token with admin privileges. Include the token in the Authorization header:
Authorization: Bearer <your_token>
Request Body
This endpoint accepts multipart/form-data format due to the required image upload.
Product price (maximum 2 decimal places)
Available stock quantity (minimum: 0)
ID of the category this product belongs to (must be >= 1 if provided)
Product image file (uploaded as multipart/form-data with field name ‘image’)
Validation Rules
name: Must be a valid string
description: Must be a valid string
price: Must be a number with maximum 2 decimal places
stock: Must be an integer with minimum value of 0
categoryId: If provided, must be an integer >= 1
image: Required file field - the endpoint will return a 400 error if missing
Response
Returns the created product object.
Unique identifier for the newly created product
Product price (decimal with 2 decimal places)
URL to the uploaded product image
ID of the category this product belongs to
Timestamp when the product was created
Timestamp when the product was last updated
Example Request
curl -X POST https://api.example.com/products \
-H "Authorization: Bearer <your_admin_token>" \
-F "name=Laptop" \
-F "description=High-performance laptop for professionals" \
-F "price=1299.99" \
-F "stock=15" \
-F "categoryId=2" \
-F "image=@/path/to/laptop.jpg"
Example Response
{
"id": 1,
"name": "Laptop",
"description": "High-performance laptop for professionals",
"price": "1299.99",
"imageUrl": "https://example.com/uploads/laptop-1234567890.jpg",
"stock": 15,
"categoryId": 2,
"createdAt": "2026-03-06T10:30:00.000Z",
"updatedAt": "2026-03-06T10:30:00.000Z"
}
Status Code: 201 Created
Error Responses
Missing Image
{
"error": "La imagen del producto es obligatoria (campo 'image')"
}
Status Code: 400 Bad Request
Validation Error
{
"error": "Validation failed",
"details": [
{
"field": "price",
"message": "El precio debe ser un número válido con máximo 2 decimales"
}
]
}
Status Code: 400 Bad Request
Unauthorized
{
"error": "Unauthorized"
}
Status Code: 401 Unauthorized
Forbidden (Non-Admin User)
{
"error": "Admin access required"
}
Status Code: 403 Forbidden