Shop's SKU Count...there must be an easier way

Billy_Griffiths
Shopify Partner
17 0 6

I want to do some calculations based on the amount of variants (SKUs) a shop has used, but I cant seem to find an API endpoint for that specific request. Is looping through every product and counting the number of variants really the only way? Nothing quicker?

Thanks!

Web development using (primarily) CakePHP. Trying my hand at Shopify Apps.
Replies 6 (6)
Sebastian_Sztur
Shopify Partner
13 0 3

Sadly there is no api endpoint for the variants count. You could loop through the paginated variants.json instead of through all the products. Depending on the amount of products in your shop, it might be faster. 

GET /admin/variants.json

EDIT: Keep in mind that this endpoint is not in the documentation and might change.

HunkyBill
Shopify Expert
4610 54 531

Since there is no such thing as "Give me a count of variants (SKUs)", and the fastest API method is to 

  1. get all the products in a store. Since you get 250 per API call, this is relatively efficient, but slows considerably after the API limits are reached.
  2. each product provides a count of their variants without any API calls, so this become a simple sum in a loop of each product's variants

In a small store this is fast... but in a large one.. it is really slow... a sample benchmark of 3 runs is below.. note the tiny shop size. Tried with store with 70,000 products and ran into a brick wall on the practicality of this...

   user     system      total        real
   Getting 53 Products from Shopify...  SKU count  = 123

    0.020000   0.010000   0.030000 (  0.931971)
    0.030000   0.000000   0.030000 (  0.509388)
    0.030000   0.000000   0.030000 (  0.522662)

user     system      total        real
Getting 68730 Products from Shopify... SKU count 1014322
148.090000   2.970000 151.060000 (3371.578904)

So... that's a wee bit of a stretch there... 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Josh_Brown2
Shopify Partner
40 0 7

Interesting conversation (and benchmarks).

I don't think it's necessary to loop through all the variants. You can cut down on the number of API calls by guessing at some high page numbers. For example...

GET /admin/variants.json?limit=250&page=100 - returns nothing, therefore the number of variants is fewer than 25,000

GET /admin/variants.json?limit=250&page=50 - returns 250 items, so 12,500 < variants < 25,000

GET /admin/variants.json?limit=250&page=75 - returns 250 items, so... I'm sure you get the idea!

Hope this helps!

HunkyBill
Shopify Expert
4610 54 531

How does guessing how many calls to make, reduce the number of calls you need to make? I am confused there. You cannot escape the fact that if you need to make 12, 128, or 1657 API calls, you need to make those calls. Personally, I write the code so that I make the right number of calls, not one less, and not one more than needed. 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
mffowler
Shopify Partner
26 0 5

This is easy to do in admin. No need to paginate.

Product count: /admin/products/count.json

Variants count: /admin/variants/count.json

niels-hecquard
Tourist
9 0 3

OMG... this is not documented ! And yet so useful, thanks !

 

@Shopify can we really use this safely ?