When we create a mobile application using Magento 2 eCommerce then we use rest API to get the catalog product image URL.
This post will give you an idea about how you can get the product image and product URL from the REST API in Magento 2.
As we know without images we can give a greater user experience to our customers on any e-commerce website. Images give clarity about the product so customers get engaged with stores and sales increase.
Get Catalog Product Image REST API
This is a step-by-step code to get the product image URLs in Magento 2 eCommerce store.
This is useful in all kinds of situations like if you are developing a mobile application for your Magento 2 store then you need the product images so in order to show the product image you need to retrieve the image URL.
Step 1: Registration of ProductImageApi
first, we have to register our first API by rergistration.php file so we have to create the file in the below location.
Wishusucess/ProductImageApi/registration.php
<?php /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_ProductImageApi', __DIR__ );
Step 2: Create a Module XML file
Wishusucess/ProductImageApi/etc/module.xml
<?xml version="1.0"?> <!-- /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Wishusucess_ProductImageApi" setup_version="1.0.2" /> </config>
Step 3: Declare Dependency Injection
Wishusucess/ProductImageApi/etc/di.xml
<?xml version="1.0"?> <!-- /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Wishusucess\ProductImageApi\Api\GetProductImage" type="Wishusucess\ProductImageApi\Model\GetProductImageUrl" /> </config>
Step 4: Create Web API Route Path
Wishusucess/ProductImageApi/etc/webapi.xml
<?xml version="1.0"?> <!-- /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ --> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route url="/V1/getproductimage/:sku" method="GET"> <service class="Wishusucess\ProductImageApi\Api\GetProductImage" method="getProductImageUrl"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Step 5: Get Catalog Product Image API Model Class
Wishusucess/ProductImageApi/Model/GetProductImageUrl.php
This model class has the actual login, so we have to write the following code in order to get the catalog product image URL.
<?php /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ namespace Wishusucess\ProductImageApi\Model; use Wishusucess\ProductImageApi\Api\GetProductImage; class GetProductImageUrl implements GetProductImage { /** * @var \Magento\Store\Model\App\Emulation */ protected $appEmulation; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @var \Magento\Catalog\Api\ProductRepositoryInterface */ protected $productRepository; /** * @var \Magento\Catalog\Helper\Image */ protected $imageHelper; /** * @param \Magento\Store\Model\App\Emulation $appEmulation * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository * @param \Magento\Catalog\Helper\Image $imageHelper */ public function __construct( \Magento\Store\Model\App\Emulation $appEmulation, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Helper\Image $imageHelper ) { $this->appEmulation = $appEmulation; $this->storeManager = $storeManager; $this->productRepository = $productRepository; $this->imageHelper = $imageHelper; } public function getProductImageUrl($sku) { $storeId = $this->storeManager->getStore()->getId(); $product = $this->productRepository->get($sku); $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true); if (!$product) { $response = [ [ "code" => '301', "message" => "SKU " . $productSku . " Not Found On Magento", ], ]; return $response; } else { $image_url = $this->imageHelper->init($product, 'product_base_image')->getUrl(); $response = [ [ "product_image_url" => $image_url, ], ]; return $response; } $this->appEmulation->stopEnvironmentEmulation(); } }
Step 6: Images URL API Class
Wishusucess/ProductImageApi/Api/GetProductPage.php
<?php /** * Category: Wishusucess_ProductImageApi * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ namespace Wishusucess\ProductImageApi\Api; interface GetProductImage { /** * @api * @param string $sku * @return array */ public function getProductImageUrl($sku); }
That's it.
Now, we execute the following command.
php bin/magento s:up php bin/magento s:s:d -f php bin/magento c:c
Now you can test your product images rest API on the postman by hitting the web route URL.
Now you can see the above images, here
The method is POST and the route URL path for the rest of API is your store URL address and the method and the product that you want the images path details.
https://www.your_store_url.com/rest/V1/products/sku
or
https://www.your_store_url.com/rest/V1/getproductimage/sku
Online Help & Support for Magento 2 REST API
If you need any kind of help and support in Magento 2 rest API then you can call me.
I am always available to support you while extension development REST API development or any help related to Magento 2 stores.
I am giving you the helpline details in the below images.
Similar Post:
Search REST API: How to Find Product in Magento 2 Using REST API
Payment API: How to Make Payment Through REST API in Magento 2