6

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

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Mar 31, 2017 at 6:13

5 Answers 5

13

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

GET /V1/products

http://domain.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=5325

 {'searchCriteria':{'filterGroups':[{'filters':[{'field':'entity_id','value':"1",'condition_type':'eq'}]}]}}

In searchCriteria add filter with product id as above.

answered Jun 21, 2017 at 9:48
4

You need to define a new API.

So, let's define a new interface:

<?php
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"/>
 <resources>
 <resource ref="anonymous"/>
 </resources>
</route>

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

<preference for="XXX\Productby\Api\ProductByInterface"
 type="Wow\Productby\Model\ProductBy"/>

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.

answered Jun 20, 2017 at 10:49
2
  • 2
    We don't need to define new API in this case, we can re-use the already function. See my answer. Commented Jul 18, 2017 at 4:10
  • Lorenzo, can you please explain where should those files to be placed and what should be their names? Commented Apr 16, 2020 at 13:16
4

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:

app/code/Vendor/CatalogProduct/etc/webapi.xml

<?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 url="/V1/products/id/:productId" method="GET">
 <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="getById"/>
 <resources>
 <resource ref="Magento_Catalog::products" />
 </resources>
 </route>
</routes>

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

answered Jul 18, 2017 at 4:08
3

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.

answered Mar 31, 2017 at 7:22
1
  • 1
    No i have product id not product sku. Commented Apr 1, 2017 at 4:58
3

You can add the filter

http://www.mysite.co/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=1&searchCriteria[filterGroups][0][filters][0][condition_type]=eq

Use field entity_id instead of id

value is Product Id here 1

condition_type is equal to here eq.

answered May 4, 2020 at 7:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.