Magento 2 API – SearchCriteria
End-to-end Magento integration
Integrate any app or service. Consult our API development experts.
Hello, Magento peeps!
I am here with another Magento 2 API 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 Magento 2 API – Get Product by SKU.
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.
In this post, I will show you how to use SearchCriteria in Magento 2 API.
Let’s begin with the Magento 2 API Search Criteria!
What is Magento 2 SearchCriteria?
According to Adobe Commerce DevDocs, 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.
The SearchCriteria is specified in the URL of the GET request. The basic structure of the same is provided below.
1 2 3 |
searchCriteria[filter_groups][][filters][][field]= searchCriteria[filter_groups][][filters][][value]= searchCriteria[filter_groups][][filters][][condition_type]= |
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:
list of the search operators |
|
---|---|
Operator | Details | eq | Equals |
finset | A value within a set of values |
from | The beginning of a range. Must be used with to. |
gt | Greater than |
gteq | Greater than or equal |
in | The value can contain a comma-separated list of values. |
like | The value can contain the SQL wildcard characters when like is specified. |
lt | Less than |
lteq | Less than or equal |
moreq | More or equal |
neq | Not equal |
nfinset | A value that is not within a set of values. |
nin | Not in. The value can contain a comma-separated list of values. |
nlike | Not like |
notnull | Not null |
null | Null |
to | The end of a range. Must be used with from. |
How to Use Magento 2 SearchCriteria in Rest API?
In order to search for products containing a “bag” word, we’ll use the SearchCriteria rest point with ‘name’ as field and ‘bag’ as value using the like operator. Complete API request for the same is provided below.
Method: GET
URL: store_url/rest/V1/products
Params: |
|
---|---|
Key | Value |
searchCriteria[filter_groups][0][filters][0][field] |
name |
searchCriteria[filter_groups][0][filters][0][value] |
bag |
searchCriteria[filter_groups][0][filters][0][condition_type] |
like |
It will build the request with the SearchCriteria as below:
1 2 3 4 |
GET store_url/rest/V1/products? searchCriteria[filter_groups][0][filters][0][field]=name& searchCriteria[filter_groups][0][filters][0][value]=bag& searchCriteria[filter_groups][0][filters][0][condition_type]=like |
Response:
On calling the API request in Magento 2 with the above URL and params, it will return the list of the products containing the ‘bag’ word in the name attribute.
In the same way, the SearchCriteria can be used to perform search and filter functions in Magento 2 using the APIs.
Conclusion
Magento 2 is a powerful eCommerce platform, 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. In case you still have any queries or doubts regarding the provided tutorial, feel free to comment. I would be happy to help! 😇
If you loved reading this blog, then do not forget to share it with your Magento 2 friends through social media. 😃
Thanks for reading! 🍀
◄ Magento 2 API – Get Product by SKUMagento 2 API – Get All Available Currencies ►
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
2 Comments
Hello sir
nice information magento installation
Hello Azeem
That is great to know that the above content is helpful for you.
Thank You