How To

How to Get Products by ID using REST API in Magento 2

Hello Magento Friends,

In today’s article, I am going to discuss How to Get Products by ID using the REST API in Magento 2.

REST API is used to send requests and receive responses. Developers can get product information using product IDs in Magento 2. 

Related Article – Magento 2: How to Get Order Details using REST API

Learn how to Get Products by ID using REST API in Magento 2

Steps to Get Products by ID using REST API in Magento 2

Step 1: Create the rootscript.php script file and add the below code

<?php
$baseUrl = " http://yourdomain/"; // your magento base url
$userData = array("username" => "admin", "password" => "admin@123");
$ch = curl_init($baseUrl."/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
 
$token = curl_exec($ch);
 
$ch = curl_init($baseUrl."rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=Your_Product_ID&searchCriteria[filterGroups][0][filters][0][condition_type]=eq");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 
$result = curl_exec($ch);
 
$result = json_decode($result, 1);
echo '<pre>';
print_r($result);
echo '</pre>';
?>

Conclusion:

This way you can Get Products by ID using the REST API in Magento 2. If you have any queries let me know in the comment box.

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Hi, I am trying to import products from 1 store to another Magento 2.4 store Using REST API. In the below code have issue to assign category to the product.

    $categories = $item['extension_attributes']['category_links'];
    $catArray = array();

    foreach($categories as $category){
    $category_id = $category['category_id'];

    foreach($localCategories as $lcat){
    if($category_id == $lcat['baseId']){
    array_push($catArray, $lcat['id']);
    }
    }
    }
    $product['categories'] = $catArray;

    The above code leaves the category blank. If I remove foreach($categories as $category), product is assigned to all the categories.

    Any help would be much appreciated. Thanks

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago