A space to discuss online store customization, theme development, and Liquid templating.
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 !
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.
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.
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.
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
For all those still wondering how to fix this.
$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)
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.
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,
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.)
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.
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
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: