Magento 2 REST API – Returns Only Default Category and Children

categorycategory-treemagento-2.1magento2rest

I am using "rest/V1/categories/" (Magento 2 Rest API). In documentation(devdocs.magento.com/swagger/#) it says "Retrieve list of categories" but it only returns default category(and its all children) every time. How can i get the list of all categories(those which are parent including default)
enter image description here

This is how i am doing it,

public function getCatagory($data){
        $response = array();
        if(isset($data['categoryId'])){
            $url = $this->baseUrl."rest/V1/categories/".$data['categoryId'];
            $response = $this->curlGet($url,$this->token);
        }else{
            $url = $this->baseUrl."rest/V1/categories/";
            $response = $this->curlGet($url,$this->token);
        }
        return $response;
    }

I am executing else part here "rest/V1/categories/" and i am getting,

{
  "id": 2,
  "parent_id": 1,
  "name": "Default Category",
  "is_active": true,
  "position": 1,
  "level": 1,
  "product_count": 0,
  "children_data": [

  ]
}

It only giving me default why are the other categories missing as shown in the attached image of admin panel. Please help!! Thanks i advance(I don't want to put all my categories under default as children, i know its a way out, but i have already made each of them a parent as they are shown in the images attached now i just want to fetch them).

Best Answer

Well you can easily get all your categories by passing "all" keyword in your categories get API URL

It will give you all the categories including all your root categories.

Try below Steps:

Note : Make sure you pass all keyword in Url and it will works