Re: Product Search By Title Using API

Product Search By Title Using API

Pulkit_Saxena
Shopify Partner
1 0 0

Hi,

I am looking for searching products based on title.

But I can't  find any search API for products in API section. Have I missed it somewhere or any info regarding this is appreciated.

Thanks !

Replies 11 (11)

Thuong_Mai
Shopify Partner
42 0 0

Hi Pulkit,

You should use the /admin/products.json API, fields=id,title, limit=250 and since_id query parameters to fetch all products, and then perform searching on your side. That's the way I've been using so far.

A Shopify agency that helps build, promote, and expand e-commerce businesses.

dawich77
Shopify Partner
7 0 1

It's a little bit of work to set up, but I used the python api and created a sqlite database that stored all of the product id's and associated sku's so that I could just query the database and look up the product directly by id, given a specific sku:

product = shopify.Product.find(id)

Then I can make changes to the product and save it. You could potentially do the same thing with titles instead of skus.

Jared_Burns1
Shopify Partner
30 0 8

You can search by title using the "title" param on the /admin/products.json API that Shopinet mentioned. As in:

/admin/products.json?title=<searchString>,limit=250,page=1,fields="id,title,etc"

I've found that some very simple matching is done. For example, searching with a title of "blue" will match a product named "blue shirt". But I haven't found a way to insert real wildcards... and the matching from the API isn't as good as the matching demonstrated by Shopify's own admin UI widgets.

So for now I'm using the "title" query param, but planning to pull everything into my own searchable datastore in the near future.

carchrae
Visitor
1 0 0

 

perhaps the api changed, but i wanted to note this is not correct

 

 

/admin/products.json?title=<searchString>,limit=250,page=1,fields="id,title,etc"

 

 

rather, you want the extra options added with `&` and not " so instead

 

/admin/api/2020-04/products.json?title=<searchString>&limit=250&fields=id,title

 

also page=1 isn't supported anymore, see here for updated pagination docs https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api

 

 

albabameem
Tourist
3 0 1

For all those still wondering how to fix this. 

  1. Title parameter in the API cannot be used to search products, it will only work if you pass the exact product name. 
  2. For CORS, hit the API on the backend rather than the client side.
  3. This is not the optimum solution but the best way I could make it work so far is by looping through all the products using since_id and then filtering the results out. Here's my code

 

        $keyword = $request->keyword;
        $active = true;
        $productsArray = [];
        $sinceId = 0;

        //loop until 0 products are returned
        while($active){
                $products = hitCurl('/admin/api/2021-01/products.json?limit=250&since_id=' . $sinceId, 'GET', $curlFields = "");
            if(sizeof($products['products']) <= 0){
                //break the loop when result is empty
                $active = false;
            }

            //loop within the results and push the product to an array
            for ($i = 0; $i <= sizeof($products['products']) - 1; $i++) {
                array_push($productsArray, $products['products'][$i]);

                //update the since_id to the latest id in the response
                if($i == sizeof($products['products']) - 1){
                    $sinceId = $products['products'][$i]['id'];
                }
            }
        }

        //loop the productsArray and filter results using strpos (php)

 

Ari9
Excursionist
35 3 11

You cannot query using the REST API from what I've found.. but you do have another option, which is to use GraphQL. It accepts a query parameter.

Here's an example:

POST https://{store}.myshopify.com/admin/api/2021-04/graphql.json

QUERY

{
  products(first: 10, query: "title=\"example produ\"") {
    edges {
      node {
        id
        handle
        title
        description
        featuredImage {
          transformedSrc
        }
        priceRangeV2 {
          minVariantPrice {
            amount
            currencyCode
          }
          maxVariantPrice {
            amount
            currencyCode
          }
        }
        status
        totalInventory
        totalVariants
      }
    }
  }
}


If you are unsure on how to work with the GraphQL API, you can follow the documentation here:
https://shopify.dev/docs/admin-api/getting-started#authentication

I couldn't find the documentation for the products query, but you can use a program like Insomnia to download the schema.
 

banned

Mounir_HAB-ARRI
Shopify Partner
11 0 12

Hi Burns,

I did an Ajax request inside my Shopify App using this "/admin/products.json?title=<searchString>,limit=250,page=1,fields="id,title,etc" to search product by title ... but i get nailed by CORS.

Is there any way to use Ajax API call inside Shopify App or any alternative solution to Filter/Search data from API ?

Thank you,

Jared_Burns1
Shopify Partner
30 0 8

I don't believe the CORS issue you're seeing is related to the query params used for searching products by title. This sounds like a general issue getting started with the API.

For what it's worth, you need to call the JSON API from your registered application... by which I mean your registered, authenticated server needs to make the call to the Shopify server. Not the browser. (I mention this because you said you're trying to use Ajax.)

Mounir_HAB-ARRI
Shopify Partner
11 0 12

Thank you Jerard,

Actually I have an Embeded App on Shopify and its run on my private VPS with an SSL certificate ... and I make calls to the Shopify API by a library called ShopifySharp ... every thing work great, But I need to filter & search products by Title! 

Is there any alternative way to do a client side filter & search ? I heard about the App Proxy but i'm not sure !

Thank you.

Akki
Shopify Partner
17 0 2

Hi Jared,

Did you find any way to search with wildcards in API. Currently, the api is not searching for picking the case sensitive words e.g "/admin/products.json?title=Test is not giving me the "test" results.

Thank you,

Ankit

pguardiario
Shopify Partner
1 0 0

I got this working today with storefront token and predictive search query (it's not specifically title but my results look good), it looked like this:

 
  headers: {
    'Content-Type': 'application/json',
    'X-Shopify-Storefront-Access-Token': "use storefront token not admin token"
  },
  method: "POST",
  body: JSON.stringify({
    query: `{
      predictiveSearch(query: "a", limit: 10) {
        products {
          id
          handle
          vendor
          variants(first: 2) {
            edges {
              node {
                id
                title
                priceV2 {
                  amount
                }
              }
            }
          }
        }
      }
    }`
  })
})