# How to get a category name with the ID via API

**URL:** https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926
**Category:** Development
**Created:** [10 ביולי,‏ 2017,‏ 8:48am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926 "2017-07-10T08:48:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon89120937](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@anon89120937](https://meta.discourse.org/u/anon89120937)
#### Post date: [10 ביולי,‏ 2017,‏ 8:48am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/1 "2017-07-10T08:48:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [10 ביולי,‏ 2017,‏ 9:10am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/2 "2017-07-10T09:10:38Z")

</div>

I asked the same question a long time ago,  
[https://meta.discourse.org/t/how-to-get-subcategory-names-using-api/29795](https://meta.discourse.org/t/how-to-get-subcategory-names-using-api/29795)

> [@riking](#):
>
> Use /site.json for this.

---

<div class="post-metadata">

### Author: ![anon89120937](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@anon89120937](https://meta.discourse.org/u/anon89120937)
#### Post date: [10 ביולי,‏ 2017,‏ 9:13am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/3 "2017-07-10T09:13:41Z")

</div>

Thanks, is it implemented in your php client ?

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [10 ביולי,‏ 2017,‏ 10:17am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/4 "2017-07-10T10:17:13Z")

</div>

No, we never implemented that. Should be fairly easy to do, I can take a look later today.

---

<div class="post-metadata">

### Author: ![anon89120937](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@anon89120937](https://meta.discourse.org/u/anon89120937)
#### Post date: [10 ביולי,‏ 2017,‏ 1:29pm UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/5 "2017-07-10T13:29:14Z")

</div>

```
function getCategoryById($categoryId) {
       $datas = $this->_getRequest("/site.json");
       $categories = $datas->apiresult->categories;
       foreach($categories as $categorie) {
         if(($categorie->id) == $categoryId) {
           return $categorie;
         }
       }

```

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [14 ביולי,‏ 2017,‏ 3:14am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/6 "2017-07-14T03:14:23Z")

</div>

You should probably save the data in a global, so you can call the function multiple times in the same PHP script.

---

<div class="post-metadata">

### Author: ![anon89120937](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@anon89120937](https://meta.discourse.org/u/anon89120937)
#### Post date: [14 ביולי,‏ 2017,‏ 11:42am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/7 "2017-07-14T11:42:13Z")

</div>

I’d rather use /site.json once (to limit the API’s calls). We’ve added the following function to DiscourseApi.php :

```
function getSite()
    {
        return $this->_getRequest("/site.json");
    }

```

and then use it to get wanted informations :

```
  $categories = $discourseApi->getSite()->apiresult->categories;
  foreach ($categories as $category) {
    if (isset($category->parent_category_id)){
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['id'] = $category->id;
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['slug'] = $category->slug;
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['name'] = $category->name;
    } else {
      $categories_all[$category->id]['id'] = $category->id;
      $categories_all[$category->id]['slug'] = $category->slug;
      $categories_all[$category->id]['name'] = $category->name;
    }
  }

```

(code from [congressus/application/config/discourse.structure.php at master · PartiPirate/congressus · GitHub](https://github.com/PartiPirate/congressus/blob/master/application/config/discourse.structure.php))

(We don’t use the getCategoryById() I’ve suggested before)

---

<div class="post-metadata">

### Author: ![nedbat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nedbat/32/150103_2.png) [@nedbat](https://meta.discourse.org/u/nedbat)
#### Post date: [16 במרץ,‏ 2020,‏ 10:22pm UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/8 "2020-03-16T22:22:21Z")

</div>

site.json seems to be the way to get a list of all categories. Is it mentioned somewhere in [docs.discourse.org](http://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?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [17 במרץ,‏ 2020,‏ 12:14am UTC](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926/9 "2020-03-17T00:14:27Z")

</div>

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.

> [@nedbat](#):
>
> I was scouring the docs looking for a way to get the names of subcategories and couldn’t find it.

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.
