Skip to main content

Endpoint

/database/categorias.php

Get All Categories

Retrieve all product categories ordered by ID (newest first).
curl -X GET https://your-domain.com/database/categorias.php

Response

{
  "success": true,
  "data": [
    {
      "id": 3,
      "nombre": "Electrónica",
      "descripcion": "Productos electrónicos y tecnología",
      "color": "#2e6df6",
      "estado": "ACTIVO",
      "fecha_creacion": "2026-03-01 10:30:00",
      "fecha_actualizacion": null
    },
    {
      "id": 2,
      "nombre": "Ropa",
      "descripcion": "Prendas de vestir",
      "color": "#ff6b6b",
      "estado": "ACTIVO",
      "fecha_creacion": "2026-02-28 14:20:00",
      "fecha_actualizacion": null
    }
  ]
}

Response Fields

id
integer
Unique category identifier
nombre
string
Category name (max 50 characters)
descripcion
string
Category description (max 200 characters)
color
string
Hex color code for UI display (e.g., “#2e6df6”)
estado
string
Category status (e.g., “ACTIVO”, “INACTIVO”)
fecha_creacion
datetime
Creation timestamp
fecha_actualizacion
datetime
Last update timestamp (null if never updated)

Create Category

Create a new product category.
curl -X POST https://your-domain.com/database/categorias.php \
  -H "Content-Type: application/json" \
  -d '{
    "nombre": "Electrónica",
    "descripcion": "Productos electrónicos y tecnología",
    "color": "#2e6df6",
    "estado": "ACTIVO"
  }'

Request Parameters

nombre
string
required
Category name (max 50 characters, must be unique)
descripcion
string
Category description (max 200 characters)
color
string
default:"#2e6df6"
Hex color code for UI display
estado
string
default:"ACTIVO"
Category status (e.g., “ACTIVO”, “INACTIVO”)

Validation Rules

  • nombre: Required, maximum 50 characters, must be unique
  • descripcion: Optional, maximum 200 characters
  • color: Optional, defaults to #2e6df6
  • estado: Optional, defaults to ACTIVO

Response

{
  "success": true,
  "id": 5,
  "message": "Categoría creada exitosamente"
}

Update Category

Update an existing category’s information.
curl -X PUT https://your-domain.com/database/categorias.php \
  -H "Content-Type: application/json" \
  -d '{
    "id": 5,
    "nombre": "Electrónica Actualizada",
    "descripcion": "Nueva descripción",
    "color": "#4a90e2",
    "estado": "ACTIVO"
  }'

Request Parameters

id
integer
required
Category ID to update (must be greater than 0)
nombre
string
required
New category name (max 50 characters)
descripcion
string
New category description (max 200 characters)
color
string
default:"#2e6df6"
New color code
estado
string
default:"ACTIVO"
New category status

Validation Rules

  • id: Required, must be greater than 0, category must exist
  • nombre: Required, max 50 characters, must be unique (excluding current category)
  • descripcion: Optional, max 200 characters
  • Updates fecha_actualizacion timestamp automatically

Response

{
  "success": true,
  "message": "Categoría actualizada exitosamente"
}

Delete Category

Delete a category from the system.
curl -X DELETE https://your-domain.com/database/categorias.php \
  -H "Content-Type: application/json" \
  -d '{"id": 5}'

Request Parameters

id
integer
required
Category ID to delete (must be greater than 0)

Validation Rules

  • id: Required, must be greater than 0, category must exist
  • Cannot delete if category has associated products
  • Checks productos table for foreign key relationships
  • Supports multiple column name patterns: categoria_id, id_categoria, categoria, categoriaId

Response

{
  "success": true,
  "message": "Categoría eliminada exitosamente"
}
Deleting a category is prevented if it has associated products. You must reassign or delete the products first.

Advanced Foreign Key Detection

The delete operation includes intelligent foreign key column detection:

Database Schema

Categories are stored in the categorias table:
CREATE TABLE categorias (
  id INT AUTO_INCREMENT PRIMARY KEY,
  nombre VARCHAR(50) NOT NULL UNIQUE,
  descripcion VARCHAR(200),
  color VARCHAR(7) DEFAULT '#2e6df6',
  estado VARCHAR(20) DEFAULT 'ACTIVO',
  fecha_creacion DATETIME,
  fecha_actualizacion DATETIME
);

Error Codes

Error MessageCauseSolution
Datos inválidosInvalid JSONCheck JSON syntax
El nombre es obligatorioMissing nombreInclude nombre in request
El nombre no puede superar 50 caracteresName too longShorten category name
La descripción no puede superar 200 caracteresDescription too longShorten description
Ya existe una categoría con este nombreDuplicate nameUse different name
ID inválidoInvalid IDProvide valid ID > 0
Categoría no encontradaCategory doesn’t existCheck category ID
No se puede eliminar. Tiene X producto(s) asociado(s)Has productsRemove products first

Build docs developers (and LLMs) love