Overview
This endpoint creates a new category with a name, description, and image. The slug is automatically generated from the category name. This is an admin-only endpoint.
Authentication
This endpoint requires admin API key authentication via the x-api-key header.
Admin API key for authentication. Must match the configured ADMIN_API_KEY environment variable.
Request Body
This endpoint accepts multipart/form-data for file upload.
The name of the category. Must be unique across all categories.
A detailed description of the category.
Category image file. Uploaded via multipart/form-data as a single file.
Response
Returns a success response with the newly created category object.
Response status. Returns “OK” for successful requests.
Human-readable message describing the result.
Array containing the newly created category object. Auto-generated unique identifier for the category.
Display name of the category.
Detailed description of the category.
Auto-generated URL-friendly slug. Created by converting the name to lowercase, replacing spaces with hyphens, converting ”&” to “and”, and removing special characters.
Path to the uploaded category image file.
Example Request
curl --request POST \
--url https://api.example.com/api/admin/category \
--header 'x-api-key: your_admin_api_key_here' \
--form 'name=Electronics' \
--form 'description=Electronic devices and accessories' \
--form 'image=@/path/to/category-image.jpg'
Example Response
201 - Created
400 - Missing Fields
400 - Missing Image
409 - Duplicate Category
403 - Invalid Admin Key
500 - Server Error
{
"status" : "OK" ,
"message" : "Category added successfully" ,
"data" : [
{
"id" : 1 ,
"name" : "Electronics" ,
"description" : "Electronic devices and accessories" ,
"slug" : "electronics" ,
"img_url" : "uploads/categories/electronics-1234567890.jpg"
}
]
}
Error Responses
Error status. Returns “ERROR” for failed requests.
Error message describing what went wrong.
Additional error data if available.
Validation Rules
name : Required, must be unique
description : Required
image : Required, must be a valid file
Slug Generation
The slug is automatically generated from the category name using the following rules:
Convert to lowercase
Replace spaces with hyphens
Replace ”&” with “and”
Remove all special characters except alphanumeric and hyphens
Example: “Home & Garden” → “home-and-garden”
Implementation Details
This endpoint is implemented in controllers/categoryController.mjs:4 using the addCategory function. It:
Validates required fields (name, description, image)
Checks for duplicate category names
Generates a URL-friendly slug
Uploads the image file
Inserts the new category into the database
Returns the created category data
The endpoint uses multer middleware for handling file uploads with the field name “image”.