Skip to main content
POST
/
products
Create Product
curl --request POST \
  --url https://api.example.com/products \
  --header 'Content-Type: application/json' \
  --data '
{
  "name": "<string>",
  "description": "<string>",
  "price": 123,
  "stock": 123,
  "categoryId": 123
}
'
{
  "id": 123,
  "name": "<string>",
  "description": "<string>",
  "price": 123,
  "imageUrl": "<string>",
  "stock": 123,
  "categoryId": 123,
  "createdAt": {},
  "updatedAt": {}
}

Overview

This endpoint allows administrators to create a new product in the catalog. It requires admin authentication and a product image upload.

Endpoint

POST /products

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.
name
string
required
Product name
description
string
required
Product description
price
number
required
Product price (maximum 2 decimal places)
stock
integer
required
Available stock quantity (minimum: 0)
categoryId
integer
ID of the category this product belongs to (must be >= 1 if provided)
image
file
required
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.
id
integer
Unique identifier for the newly created product
name
string
Product name
description
string
Product description
price
decimal
Product price (decimal with 2 decimal places)
imageUrl
string
URL to the uploaded product image
stock
integer
Available stock quantity
categoryId
integer
ID of the category this product belongs to
createdAt
datetime
Timestamp when the product was created
updatedAt
datetime
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

Build docs developers (and LLMs) love