2

I'm using the Ruby wrapper for Shopify's API and I want to search for products based on the SKU's.

Here's what I'm currently trying:

ShopifyAPI::Product.find(sku: 'wi196217')

# => ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.
# from /Users/narzero/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:144:in `handle_response'

I've also tried:

require 'curb'

p = Curl.get('https://<REDACTED>:<REDACTED>@<REDACTED>.myshopify.com/admin/products/search.json?query=sku:wi196217')

puts p.body

# => "{\"errors\":\"[API] That action is not currently supported.\"}"

What should I try next?

narzero
  • 2,199
  • 5
  • 40
  • 73
  • This `admin/products/search.json` API endpoint seems to work ok in my browser at least, but where is it documented? I don't see it in the Shopify API docs (https://docs.shopify.com/api/reference/product). I too want to find a product based on SKU. – Liam Mar 04 '16 at 20:54

3 Answers3

3

The search URL requires a cookie to work (that only the admin has) this cookie is set when you login to the shopify site. So when trying to access it publicly via the API it returns

501 : {"errors":"[API] That action is not currently supported."}

To get past this I forked the gem and wrote a hack that allows you to search products through the gem.

https://github.com/achadee/shopify_api

I would not recommend using this for large inventory stores as it pulls down all the products down before searching... anyway usage is like so:

ShopifyAPI::Product.search({:sku => "XFUS-63912"})

ShopifyAPI::Product.search({:sku => "XFUS-63912", :handle => "hey hey"})

Also it dynamically searches any tag on both the product and all its variants

enjoy :)

1

First thing I noticed is that sku is a property of Product Variants, not Products.

This Stack Overflow answer indicates that it's not possible to get a product based on its sku with the Shopify API, but I found this discussion on the Shopify forums that may be useful to you.

Community
  • 1
  • 1
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
0

You can use

*.myshopify.com/search?view=json&type=product&q=sku:SKUID