Display categories and their subcategories in Magento using REST API

Display categories and their subcategories in Magento using REST API

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display categories and their subcategories in Magento using REST API

Displaying Categories and Subcategories Using Magento REST API

As a seasoned developer who has worked extensively with e-commerce platforms like Magento, I understand your need to fetch category information via the REST API. Let’s dive into how you can achieve this.

Understanding the Magento REST API Structure

The Magento 2 REST API provides a comprehensive way to interact with various data entities in your store, including categories and products. The API endpoints typically follow this pattern:

/api/rest/<entity>/<id>

Where <entity> represents the type of resource you want to access (like categories, products, etc.), and <id> is the specific identifier for that resource.

Fetching All Categories

You’re on the right track with your second URL attempt. To fetch all categories, you can use the following endpoint:

GET http://localhost/magento/api/rest/categories

This will return a JSON response containing an array of category objects. Each object has properties like category_id (the unique identifier for the category), name, description, and more.

Including Subcategories

To include subcategories within the main categories, you can use Magento’s nested category structure. However, this isn’t directly available through the REST API by default.

To achieve this, you would need to create a custom module that extends the REST API with an additional endpoint or modify the existing categoryRepository to return the data in a desired format.

Fetching Category Tree Structure

If you just want to fetch all categories and their subcategories in a tree-like structure, you can use Magento’s built-in getTreeHtml function. This is available via the frontend controller and can be accessed like this:

GET http://localhost/magento/rest/V1/categories/tree/

This will return a JSON array of category nodes with their respective child nodes.

Conclusion

Fetching categories and their subcategories in Magento using the REST API requires a combination of understanding the API structure, creating custom modules for extended functionality, or utilizing built-in functions available through frontend controllers.

Remember, working with the REST API in Magento often involves a good understanding of how data is structured and accessed. If you find yourself needing more complex operations or data manipulation, consider checking out our comprehensive guides on working with Magento 2 APIs.

Don’t forget to thoroughly test your API calls in a development environment before deploying them to production to ensure they handle edge cases and errors gracefully.

Happy coding!

Magento 2 Catalog API Extensions

Our network