magento 2 rest api : get cart items with images

magento 2 rest api : get cart items with images

August 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento 2 rest api : get cart items with images

Displaying Product Images via Magento 2 REST API

In your current setup, you’re successfully retrieving cart items using the /rest/V1/guest-carts/{cart_id}/items endpoint in the Magento 2 REST API. This endpoint provides a list of products in the cart along with essential details such as item_id, sku, name, and price. However, it currently lacks the product images you’re looking for.

To include product images in your response, we need to modify our approach by leveraging the product media gallery functionality provided by Magento. Here’s a step-by-step guide on how to achieve this:

Step 1: Retrieve Product Data

First, let’s fetch detailed product information using the /rest/V1/products/{sku} endpoint. This will give us access to the product’s media gallery.

GET /rest/V1/products/abc-1

The response looks something like this:

{
  "id": 3,
  "sku": "abc-1",
  "name": "Product one",
  "price": 19.00,
  "short_description": "<p>Short description of product one.</p>\n",
  "description": "<p>Long description of product one with images and formatting.</p>\n",
  "weight": 2.387,
  "categories": [...],
  "stock_data": {
    "is_in_stock": true
  },
  "media_gallery_entries": [
    {
      "file": "product_1.jpg",
      "label": "Product one image",
      "position": null,
      "disabled": false
    }
  ],
  "url_key": "product-one"
}

Step 2: Extract Image Information

In the media_gallery_entries field, we find the image files associated with the product. The file attribute contains the image file name (including extensions), which you can use to construct a URL for the image.

To get the full image URL, you’ll need to prepend the base media URL to this file name. Typically, the base URL is something like http://your_magento_site.com/media/catalog/product. You can usually find or configure this within your Magento system.

Step 3: Integrate Images with Cart Items

Now that we have the image information for each product in our cart, we need to integrate it into our existing API response. One way to do this is by adding a new field called image_url to the cart item objects:

[
{
  "item_id": 100,
  "sku": "abc-1",
  "qty": 1,
  "name": "Product one",
  "price": 19.00,
  "product_type": "simple",
  "quote_id": "e3e1fd447e0e315dd761942cf949ce5d",
  "image_url": "http://your_magento_site.com/media/catalog/product/abc-1.jpg"
},
{
  "item_id": 101,
  "sku": "abc-2",
  "qty": 1,
  "name": "Product two",
  "price": 54.00,
  "product_type": "simple",
  "quote_id": "e3e1fd447e0e315dd761942cf949ce5d",
  "image_url": "http://your_magento_site.com/media/catalog/product/abc-2.jpg"
}
]

Step 4: Optimizing Performance

While this approach works, it requires multiple API calls (one for each product), which can be inefficient for large carts. A more efficient method would involve creating a custom REST API endpoint that retrieves both the cart items and their corresponding images in a single call.

This is where Magento’s ability to create custom APIs shine. Using Magento 2’s API controller, you can write a custom API action that handles both operations simultaneously.

For more details on how to create custom APIs in Magento 2, I recommend checking out this guide from our developers at Laravel Company.

Conclusion

In conclusion, while the default cart items API doesn’t include images, we can achieve this by using a combination of the product details API and some manual image URL construction. However, for larger-scale applications, creating a custom API endpoint that fetches both data sources simultaneously would be the recommended approach.

Remember to always validate and sanitize your inputs when handling data in your APIs to ensure the security of your application.

Happy coding!

Magento 2 Checkout Catalog API

Our network