Magento API example

Magento API example

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento API example

Building Magento API-Enabled Modules: A Step-by-Step Guide

As a seasoned developer, I understand your need for a practical example of an API-enabled module in Magento. This powerful e-commerce platform allows you to create custom APIs that extend its functionality and integrate with external systems seamlessly.

Defining the API Module

Before we dive into the code, let’s define what our API module will do. For this example, I’ll create a simple module named MyCompany_MyModule that provides an API endpoint for retrieving all enabled products.

Configuring the Module

First, register your module in the app/etc/modules/MyCompany_MyModule.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:module/etc/module.xsd">
    <module name="MyCompany_MyModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Creating the API Endpoint

Next, create a new file named ProductController.php in your module’s controller directory (app/code/MyCompany/MyModule/Controller/API/). This is where we’ll define our API endpoint:

<?php
declare(strict_types=1);

namespace MyCompany\MyModule\Controller\API;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;

class Product implements HttpPostActionInterface
{
    private $resultFactory;

    public function __construct(PageFactory $resultFactory)
    {
        $this->resultFactory = $resultFactory;
    }

    public function execute()
    {
        return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData([
            'success' => true,
            'products' => [
                ['id' => 1, 'name' => 'Product 1'],
                ['id' => 2, 'name' => 'Product 2'],
            ],
        ]);
    }
}

This controller will return a JSON response containing an array of products when accessed via the API.

Configuring the Routing

Finally, add your API route to your module’s routes.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="mycompany_mymodule_api_product" frontName="api" args="-s product">
            <module name="MyCompany_MyModule"/>
        </route>
    </router>
</config>

Calling the API

With the setup complete, you can now call your API endpoint at http://your-magento-instance/api/product/. The response should look like this:

{
  "success": true,
  "products": [
    {
      "id": 1,
      "name": "Product 1"
    },
    {
      "id": 2,
      "name": "Product 2"
    }
  ]
}

Conclusion

Creating API-enabled modules in Magento is a powerful way to extend the platform’s functionality. By following this example, you can build custom APIs tailored to your specific business needs.

Remember that while this example uses a simple data structure, real-world applications would likely retrieve data from the database using Magento’s repository patterns or other data access layers.

For more advanced Magento API development techniques and best practices, I recommend checking out our comprehensive guide on Magento 2 API development at Laravel Company. It provides an in-depth look at working with the platform’s SOAP and REST APIs.

Don’t hesitate to reach out if you have any questions or need further guidance on Magento development!

Catalog API Extensions Themes

Our network