I want to get product information using API call. I have product id of that product is it possible to do that.


To get product information using API call with product id you can use following API with searchCriteria :

GET /V1/products



In searchCriteria add filter with product id as above.


You need to define a new API.

So, let's define a new interface:


namespace XXX\Productby\Api;

interface ProductByInterface
     * GET product by its ID
     * @api
     * @param string $id
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
    public function getProductById($id);

The definition is similar to the core API, and it throws the same exception.

Now, let's define a model to implement this:

namespace XXX\Productby\Model;

use XXX\Productby\Api\ProductByInterface;

class ProductBy implements ProductByInterface

     * @var \Magento\Catalog\Api\ProductRepositoryInterface
    protected $productRepository;

    public function __construct(
       \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
        $this->productRepository = $productRepository;

     * {@inheritdoc}
    public function getProductById($id)
        return $this->productRepository->getById($id);

Then, simply use your webapi.xml file to use this method when accessing a given route, adding something like:

<route url="/V1/xxx-productbyid/:id" method="GET">
    <service class="XXX\ProductBy\Api\ProductByInterface" method="getProductById"/>
        <resource ref="anonymous"/>

and to bind the model to the interface in the di.xml file:

<preference for="XXX\Productby\Api\ProductByInterface"

Now you can access your product through APIs with a simple GET call:

GET /V1/xxx-productbyid/12345

Source: I had to already do this and even accessing the product with its URL key. Blog article here.

  • 1
    We don't need to define new API in this case, we can re-use the already function. See my answer. – Khoa TruongDinh Jul 18 '17 at 4:10

Do you mean SKU when you say you have product ID?

If so, yes, you can call https://domain.com/rest/V1/products/:sku where :sku is the product SKU.

You can see a full list of APIs here.

  • 1
    No i have product id not product sku. – Nitin Pawar Apr 1 '17 at 4:58

Take a look: \Magento\Catalog\Api\ProductRepositoryInterface

public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);

As we can see, Magento already provides the function to get product by Id. We can re-use it.

Create a new module, declare the new api url:


<?xml version="1.0"?>

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <route url="/V1/products/id/:productId" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="getById"/>
            <resource ref="Magento_Catalog::products" />

Now, we can get: http://magesitd.loc/rest/V1/products/id/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.