Skip to main content

POST /api/products

Creates a new product in the system. This endpoint requires admin authentication.

Authentication

Required. Must include a valid JWT token in the Authorization header.

Authorization

Admin role required. The authenticated user’s ID will be stored as createdBy.

Request Headers

Authorization: Bearer <jwt_token>
Content-Type: application/json

Body Parameters

name
string
required
Product name
description
string
required
Detailed description of the product
price
number
required
Product price. Must be a positive number (>= 0)
category
string
required
Product category. Must be one of: digital, physical, service
stock
number
Available stock quantity. Defaults to 0 if not provided
image
string
Image URL for the product. Defaults to null if not provided

Response

message
string
Success message
product
object
The created product object
_id
string
Unique product identifier
name
string
Product name
description
string
Product description
price
number
Product price
stock
number
Available stock quantity
category
string
Product category
image
string
Image URL for the product
createdBy
string
ID of the admin user who created the product
isActive
boolean
Whether the product is active (default: true)
createdAt
string
ISO 8601 timestamp of creation
updatedAt
string
ISO 8601 timestamp of last update

Example Request

curl -X POST https://api.example.com/api/products \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Ethereum Staking Guide",
    "description": "Complete guide to Ethereum staking",
    "price": 79.99,
    "category": "digital",
    "stock": 200,
    "image": "https://example.com/images/eth-guide.jpg"
  }'

Example Response

{
  "message": "Product created successfully",
  "product": {
    "_id": "507f1f77bcf86cd799439013",
    "name": "Ethereum Staking Guide",
    "description": "Complete guide to Ethereum staking",
    "price": 79.99,
    "stock": 200,
    "category": "digital",
    "image": "https://example.com/images/eth-guide.jpg",
    "createdBy": "507f1f77bcf86cd799439000",
    "isActive": true,
    "createdAt": "2026-03-04T12:00:00.000Z",
    "updatedAt": "2026-03-04T12:00:00.000Z"
  }
}

Error Responses

400 - Bad Request

Missing required fields:
{
  "error": "All fields are required"
}
Invalid price:
{
  "error": "Price must be positive"
}

401 - Unauthorized

{
  "error": "Authentication required"
}

403 - Forbidden

{
  "error": "Admin access required"
}

500 - Internal Server Error

{
  "error": "Database connection failed"
}

Build docs developers (and LLMs) love