Skip to main content

Collection API Endpoints

The Collections API provides a public endpoint for checking whether active collections exist in the system. Collections are stored in the database and managed through the collection repository layer.

Source file: template/app/api/collections/exists/route.ts

Endpoint Summary

MethodPathAuthDescription
GET/api/collections/existsNoneCheck if active collections exist

GET /api/collections/exists

Checks whether any active collections are available. Returns a boolean exists flag along with the count of active collections. This is a public endpoint primarily used by the frontend to decide whether to render collection-related UI elements.

Query Parameters

None.

How It Works

The handler uses the collectionRepository to fetch all active collections, then checks if the result is a non-empty array:

const collections = await collectionRepository.findAll({
includeInactive: false
});

const hasCollections =
Array.isArray(collections) && collections.length > 0;

return NextResponse.json({
exists: hasCollections,
count: collections?.length || 0
});

Response Shape

200 -- Collections Found

{
"exists": true,
"count": 5
}

200 -- No Collections

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

500 -- Server Error

On failure, the endpoint returns a 500 status with a generic error message. Detailed error information is logged server-side only:

{
"exists": false,
"count": 0,
"error": "Failed to check collections existence"
}

Authentication

This is a public endpoint -- no authentication is required.

Usage Example

// Check if collections exist before rendering collection section
const res = await fetch('/api/collections/exists');
const data = await res.json();

if (data.exists) {
console.log(`${data.count} active collections available`);
// Render collection navigation
}

Differences from Categories Endpoint

AspectCategoriesCollections
Data sourceGit-based CMS contentDatabase via repository layer
Error behaviorReturns 200 with exists: falseReturns 500 with error message
Filter supportLocale parameterActive-only filter (hardcoded)
Requires databaseNoYes

Notes

  • Only active collections are counted. Inactive collections are excluded by the includeInactive: false filter.
  • Detailed errors are logged server-side and never exposed to the client (to avoid information disclosure).
  • The endpoint requires a working database connection since collections are database-backed.
FilePurpose
template/app/api/collections/exists/route.tsRoute handler
template/lib/repositories/collection.repository.tsCollection data access layer