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.
Authorization: Bearer <jwt_token>
Content-Type: application/json
Body Parameters
Detailed description of the product
Product price. Must be a positive number (>= 0)
Product category. Must be one of: digital, physical, service
Available stock quantity. Defaults to 0 if not provided
Image URL for the product. Defaults to null if not provided
Response
The created product objectUnique product identifier
Image URL for the product
ID of the admin user who created the product
Whether the product is active (default: true)
ISO 8601 timestamp of creation
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"
}