Skip to main content
POST
/
api
/
admin
/
category
Create Category
curl --request POST \
  --url https://api.example.com/api/admin/category \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <x-api-key>' \
  --data '
{
  "name": "<string>",
  "description": "<string>"
}
'
{
  "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"
    }
  ]
}

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.
x-api-key
string
required
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.
name
string
required
The name of the category. Must be unique across all categories.
description
string
required
A detailed description of the category.
image
file
required
Category image file. Uploaded via multipart/form-data as a single file.

Response

Returns a success response with the newly created category object.
status
string
Response status. Returns “OK” for successful requests.
message
string
Human-readable message describing the result.
data
array
Array containing the newly created category object.

Example Request

cURL
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

{
  "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

status
string
Error status. Returns “ERROR” for failed requests.
message
string
Error message describing what went wrong.
data
any
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:
  1. Convert to lowercase
  2. Replace spaces with hyphens
  3. Replace ”&” with “and”
  4. 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:
  1. Validates required fields (name, description, image)
  2. Checks for duplicate category names
  3. Generates a URL-friendly slug
  4. Uploads the image file
  5. Inserts the new category into the database
  6. Returns the created category data
The endpoint uses multer middleware for handling file uploads with the field name “image”.

Build docs developers (and LLMs) love