When I send GET request to categories.json, I get subcategoryIds of Ids for any category but how do I get the corresponding names of subcategories using the API.
Basically I want the name and id of all the categories/subcategories.
In a support discussion titled ‘Subcategory Name,’ users seek to retrieve subcategory names alongside their IDs via the Discourse API. Minhaj initially notes that GET /categories.json returns subcategoryIds but lacks corresponding names. osioke suggests consulting a reverse-engineering guide, but Minhaj finds it unhelpful. Sören_Geier echoes the confusion, questioning whether internal mechanisms like create-pretender resolve names magically, and shares a screenshot highlighting the issue. Eventually, Sören_Geier answers their own question: subcategory names are available in /sites.json, which lists all categories. By cross-referencing subcategoryIds from /categories.json with entries in /sites.json, one can retrieve names. They speculate that /sites.json alone might suffice if it includes flags to distinguish parent/main categories — though untested. The goal is to build a customizable list of categories and subcategories using custom markup. Sören_Geier implies this approach may eliminate the need for multiple API calls.
When I send GET request to categories.json, I get subcategoryIds of Ids for any category but how do I get the corresponding names of subcategories using the API.
Basically I want the name and id of all the categories/subcategories.
You may find this post useful Reverse engineer the Discourse API
I am unable to find the corresponding names of subcategories. I have tried to parse the full response of GET request on /categories.json and I don’t see any field which gives me subcategory Name.
thanks for the reply. looking at the reverse engineer guide doesn’t really help me to be honest as I think some magic is going on here.
would you be able to pinpoint how the subcategory names are rendered from that JSON response? I am also not seeing any relevant subsequent calls that could potentially resolve those ids to actual names. The goal is to create a list of categories and subcategories which I can then position freely with custom markup. any help is appreciated.
I wonder if discourse/app/assets/javascripts/discourse/tests/helpers/create-pretender.js at 928f1025166681105084a46ee733bfb08ca8d5e4 · discourse/discourse · GitHub create-pretenders are in play here to magically resolve names?
answering my own question here…
all categories of a site are listed in /sites.json. so you can use /catgories_and_latest for them main level categories and when you get across the subcategories ids for each main category you pick the subcategory name from the /sites.json response.
I assume there are some props in sites.json that identify a category as main or parent category so in the end /sites.json might be sufficient (one request). haven’t tested yet.