How to filter by category in Magento 2's API?

How to filter by category in Magento 2's API?

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to filter by category in Magento 2's API?

Filtering Products by Category Using Magento 2’s REST API

In Magento 2’s powerful REST API, you can leverage various search criteria to filter products based on different attributes, including categories. While the official documentation provides extensive examples for searching by fields like name and description, it may not explicitly cover filtering by category. In this article, we’ll walk through a step-by-step guide to achieve this.

Understanding Magento 2’s REST API Structure

Before diving into the code, let’s briefly recap the structure of a typical REST API request in Magento 2:

http://magentohost/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=FIELD_NAME&
    searchCriteria[filter_groups][0][filters][0][value]=VALUE&
    searchCriteria[filter_groups][0][filters][0][condition_type]=CONDITION_TYPE

Here’s what each part represents:

  • http://magentohost/rest/V1/products: The base URL for the products endpoint.
  • searchCriteria: This is a nested object containing all the filter criteria.
  • filter_groups[0]: Defines a group of filters. You can add multiple groups ([0], [1], etc.) to combine filters using logical operators like AND or OR.
  • filters[0][field], value, and condition_type: These define each individual filter within the group.

Filtering by Category

To filter products by category, we’ll use the category_id field in combination with a condition type that suits our needs. In this case, we’ll use the in condition type to match multiple categories simultaneously:

http://magentohost/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&
    searchCriteria[filterGroups][0][Filters][0][Value]=1,2,3&
    searchCriteria[filterGroups][0][Filters][0][Condition_type]=in

In this example:

  • Replace http://magentohost with the URL of your Magento 2 instance.
  • The category IDs 1, 2, and 3 are placeholders. You should replace these with the actual category IDs you want to filter by.
  • The in condition type allows matching products assigned to any of the specified categories.

Advanced Filtering: Combining Category Filters

You can also combine multiple category filters within a single request, either using an AND operator (filter_groups[0]) or an OR operator (by adding additional groups like [1], [2], etc.):

http://magentohost/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id&
    searchCriteria[FilterGroups][0][Filters][0][Value]=1,2&
    searchCriteria[FilterGroups][0][Filters][0][Condition_type]=in&

    searchCriteria[FilterGroups][1][Filters][0][Field]=category_id&
    searchCriteria[FilterGroups][1][Filters][0][Value]=3,4&
    searchCriteria[FilterGroups][1][Filters][0][Condition_type]=in

In this advanced example, we’re filtering products that belong to any combination of categories 1, 2 and 3, 4. The two filter groups are combined using an OR operator.

Conclusion

The Magento 2 REST API provides a powerful and flexible framework for searching and filtering products based on various criteria, including category. By understanding the structure of searchCriteria objects and leveraging condition types like in, you can efficiently retrieve products that match specific category assignments.

Remember to consult the official Magento 2 REST API documentation for more information on available endpoints and search criteria options.

At Laravel Company, we specialize in helping businesses maximize the potential of their Magento 2 platforms through expert development services and comprehensive training programs. Contact us today to learn how we can help streamline your e-commerce operations using Magento’s powerful APIs.

Magento 2 Catalog API

Our network