Skip to main content

Categories API Endpoints

The Categories API provides a public endpoint to check whether any categories exist in the content system. Categories are sourced from the Git-based CMS content repository and represent the top-level taxonomy for organizing items.

Source: template/app/api/categories/exists/route.ts


Check Categories Existence

Checks whether any categories are available in the system and returns the count.

PropertyValue
MethodGET
Path/api/categories/exists
AuthNone (public)

Query Parameters

ParameterTypeRequiredDefaultDescription
localestringNo"en"Locale code for fetching categories (e.g., en, fr, de)

Response

Status 200 -- Categories existence checked successfully.

{
"exists": true,
"count": 12
}
FieldTypeDescription
existsbooleanWhether any categories exist
countnumberNumber of categories found

Error Handling

On any error, the endpoint returns a 200 response with safe defaults rather than an error status code:

{
"exists": false,
"count": 0
}

This fail-safe behavior ensures that the UI can degrade gracefully when the content system is unavailable.

curl Example

# Check if categories exist (default locale)
curl -s http://localhost:3000/api/categories/exists

# Check categories for French locale
curl -s http://localhost:3000/api/categories/exists?locale=fr

TypeScript Usage

interface CategoriesExistResponse {
exists: boolean;
count: number;
}

async function checkCategoriesExist(locale: string = 'en'): Promise<CategoriesExistResponse> {
const res = await fetch(`/api/categories/exists?locale=${locale}`);
return res.json();
}

// Usage
const { exists, count } = await checkCategoriesExist('en');
if (exists) {
console.log(`Found ${count} categories`);
}

Implementation Notes

  • Categories are fetched from the Git-based CMS via fetchItems() from @/lib/content.
  • The endpoint does not require authentication -- it is designed for use by the public-facing UI to conditionally render category navigation elements.
  • Errors are only logged in development mode (NODE_ENV === 'development').
  • The locale parameter maps to the lang option in the content fetch layer.