How To Get Categories Through Magento 2 REST API?

how to get categories through magento 2 api

In this blog post, we will show you how to get the categories from a Magento website by using the Magento REST API. Therefore, you can use a tool to access the API. Here we are using Postman. And to get the category list, first, we need to get the access token of the admin user: Endpoint: “http(s)://yourdomain.com/rest/V1/integration/admin/token” Method: POST

Request:

{
	"username" : "string",
	"password" : "string"
}

Response: “token” (string)

For example:

magento 2 api tutorial

Magento system will check whether that username is available or not, then API will return the access token as a string.

For example:

If your username and password are not correct, the API will return errors like the following:

magento rest api get category

Now, we have the access token. Let’s begin to get the categories data from API. Endpoint: “http(s)://yourdomain.com/rest/ V1/categories/list” Method: GET Request: searchCriteria. Header: Authorization : Bearer (Token) Response: token(string)

For example:

If the access token is correct, the API will return the Customer’s information. For example:

Rest API magento 2 get categories

If the access token is incorrect, the API will return errors as below:

Magento 2 doesn’t support get all categories but they allow us to get a specific category that contains children category. Endpoint: “http(s)://yourdomain.com/graphql” Query structure:

category (
   id: int
): CategoryTree

Request example:

{
  category(id: 20) {
    products {
      total_count
      page_info {
        current_page
        page_size
      }
    }
    children_count
    children {
      id
      level
      name
      path
      children {
        id
        level
        name
        path
        children {
          id
          level
          name
          path
          children {
            id
            level
            name
            path
          }
        }
      }
    }
  }
}

Response example:

{
  "data": {
    "category": {
      "products": {
        "total_count": 0,
        "page_info": {
          "current_page": 1,
          "page_size": 20
        }
      },
      "children_count": "8",
      "children": [
        {
          "id": 21,
          "level": 3,
          "name": "Tops",
          "path": "1/2/20/21",
          "children": []
        },
        {
          "id": 22,
          "level": 3,
          "name": "Bottoms",
          "path": "1/2/20/22",
          "children": [ 
            {
              "id": 27,
              "level": 4,
              "name": "Pants",
              "path": "1/2/20/22/27",
              "children": []
            },
            {
              "id": 28,
              "level": 4,
              "name": "Shorts",
              "path": "1/2/20/22/28",
              "children": []
            }
          ]
        }
      ]
    }
  }
}

Now let’s write down the endpoint and your request:

graphql magento 2 get category

And then click run to send the request to the server. API will return a response which has data like below:

magento graphql tutorial

We have given you some easy steps to get the categories via API. If you have any issues when following the guide, just leave a comment below. See you in the next tutorial!

Related Posts:

How To Get A Product By SKU Using GraphQL In Magento 2

How To Create API Request In Magento 2?

How To Get Product List (Catalog) Through Magento 2 API?

How To Create A Credit Memo In Magento 2 In 10 minutes

How To Set Up Automatic Product Redirects in Magento 2?

How To Customize Magento 2 Checkout Shipping Address Form

How To Get All Products Through Magento 2 API