narzero
narzero

Reputation: 2299

How do I search for a product based on the SKU using the Shopify API?

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?

Upvotes: 2

Views: 4323

Answers (3)

Rafael Ruiz Tabares
Rafael Ruiz Tabares

Reputation: 741

You can use

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

Upvotes: 0

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 :)

Upvotes: 3

Steph Sharp
Steph Sharp

Reputation: 11682

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.

Upvotes: 1

Related Questions