In a discussion titled ‘How to get a category name with the ID via API’ under the Dev category, users explore how to retrieve category names and slugs using the Discourse API. anon89120937 seeks a way to get subcategory details (name/slug) from IDs using the PHP Discourse API client. michaeld references an old thread suggesting /site.json as the solution, citing riking. anon89120937 asks if this is implemented in the PHP client; michaeld confirms it isn’t but offers to add it. anon89120937 shares a custom getCategoryById function using /site.json, which riking advises caching for efficiency. Later, anon89120937 updates with a more efficient approach: fetching /site.json once and parsing all categories and subcategories into a structured array, referencing their GitHub implementation. nedbat notes the lack of documentation for site.json and asks if there’s a better method. simon confirms /site.json is the correct approach, especially for subcategories, and clarifies that authenticated requests are needed to include private categories. He also acknowledges the docs are outdated — categories.json doesn’t return all categories as claimed — and commits to fixing the documentation.
I use Discourse API client for PHP (by @michaeld I guess) and I can’t find how to get a category name/slug with only with an ID.
I need it to display (slug and id) of all our categories and their sub-categories.
But when I get the categories list, the only information about sub-categories is their id.
site.json seems to be the way to get a list of all categories. Is it mentioned somewhere in docs.discourse.org? I was scouring the docs looking for a way to get the names of subcategories and couldn’t find it. Or is there a better way?
Making an API request to site.json is the correct way to get a list of categories that includes subcategories. Also, if you want private categories to be returned by the request to site.json, you need to make an authenticated API request.
I’m not seeing any reference to site.json in the docs. SInce categories.json doesn’t return subcategories, the description of the categories route in the docs isn’t correct, it says that the route returns all categories. I will fix that.