Magento 2 API: Get Categories

In this article, we will be looking into how we can get the Categories in Magento 2 using its API.

  • Get All Categories
  • Get Category by category id
  • Search Categories

Anonymouse APIs Restriction

The REST endpoints to get the stores’ information falls under the anonymous APIs category. However, in the recent Magento version, those API endpoints are restricted. They need “admin” or “integration” token to authenticate.

If you need/want to bypass the token authentication for those APIs, you can disable this feature by going to:

Magento Admin > STORES > Settings > Configuration > Services > Magento Web API > Web API Security > Allow Anonymous Guest Access = Yes.

NOTE:

  • We will be passing the admin bearer token to authenticate the APIs.
  • This article shows how we can get the admin token in Magento: Magento 2 API: Get Admin Token

Get All Categories

API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/categories#operation/GetV1Categories

Endpoint:


GET <host>/rest/V1/categories

Authentication Bearer Token:


<admin-token>

Response:


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

Get Categories using cURL Request


curl --location --request GET 'https://app.your-project.test/rest/V1/categories' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'

Get Category by category id

API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/categoriescategoryId#operation/GetV1CategoriesCategoryId

Endpoint:


GET <host>/rest/V1/categories/{categoryId}

Authentication Bearer Token:


<admin-token>

Response:


{
    "id": 38,
    "parent_id": 2,
    "name": "What's New",
    "is_active": true,
    "position": 1,
    "level": 2,
    "children": "",
    "created_at": "2022-11-25 15:45:22",
    "updated_at": "2022-11-25 15:45:22",
    "path": "1/2/38",
    "include_in_menu": true,
    "custom_attributes": [
        {
            "attribute_code": "display_mode",
            "value": "PAGE"
        },
        {
            "attribute_code": "is_anchor",
            "value": "0"
        },
        {
            "attribute_code": "path",
            "value": "1/2/38"
        },
        {
            "attribute_code": "children_count",
            "value": "0"
        },
        {
            "attribute_code": "url_key",
            "value": "what-is-new"
        },
        {
            "attribute_code": "url_path",
            "value": "what-is-new"
        }
    ]
}

Get Category by category id using cURL Request


curl --location --request GET 'https://app.your-project.test/rest/V1/categories/38' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'

Search Categories

API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/categorieslist

Endpoint:


GET <host>/rest/V1/categories/list

Params:

In the example below, we are searching for name like “%new%” and we are limiting the result count to 3.

SearchCriteria Doc: https://developer.adobe.com/commerce/webapi/rest/use-rest/performing-searches/


searchCriteria[filterGroups][0][filters][0][conditionType].   like
searchCriteria[filterGroups][0][filters][0][field]            name
searchCriteria[filterGroups][0][filters][0][value]            %new%
searchCriteria[pageSize]                                      3

Authentication Bearer Token:


<admin-token>

Response:


{
    "items": [
        {
            "id": 8,
            "parent_id": 7,
            "name": "New Luma Yoga Collection",
            "is_active": true,
            "position": 1,
            "level": 3,
            "children": "",
            "created_at": "2022-11-24 23:51:56",
            "updated_at": "2022-11-24 23:51:56",
            "path": "1/2/7/8",
            "include_in_menu": false,
            ...
            ...
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "name",
                        "value": "%new%",
                        "condition_type": "like"
                    }
                ]
            }
        ],
        "page_size": 3
    },
    "total_count": 4
}

Get Category by category id using cURL Request


curl --location --request GET 'https://app.your-project.test/rest/V1/categories/list?searchCriteria[pageSize]=3&searchCriteria[filterGroups][0][filters][0][conditionType]=like&searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][value]=%new%' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'

Hope this helps. Thanks.

Magento REST API Reference: https://developer.adobe.com/commerce/webapi/rest/quick-reference/