Latest news

How To Get Categories Through Magento 2 REST API?

No comments

Today, we will show you how to get the categories from a Magento website by using the Magento REST API.

You can use a tool to access the API. Here we are using Postman.

To get 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 which 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 Create API Request In Magento 2?

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

How To Speed Up Magento Websites: AMP or PWA?

Magento 2 PWA Studio (Venia) Vs. Tigren’s Magento 2 PWA Theme

How To Use Stripe Connect With Vendor Account In Magento 2?

 

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 5.00 out of 5)
Loading...
April LeeHow To Get Categories Through Magento 2 REST API?

Leave a Reply

Your email address will not be published. Required fields are marked *