api connection

Magento 2 Search Criteria API (Updated 2023): How to use search Criteria to get product

You can use Magento 2 API to search for products in detail or general. When filtering product collections, the V1/products endpoint comes in handy. In addition, the Magento 2 search criteria API parameters help easily limit your output. Several search criteria can be specified in the URL Get request to deliver your desired products.

About the Magento 2 Search Criteria API

The basic pattern for specifying the criteria is

searchCriteria[filter_groups][<index>][filters][<index>][field]=<field_name> searchCriteria[filter_groups][<index>][filters][<index>][value]=<search_value> searchCriteria[filter_groups][<index>][filters][<index>][condition_type]=<operator>
Code language: HTML, XML (xml)
  • field is an attribute name.
  • value specifies the value to search for.
  • condition_type is one of the following values:
CONDITIONNOTES
eq Equals
finset A value within a set of values
fromThe beginning of a range. Must be used with to
gtGreater than
gteqGreater than or equal
inIn. The value can contain a comma-separated list of values
likeLike. The value can contain the SQL wildcard characters when like is specified
lt Less than
lteq Less than or equal
moreqMore or equal
neqNot equal
nfinset A value that is not within a set of values
ninNot in. The value can contain a comma-separated list of values
notnullNot null
nullNull to The end of a range. Must be used with from

The following search criteria can be used to determine the sort order and the number of items to return.

Specifies the field to sort on. By default, search results are returned in descending order.

searchCriteria[sortOrders][<index>][field]=<field-name>
Code language: HTML, XML (xml)

You can sort on multiple fields. For example, to sort on price first and then by name, call:

searchCriteria[sortOrders][0][field]=price&searchCriteria[sortOrders][1][field]=name.

Specifies whether to return results in ascending (ASC) or descending (DESC) order.

searchCriteria[sortOrders][<index>][direction]=ASC | DESC
Code language: HTML, XML (xml)

To expand the previous example and sort the price fields in descending order and the name fields in ascending order, call.

searchCriteria[sortOrders][0][field]=price&searchCriteria[sortOrders][1][field]=name&searchCriteria[sortOrders][1][direction]=ASC

Specifies the maximum number of items to return. The value must be an integer. If the pageSize is not specified, the system returns all matches.

searchCriteria[pageSize]
Code language: CSS (css)

Returns the current page.

searchCriteria[currentPage]
Code language: CSS (css)

Today, we will learn how to get the product list from a Magento 2 website by using the REST API. Here we are using Postman to access the API.

Step 1: Get Access Token

We need to get the access token of the admin user with this endpoint:

POST /rest/V1/integration/admin/token

Enter your Magento admin username and password in Body sections, then click Send

{ "username" : "string", "password" : "string" }
Code language: JSON / JSON with Comments (json)

If your username and password are correct, the API will return the token in response section.

get access token magento 2 api
Magento 2 Search Criteria API (Updated 2023): How to use search Criteria to get product 5

Step 2: Get product list information

Create a new request with this endpoint:

GET /rest/V1/product

In the Authorization section, choose Bearer Type and enter your Access Token.

enter access token postman
Magento 2 Search Criteria API (Updated 2023): How to use search Criteria to get product 6

There are two ways to enter search criteria.

#1: In Param section, enter search criteria in the key column and enter the value you want in the value column. Remember to put a tick in left checkbox to choose which criteria you will apply for the search.

#2: In Request section, enter search criteria along with its value in as one single line.

You can choose to enter search criteria in the Request section or in Param section; either way will work just fine.

Simple search example:

The following example returns all product items whose category_gear attribute includes the value 86 are returned by the following query.

GET <host>/rest/<store_code>/V1/products/? searchCriteria[filter_groups][0][filters][0][field]=category_gear& searchCriteria[filter_groups][0][filters][0][value]=86& searchCriteria[filter_groups][0][filters][0][condition_type]=finset fields=items[sku,name]
Code language: HTML, XML (xml)
search criteria select
Magento 2 Search Criteria API (Updated 2023): How to use search Criteria to get product 7

API will return a response that has data like this:

{ "items": [ { "sku": "24-MG04", "name": "Aim Analog Watch" }, { "sku": "24-MG01", "name": "Endurance Watch" }, { "sku": "24-MG03", "name": "Summit Watch" }, { "sku": "24-MG05", "name": "Cruise Dual Analog Watch" }, { "sku": "24-MG02", "name": "Dash Digital Watch" }, { "sku": "24-WG09", "name": "Luma Analog Watch" }, { "sku": "24-WG01", "name": "Bolo Sport Watch" }, { "sku": "24-WG03", "name": "Clamber Watch" }, { "sku": "24-WG02", "name": "Didi Sport Watch" } ] }
Code language: JSON / JSON with Comments (json)
product API response
Magento 2 Search Criteria API (Updated 2023): How to use search Criteria to get product 8

The above example returns the SKU and name of the product which is tagged as electronics following Magento Open Source sample data.

I have shown you a quick look at the search criteria and how to use it to get product list API in Magento 2. Visit our blog to learn more about Magento 2 API or check here.

See how it all goes through with our demo: