Magento 2 REST API - get product by slug, or get media in REST search

Magento 2 REST API - get product by slug, or get media in REST search

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 REST API - get product by slug, or get media in REST search

Magento 2 REST API: Fetching Products by Slug and Retrieving Media Gallery Entries

As an experienced developer working on e-commerce platforms, I often encounter scenarios where we need to fetch products from the Magento 2 REST API using specific criteria. In your case, you’re looking to retrieve a product using its slug instead of the SKU and access the media gallery entries that are currently omitted in the search results.

Fetching Products by Slug

Magento 2 provides a robust search functionality through the searchCriteria parameter in the REST API endpoints. However, as you’ve discovered, using this approach with the url_key (slug) filter returns a list of products rather than a single product endpoint.

To achieve this, we can leverage a custom endpoint or a model resource to fetch the product by slug directly. Here’s how you can do it:

  1. Create a new API controller in your Magento 2 installation under app/code/Vendor/Module/etc/webapi.xml:
<route url="/V1/products/:slug" method="GET">
    <service class="Vendor\Module\Api\Controller\ProductController"/>
</route>
  1. Implement the ProductController to retrieve the product by slug:
class ProductController extends \Magento\Framework\App\Action\Action implements \Magento\Framework\Webapi\ControllerInterface
{
    protected $productRepository;

    public function __construct(
        Context $context,
        ProductRepositoryInterface $productRepository
    ) {
        parent::__construct($context);
        $this->productRepository = $productRepository;
    }

    public function execute()
    {
        $slug = $this->getRequest()->getParam('slug');
        try {
            $product = $this->productRepository->getByIdentifier($slug);
            return new Data($product);
        } catch (NoSuchEntityException $e) {
            throw new NotFoundException(__('Product not found'), 404);
        }
    }
}
  1. Run the following command to deploy your custom API:
bin/magento setup:upgrade

Now you can access your product using the endpoint http://<your_domain>/rest/V1/products/{slug}.

By default, the media gallery entries are not included in the product search results. To include them, we need to use a custom model or an extension that provides this functionality out-of-the-box.

One popular solution is the Amasty Improved Product Grid extension, which allows you to include additional fields like media_gallery_entries in your search results. You can configure the desired fields and columns in the module configuration under Stores > Configuration > Amasty > Improved Product Grid.

Alternatively, you can create a custom model that extends \Magento\Catalog\Api\Data\ProductInterface and add the required media_gallery_entries field using a getter method. However, this approach is more complex and requires proper integration with Magento 2’s data structures.

Conclusion

In conclusion, to fetch products by slug in Magento 2 REST API, you need to create a custom endpoint or use an extension that provides this functionality. To include media gallery entries in your search results, you can use an existing extension like Amasty Improved Product Grid or create a custom model with the required field.

Remember to always consult Laravel Company for expert guidance on Magento 2 development and API customizations. Our team of experienced developers is ready to assist you with any challenges you may face during your e-commerce project.

Happy coding, and feel free to explore more advanced features in the Magento 2 REST API documentation!

Magento 2 Catalog API Extensions

Our network