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>
  • 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 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.

searchCriteria[sortOrders][<index>][field]=<field-name>

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

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]

Returns the current page.

searchCriteria[currentPage]

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"
}

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

get access token magento

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

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]
search criteria select

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"
        }
    ]
}
product API response

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:

https://youtu.be/ic_Xv17LcO0
Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email