# 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 juli 2017 om 08:48 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:** 1\
**Showing post:** 7

<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 juli 2017 om 11:42 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)

---

_[View the full topic](https://meta.discourse.org/t/how-to-get-a-category-name-with-the-id-via-api/65926)._
