Overview
The Categories API provides endpoints for managing product categories, including CRUD operations and active category filtering. Base URL:/api/categorias
Source: CategoriaController.java
Authentication
- Public endpoints: GET requests (list, view)
- Admin endpoints: POST, PUT, DELETE operations (no explicit
@PreAuthorizein source, but should be admin-only in production)
Endpoints
List All Categories
200 OK - Array of category objects
List Active Categories
activo = true).
Authentication: None (public endpoint)
Response: 200 OK - Array of active category objects
Example Request:
cURL
Get Category by ID
Category ID
200 OK- Category object404 Not Found- Category does not exist
cURL
Create Category
ROLE_ADMIN (not explicitly enforced in source)
Request Body:
Category name
Category description
Active status (default: true)
200 OK - Created category object with generated ID
Example Request:
cURL
Update Category
ROLE_ADMIN
Category ID to update
200 OK- Updated category object404 Not Found- Category does not exist
cURL
Delete Category
ROLE_ADMIN
Category ID to delete
200 OK- Category successfully deleted400 Bad Request- Cannot delete (may have associated products)
cURL
Category Model
The complete category object structure:Integration Examples
Frontend: Display Category Navigation
JavaScript
Admin: Create New Category
JavaScript
Filter Products by Category
JavaScript
Use Cases
Navigation Menu
Use
/activos endpoint to populate the category navigation menu on the storefront.Product Filtering
Combine with Products API to filter products by category.
Admin Management
Create, update, and organize product categories from the admin panel.
Inventory Organization
Organize products into logical groups for easier inventory management.
Best Practices
Use Active/Inactive Instead of Deleting
Use Active/Inactive Instead of Deleting
Instead of permanently deleting categories, toggle the
activo flag to false. This preserves data integrity and allows you to restore categories later.Check for Associated Products
Check for Associated Products
Before deleting a category, verify that no products are assigned to it. Implement cascade rules or reassign products to a different category.
SEO-Friendly URLs
SEO-Friendly URLs
Consider adding a
slug field (similar to products) for SEO-friendly category URLs like /categories/electronics instead of /categories/1.Error Responses
| Status Code | Description |
|---|---|
400 Bad Request | Cannot delete category with associated products |
401 Unauthorized | Missing or invalid JWT token |
403 Forbidden | User does not have admin role |
404 Not Found | Category does not exist |
500 Internal Server Error | Server error |
Related Endpoints
Products API
Manage products within categories
Products by Category
Filter products by category ID