I want to get product information using API call. I have product id of that product is it possible to do that.
5 Answers 5
To get product information using API call with product id you can use following API with searchCriteria :
GET /V1/products
 {'searchCriteria':{'filterGroups':[{'filters':[{'field':'entity_id','value':"1",'condition_type':'eq'}]}]}}
In searchCriteria add filter with product id as above.
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.
- 
 2We don't need to define new API in this case, we can re-use the already function. See my answer.Khoa Truong– Khoa Truong2017年07月18日 04:10:05 +00:00Commented Jul 18, 2017 at 4:10
 - 
 Lorenzo, can you please explain where should those files to be placed and what should be their names?Milen Mihalev– Milen Mihalev2020年04月16日 13:16:40 +00:00Commented Apr 16, 2020 at 13:16
 
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
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.
- 
 1No i have product id not product sku.Nitin Pawar– Nitin Pawar2017年04月01日 04:58:03 +00:00Commented Apr 1, 2017 at 4:58
 
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.