{"id":2562,"date":"2024-12-31T20:23:00","date_gmt":"2024-12-31T20:23:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/magento-2-api-searchcriteria\/"},"modified":"2025-04-09T17:42:55","modified_gmt":"2025-04-09T12:12:55","slug":"magento-2-api-searchcriteria","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-api-searchcriteria\/","title":{"rendered":"Magento 2 API &#8211; SearchCriteria"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello, Magento peeps!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am here with another&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>&nbsp;tutorial on SearchCriteria. In my last blog post, I provided a complete step-wise guide to getting a product by SKU using Magento 2 REST API. In case you missed it, you can read it at&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-product-by-sku\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API \u2013 Get Product by SKU<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SearchCriteria in Magento 2 API is useful to add the search\/filter functionality when developing a third-party application based on Magento 2. Suppose you want to facilitate the visitors on your Magento 2 app to search for products. In such cases, you can use the SearchCriteria in API to perform search or filter operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, I will show you how to use SearchCriteria in Magento 2 API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s begin with the&nbsp;Magento 2 API Search Criteria!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Magento 2 SearchCriteria?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">According to&nbsp;<a href=\"https:\/\/developer.adobe.com\/commerce\/php\/development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Adobe Commerce DevDocs<\/a>, A SearchCriteria is an implementation of the SearchCriteriaInterface class that allows you to build custom requests with different conditions. The SearchCriteria endpoint in the API is useful for fetching the data from the repository using various conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The SearchCriteria is specified in the URL of the GET request. The basic structure of the same is provided below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">searchCriteria[filter_groups][][filters][][field]=\nsearchCriteria[filter_groups][][filters][][value]=\nsearchCriteria[filter_groups][][filters][][condition_type]=<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where the field name, search value, and operators can be changed as per the requirement. Provided below is the list of the search operators for the SearchCriteria endpoint:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>list of the search operators<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Operator<\/strong><\/th><th><strong>Details<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>eq<\/code><\/td><td>Equals<\/td><\/tr><tr><td><code>finset<\/code><\/td><td>A value within a set of values<\/td><\/tr><tr><td><code>from<\/code><\/td><td>The beginning of a range. Must be used with&nbsp;<strong>to<\/strong>.<\/td><\/tr><tr><td><code>gt<\/code><\/td><td>Greater than<\/td><\/tr><tr><td><code>gteq<\/code><\/td><td>Greater than or equal<\/td><\/tr><tr><td><code>in<\/code><\/td><td>The value can contain a comma-separated list of values.<\/td><\/tr><tr><td><code>like<\/code><\/td><td>The value can contain the SQL wildcard characters when like is specified.<\/td><\/tr><tr><td><code>lt<\/code><\/td><td>Less than<\/td><\/tr><tr><td><code>lteq<\/code><\/td><td>Less than or equal<\/td><\/tr><tr><td><code>moreq<\/code><\/td><td>More or equal<\/td><\/tr><tr><td><code>neq<\/code><\/td><td>Not equal<\/td><\/tr><tr><td><code>nfinset<\/code><\/td><td>A value that is not within a set of values.<\/td><\/tr><tr><td><code>nin<\/code><\/td><td>Not in. The value can contain a comma-separated list of&nbsp;<strong>values<\/strong>.<\/td><\/tr><tr><td><code>nlike<\/code><\/td><td>Not like<\/td><\/tr><tr><td><code>notnull<\/code><\/td><td>Not null<\/td><\/tr><tr><td><code>null<\/code><\/td><td>Null<\/td><\/tr><tr><td><code>to<\/code><\/td><td>The end of a range. Must be used with&nbsp;<strong>from<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use Magento 2 SearchCriteria in Rest API?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to search for products containing a \u201cbag\u201d word, we\u2019ll use the SearchCriteria rest point with \u2018name\u2019 as field and \u2018bag\u2019 as value using the&nbsp;<strong>like<\/strong>&nbsp;operator. Complete API request for the same is provided below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method:&nbsp;<\/strong>GET<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>URL:&nbsp;<\/strong><em>store_url\/rest\/V1\/products<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Params:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Key<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>Value<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>searchCriteria[filter_groups][0][filters][0][field]<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\">name<\/td><\/tr><tr><td><code>searchCriteria[filter_groups][0][filters][0][value]<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\">bag<\/td><\/tr><tr><td><code>searchCriteria[filter_groups][0][filters][0][condition_type]<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\">like<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It will build the request with the SearchCriteria as below:<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">GET store_url\/rest\/V1\/products?\nsearchCriteria[filter_groups][0][filters][0][field]=name&amp;amp;\nsearchCriteria[filter_groups][0][filters][0][value]=bag&amp;amp;\nsearchCriteria[filter_groups][0][filters][0][condition_type]=like<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On calling the API request in Magento 2 with the above URL and params, it will return the list of the products containing the \u2018bag\u2019 word in the name attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the same way, the SearchCriteria can be used to perform search and filter functions in Magento 2 using the APIs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 is a powerful\u00a0<a href=\"https:\/\/meetanshi.com\/blog\/best-ecommerce-platforms\/\">eCommerce platform<\/a>, the functionalities of which can be augmented to other applications through the use of APIs. Magento 2 SearchCriteria is very useful for performing search and filter functions while developing third-party applications. I hope this complete tutorial will help you understand how to use SearchCriteria in Magento 2 API. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you loved reading this blog, then do not forget to share it with your Magento 2 friends through social media.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!  <\/p>\n\n\n\n<div class=\"wp-block-buttons is-nowrap is-layout-flex wp-container-core-buttons-is-layout-8f761849 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-product-by-sku\/\">Magento 2 API \u2013 Get Product by SKU<\/a><\/div>\n\n\n\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-all-available-currencies\/\">Magento 2 API \u2013 Get All Available Currencies<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Magento peeps! I am here with another&nbsp;Magento 2 API&nbsp;tutorial on SearchCriteria. In my last blog post, I provided a complete step-wise guide to getting&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2562","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2562"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions"}],"predecessor-version":[{"id":11740,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions\/11740"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}