api connection

Magento 2 API How to use search Criteria to get product

In Magento 2, you can use API to search for products either in detail or in general. When filtering product collections, the V1/products endpoint comes in handy. In addition, the search criteria params help limit your output with ease. Several search criteria can be specified in the URL Get request to deliver your desire products.

About the Search Criteria

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:
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 searchCriteria 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.

Code language: HTML, XML (xml)

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


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


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.

Code language: CSS (css)

Returns the current page.

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 <host>/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
Magento 2 API How to use search Criteria to get product 5

Step 2: Get product list information

Create a new request with this endpoint:

GET <host>/rest/V1/product

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

enter access token postman
Magento 2 API 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 key column and enter the value you want to get in 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 API How to use search Criteria to get product 7

API will return a response which 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 API How to use search Criteria to get product 8

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

I have shown you a quick look at what is 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: