Extend current API's in Magento to get items in JSON format

Extend current API's in Magento to get items in JSON format

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Extend current API's in Magento to get items in JSON format

Extending Magento APIs to Retrieve Data in JSON Format

As a beginner in Magento, you’re right on track wanting to extend the existing API classes to fulfill your specific data retrieval needs. In this post, I’ll guide you through extending the current APIs to get all stores in a website, categories and subcategories for a store, and products within a category—all in JSON format.

Understanding Magento’s REST API

Magento provides built-in REST APIs that allow external systems to communicate with your Magento installation. By default, these APIs are disabled; you’ll need to enable them first. To do this:

  1. Log in to the Magento admin panel.
  2. Navigate to Stores > Settings > Configuration > Services > Magento REST API.
  3. Set ‘API Enabled’ and ‘Webservices URL’ fields as per your requirements.

Once enabled, you can access the APIs using standard HTTP methods (GET, POST, etc.) with data in JSON or XML format.

Getting All Stores in a Website

First, let’s retrieve all stores within a specific website. Here’s how:

$url = "http://your_magento_url/index.php/rest/V1/stores";
$headers = array("Content-Type: application/json", "Authorization: Bearer $token");
$response = file_get_contents($url, false, stream_context_create(["http" => ["header" => implode("\r\n", $headers)]]));

Replace your_magento_url with your Magento URL and $token with a valid token obtained through Magento’s REST API authentication process.

The response will be in JSON format containing an array of stores for the specified website. The structure looks like this:

[
  {
    "store_id": 1,
    "code": "main_website",
    "name": "Main Website Store"
  },
  ...
]

Getting Categories and Subcategories

Next, let’s fetch all categories (and their subcategories) for a specific store. Here’s how:

$url = "http://your_magento_url/index.php/rest/V1/categories";
$headers = array("Content-Type: application/json", "Authorization: Bearer $token");
$response = file_get_contents($url, false, stream_context_create(["http" => ["header" => implode("\r\n", $headers)]]));

The response will be a JSON array of categories. Each category object includes properties like category_id, name, and an array of children containing its subcategories.

Getting Products in a Specific Category

Finally, let’s retrieve all products within a specific category:

$url = "http://your_magento_url/index.php/rest/V1/categories/2/products";
$headers = array("Content-Type: application/json", "Authorization: Bearer $token");
$response = file_get_contents($url, false, stream_context_create(["http" => ["header" => implode("\r\n", $headers)]]));

Replace 2 with the ID of your desired category.

The response will be a JSON array of product objects. Each object contains properties like sku, name, price, and other relevant product details.

Conclusion

In this post, we’ve extended Magento’s built-in REST APIs to retrieve data in JSON format—something crucial for building scalable eCommerce applications.

Remember, the key is to understand your specific needs first; then map those requirements to Magento’s API endpoints. If you’re looking to build more complex APIs or need assistance with authentication and access control, I highly recommend checking out our comprehensive guide on Magento 2 REST API Development.

As a beginner, practice with these examples and gradually take on more complex tasks. With time and patience, you’ll become proficient in extending Magento’s APIs to meet your unique eCommerce needs.

Happy coding!

Catalog API Admin Configuration

Our network