How To Get Category List By REST API and GraphQL In Magento 2, E.g.

In this article, I will explain how you can Get Categories List By REST API in Magento 2 default categories REST API, and also we will get the same data in Magento 2 GraphQL.  It's basically used nowadays in native application development using Magento 2.

When we develop an e-commerce mobile application in android or ReactJS using Magento 2 shopping framework then usually we get categories data and other data of the Magento store using REST API or GraphQL.

Therefore, here I will explain how we can get the categories data using Magento 2 REST API and GraphQL.

We will use two tools to get the data from Magento 2 store.

  • Postman for REST API
  • Altair for GraphQL

 

Get Categories List By REST API

In Magento 2 whenever we try to get some data then we need to create the token first then that token we have to send with the request so Magento 2 store will verify the tokens and validate that request.

So here we are trying to get all categories data so we have to get the admin token first by using admin user.

Endpoint:

https://wishusucess.com/rest/all/V1/integration/admin/token

Note: In the above endpoint, you have to replace the domain wishusucess.com with your domain name.

Now Send Request by using the below syntax in JSON format:

{

"username": "Admin",

"password": "Admin@77#123"

}

so if everything will be correct then you will get the response below format.

So here we have received an admin access token by using an admin user.

Create Admin Token Using REST API

Now we will use this admin token for authorization and get the categories data so we have to send the token with the request so that token will be validated by the Magento 2 store and if the token will get validated then you will get the response something like below format.

Get Categories List By REST API and GraphQL

So, in the above image, you can see that the admin access token has been validated and the server sent the details of all the categories list.

Endpoint:

https://wishusucess.com/rest/V1/categories

Get Categories List By REST API

In case of any token invalidation, the server will respond to you like you are not a valid user and you can't access the resources.

Invalid Access Token Message

 

Get Categories List By GraphQL in Magento 2

In order to get the categories list using Magento 2 Graphql, we have to send the request in query format and then we have to specify the Graphql API in the URL section.

So here I have defined a root category in the query which is a default category in my store so all the subcategories of that particular id will show in the result.

https://wishusucess.com/graphql

Now I have submitted the query with the following details in the request then I received a response like the below image.

{
category(id: 2) {
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
}
}
}
}
}
}

This is the response to the Magento 2 Category list data query in Magento 2 using the Altair Graphql tool.

Magento 2 Get Categories List by GraphQL

 

Related Post:

Sign Up Rest API: How to Call Magento 2 Customer Sign Up Rest API, E.g.

Magento 2 Customer API: Create Custom API For Customer

 

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing