get an array of all categories with details in Magento

get an array of all categories with details in Magento

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
get an array of all categories with details in Magento

Retrieving All Categories via Magento API

As a senior developer working on an e-commerce platform, I often need to interact with the database and APIs to fetch data for various purposes, including SEO optimization and data migration. One common requirement is retrieving all category details through the API.

Recently, I was tasked with fetching an array of all categories along with their details like URL key using Magento’s REST API. The goal was to achieve a structure similar to this:

$massage_cats = array(
    array("entity_id" => 78,
          "name" => "Massage Oils and Tools",
          "url_key" => "massage-oils-and-tools",
          "url_path" => "essential-accessories/massage-oils-and-tools.html"),
    array("entity_id" => 79,
          "name" => "Massage Oils",
          "url_key" => "massage-oils",
          "url_path" => "essential-accessories/massage-oils-and-tools/massage-oils.html")
);

To accomplish this, I initially attempted to use a made-up API call:

$massage_cats = array();
$allcats = Mage::getModel('catalog/cats')->loadAll();
foreach($allcats as $k => $item) {
    array_push($massage_cats, $item->loadDetails());
}

However, this approach is not valid for the Magento API. To retrieve all categories with details via the REST API, you can use the following steps:

  1. First, enable the API and set up a consumer key and secret in your Magento admin panel.

  2. Make an HTTP GET request to the rest/V1/categories endpoint using the appropriate authentication headers.

Here’s an example of how you can make this API call using cURL in PHP:

$url = "http://your_magento_store/rest/V1/categories";
$headers = array("Authorization: Bearer your_access_token");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

// Process the data
foreach ($data['items'] as $category) {
    echo "ID: " . $category['entity_id'] . PHP_EOL;
    echo "Name: " . $category['name'] . PHP_EOL;
    echo "URL Key: " . $category['url_key'] . PHP_EOL;
    echo "URL Path: " . $category['url_path'] . PHP_EOL;
}

Remember to replace your_magento_store with your actual Magento store URL and your_access_token with the token obtained from the admin panel.

This code will fetch all categories along with their details like entity ID, name, URL key, and URL path. You can then process this data as needed for your specific use case.

Conclusion

Retrieving category details via the Magento REST API is a common requirement in e-commerce development. By following the steps outlined above and using the provided code example as a starting point, you should be able to fetch an array of all categories with their details easily.

For more information on working with Magento APIs and best practices for data retrieval, I recommend checking out the official Magento API documentation: Magento REST API Overview

Additionally, if you’re looking to optimize your e-commerce platform’s performance and security, consider exploring our services at Laravel Company. We offer expert Magento development, troubleshooting, and optimization solutions tailored to meet your business needs.

Don’t hesitate to reach out for any further assistance or questions regarding your specific project.

Catalog Database Performance API

Our network