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,
  "stockQuantity": 123,
  "category": [
    {}
  ]
}
'
{
  "201": {},
  "400": {},
  "500": {},
  "id": 123,
  "name": "<string>",
  "description": "<string>",
  "price": 123,
  "stockQuantity": 123,
  "categories": [
    {
      "id": 123,
      "category": {
        "id": 123,
        "name": "<string>"
      }
    }
  ],
  "createdAt": {},
  "updatedAt": {},
  "_links": {}
}

Overview

This endpoint creates a new product with the specified attributes and category associations. Categories are automatically created if they don’t already exist. The operation is transactional, ensuring data consistency.

Request Body

name
string
required
The name of the product
description
string
required
Detailed description of the product
price
decimal
required
Product price in the base currency (BigDecimal format, e.g., 29.99)
stockQuantity
long
required
Initial stock quantity (must be a positive long integer)
category
array
required
Array of category names (strings). Categories will be created automatically if they don’t exist.

Response

The response includes the created product with generated fields and a Location header pointing to the new resource.
id
long
Auto-generated unique identifier for the product
name
string
Product name as provided in the request
description
string
Product description as provided in the request
price
decimal
Product price
stockQuantity
long
Initial stock quantity
categories
array
List of category associations created for this product
id
long
Product-category association ID
category
object
id
long
Category ID (auto-generated)
name
string
Category name
createdAt
datetime
Timestamp when the product was created (auto-generated, ISO 8601 format)
updatedAt
datetime
Timestamp when the product was last updated (initially same as createdAt)
HATEOAS links for navigation

Status Codes

201
Created
Product created successfully. The Location header contains the URI of the new product.
400
Bad Request
Invalid request body or missing required fields
500
Internal Server Error
Server error occurred while creating the product

Examples

curl -X POST "http://localhost:8080/products" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "name": "Wireless Mouse",
    "description": "Ergonomic wireless mouse with adjustable DPI",
    "price": 29.99,
    "stockQuantity": 150,
    "category": ["Electronics", "Computer Accessories"]
  }'

Request Body Example

{
  "name": "Wireless Mouse",
  "description": "Ergonomic wireless mouse with adjustable DPI settings, 6 programmable buttons, and long battery life up to 18 months",
  "price": 29.99,
  "stockQuantity": 150,
  "category": ["Electronics", "Computer Accessories"]
}

Success Response Example (201 Created)

{
  "id": 42,
  "name": "Wireless Mouse",
  "description": "Ergonomic wireless mouse with adjustable DPI settings, 6 programmable buttons, and long battery life up to 18 months",
  "price": 29.99,
  "stockQuantity": 150,
  "categories": [
    {
      "id": 15,
      "product": null,
      "category": {
        "id": 1,
        "name": "Electronics"
      }
    },
    {
      "id": 16,
      "product": null,
      "category": {
        "id": 3,
        "name": "Computer Accessories"
      }
    }
  ],
  "createdAt": "2026-03-03T10:30:00",
  "updatedAt": "2026-03-03T10:30:00",
  "_links": {
    "self": {
      "href": "http://localhost:8080/products/42"
    },
    "products": {
      "href": "http://localhost:8080/products"
    }
  }
}
Response Headers:
Location: http://localhost:8080/products/42
Content-Type: application/json

Error Response Example (400 Bad Request)

{
  "timestamp": "2026-03-03T10:30:00",
  "status": 400,
  "error": "Bad Request",
  "message": "Validation failed",
  "errors": [
    {
      "field": "price",
      "message": "must not be null"
    }
  ],
  "path": "/products"
}
Transaction Management: This endpoint is annotated with @Transactional, which means if any part of the operation fails (product creation or category association), all changes will be rolled back to maintain database consistency.
Category Auto-Creation: If a category name in the category array doesn’t exist in the database, it will be automatically created. Existing categories are retrieved and reused by name.
All fields in the request body are required. Make sure to validate the data on the client side before sending the request to avoid 400 Bad Request errors.

Build docs developers (and LLMs) love