Magento – Magento 2 – Best approach for retrieving product data – REST API or Custom Endpoint

magento2productsrest api

I would like to know what you think the best approach is for retrieving product data from an external source in Magento 2

I have tried two options and both work but I would like to know what you think is the best method:

Method 1 – Custom Endpoint

Using a controller I can make a GET request to:
http://domain.com/modulename/product/index And pass the SKU as a Param in the request from an external source

Then my controller will call my helper class and return a JSON array of product data:

public function execute()
{
    /**
     *  We would normally create some basic Auth check before instatiating the getProduct() method
     *  The Auth token would be stored in the admin config of the module using system.xml
     */
    return $this->resultJsonFactory->create()->setData($this->helper->getProduct());
}

And my Helper

  public function getProduct()
    {
        // This is just for the test I am going to load a configurable product then get the child products
        // Normally the loop would check but this is only for the test.
        $product = $this->products->get('MH01');
        // This is the parent product name and concatenated the SKU
        $productData['name'] = $product->getName() . ' ' . $product->getSku();
        // Get the child products
        $childIds = $this->configurable->getChildrenIds($product->getId());
        $childData = [];
        foreach ($childIds[0] as $childId) {
            $simple = $this->products->getById($childId);
            $childData[] = [
                'name' => $simple->getName(),
                'sku' => $simple->getSku()
            ];
        }
        return ['parent' => $productData, 'children' => $childData];
    }

The other approach would be to use the Magento REST API Webservice method

Method 2 – REST API

I set the route via webapi.xml then make a GET request to:
http://domain.com/rest/V1/module-name/products/{$id}

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="GET" url="/V1/costa-test/products/:id">
        <service class="Costa\Test\Api\ProductsManagementInterface" method="getProducts"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

The ProductsManagementInterface Class will take the $id and pass it

interface ProductsManagementInterface
{
    /**
     * GET for products api
     * @param string $id
     * @return string
     */
    public function getProducts($id);
}

Then in my ProductsManagement Class

public function getProducts($id)
{
    // This is just for the test I am going to load a configurable product then get the child products
    // Normally the loop would check but this is only for the test.
    $product = $this->products->getById($id);
    // This is the parent product name and concatenated the SKU
    $productData['name'] = $product->getName() . ' ' . $product->getSku();
    // Get the child products
    $childIds = $this->configurable->getChildrenIds($product->getId());
    $childData = [];
    /** @var $childIds array */
    foreach ($childIds[0] as $childId) {
        $simple = $this->products->getById($childId);
        $childData[] = [
            'name' => $simple->getName(),
            'sku' => $simple->getSku(),
        ];
    }
    return ['parent' => $productData, 'children' => $childData];
}

Both approaches work well but what would you say is the best method?

Best Answer

I recommend using the Rest API.

  • If the default Magento APIs can meet your requirements, should use it. We don’t need to spend money and time on creating the new one.
  • Security: APIs mostly need the security. Magento tried to do it for you.
Related Topic