Back to Documentation
API Reference
Get All Categories
Retrieve a list of all available categories for organizing your blog posts.
GET
client.getCategories()import { LightweightClient } from 'lightweight-client';
async function getCategories() {
const key = process.env.LIGHTWEIGHT_API_KEY;
if (!key) throw Error('LIGHTWEIGHT_API_KEY environment variable must be set. You can use the DEMO key a8c58738-7b98-4597-b20a-0bb1c2fe5772 for testing - please set it in the root .env.local file');
const client = new LightweightClient(key);
return client.getCategories();
}Parameters
This endpoint does not require any parameters.
Response
[
{
"slug": string, // URL-friendly category identifier
"title": string // Category display name
},
{
"slug": string,
"title": string
},
// ... more category objects
]Response Schema
Returns an array of category objects, each containing:
slugstringURL-friendly identifier for the category. Used in API calls and routing.
titlestringHuman-readable display name for the category.
Usage Example
Here's how you might use the categories to filter posts:
// Get all categories
const categories = await client.getCategories();
// Display categories in a navigation menu
categories.forEach(category => {
console.log(`<a href="/blog/${category.slug}">${category.title}</a>`);
});
// Filter posts by category slug
const techPosts = await client.getPostsByCategory('technology');Notes
- •Categories are predefined in your CMS and cannot be created via the API
- •The
slugfield is used for URL routing and API filtering - •Categories are returned in alphabetical order by default
- •Use the DEMO API key
a8c58738-7b98-4597-b20a-0bb1c2fe5772for testing