So,

you are using the Magento2 APIs and you need to access a product.
The core API forces you to access product by its SKU:

GET /V1/products/:sku

Sometimes you need to access a product by its ID, or by its URL key.

Let’s see how we can create two simple APIs to perform this task.

First of all, let’s fill your webapi.xml with the two routes we are going to use for this.

Ex.:

<route url="/V1/lorenzo-productbyurl/:urlKey" method="GET">
 <service class="Lorenzo\ProductBy\Api\ProductByInterface" method="getProductByUrl"/>
 <resources>
   <resource ref="anonymous"/>
 </resources>
</route>
<route url="/V1/lorenzo-productbyid/:id" method="GET">
 <service class="Lorenzo\ProductBy\Api\ProductByInterface" method="getProductById"/>
 <resources>
   <resource ref="anonymous"/>
 </resources>
</route>

Now we are going to define the interface cited in the previous XML file, declaring the two methods:

interface ProductByInterface
{
 /**
  * GET product identified by its URL key
  *
  * @api
  * @param string $urlKey
  * @return \Magento\Catalog\Api\Data\ProductInterface
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 public function getProductByUrl($urlKey);

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

 

Ok, finally we are going to define the model that will implement this interface. Please be sure to update your di.xml accordingly.

 

class ProductBy implements ProductByInterface
{

 /**
  * @var \Magento\Framework\Api\SearchCriteriaBuilder
  */
 protected $searchCriteriaBuilder;

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

 /**
  * @var \Magento\Framework\Api\FilterBuilder
  */
 protected $filterBuilder;

 /**
  * @var \Magento\Framework\Api\Search\FilterGroup
  */
 protected $filterGroup;

 public function __construct(
   \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
   \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria,
   \Magento\Framework\Api\FilterBuilder $filterBuilder,
   \Magento\Framework\Api\Search\FilterGroup $filterGroup)
 {
   $this->productRepository = $productRepository;
   $this->searchCriteria = $searchCriteria;
   $this->filterBuilder = $filterBuilder;
   $this->filterGroup = $filterGroup;
 }

 /**
  * {@inheritdoc}
  */
 public function getProductByUrl($urlKey)
 {
   $this->filterGroup->setFilters([
   $this->filterBuilder->setField('url_key')->setConditionType('eq')
        ->setValue($urlKey)->create()]);
   $this->searchCriteria->setFilterGroups([$this->filterGroup]);
   $products = $this->productRepository->getList($this->searchCriteria);
   if (count($products) == 0) {
     return null;
   }
   $items = $products->getItems();
   foreach ($items as $item) {
     return $item;
   }
 }

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

Now you can access a product by ID with:

GET /V1/lorenzo-productbyid/:id

And by URL key with:

GET /V1/lorenzo-productbyurl/:urlKey