Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Get all metafields for products

Get all metafields for products

ReloadSEO
Shopify Partner
48 0 17

This has been posted before, but the Shopify support hasn't answered it yet... And it is a good and reasonable question. 

 

I want to get all the metafields for all the products.

My understanding is that there is no API like:

/admin/products/metafields.json

The only one is like this:

/admin/products/#productid#/metafields.json

Therefore, the only way is to get all the metafields for all the products is to make one API call per product!

It is also impossible to cache the data, because it takes one API call per product just to check if the metafields for that product have been updated!  For large stores, it is handicapping API applications and forcing one call per product, resulting in extremely time-consuming fetches for tiny amounts of data.

Can you please add an API call that works like this?  /admin/products/metafields.json

It would return all the metafields for all the products, 250 at a time.  We can then page through in a reasonable amount of time.

Or, does anyone know some other workaround to get all the product metafields at once?

Replies 64 (64)

fadi
Shopify Partner
11 0 3

Hey Guys,

Is there any update on this issue?

I am trying to read all products from Shopify Admin API. I also need to include the product metafields.

I have abou 7000 products and if I use the currentl solution it will took almost an hour to get these products, which is very very long time.

Kiflay

HunkyBill
Shopify Partner
4853 60 568

@fadi. 7000 products is 28 API calls. You get about 40 without delay, so without dwelling on what that exactly means, consider it quick, not very very long time.

Additionally, now with GraphQL at your disposal, you are free to get your products with the metafield data you crave at the same time. So it seems you are one lucky customer! Not only can you get your get products fast, but you no longer need to make secondary calls for the metafields.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

fadi
Shopify Partner
11 0 3

@HUnkyBIll Thanks for the quick response.

You right to get all products is 28 API call.

To get metafield for each product will be

(7000/2) seconds

I  have to fire a single request for each product. it Will take more than an hour.

So am not sure what do you mean it will be only 28 API

HunkyBill
Shopify Partner
4853 60 568

@fadi.

You missed something crucial in my explanation. Read it again. Closely. With GraphQL you can retrieve a product AND its metafield data of interest to you. 

So your math is off. I told you you now get to skip those 7000 extra calls. Pay attention man! I waste no words. They all matter.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

fadi
Shopify Partner
11 0 3

@HunkBill,

Look man I didn't ask you an advice with what I can and I can't do with GraphQL.My question is related to the REST API and metafields

You said 

"7000 products is 28 API calls. You get about 40 without delay, so without dwelling on what that exactly means, consider it quick, not very very long time." 

So how does getting the metafields for each product is not taking long calls for my example about an hour.

 

Alex_B
Shopify Staff
56 4 29

Allow me to interject here.

With the new GraphQL Admin API, it is possible to obtain metafields for products in a way that does not add to your REST Admin API call limit. It is possible to combine your existing REST calls, which gathers a ton of data at the Product level, and then use the "admin_graphql_api_id" from that call to query the metafields for those products using GraphQL.

Starting with the assumption that you already have the REST Admin API call to the Product endpoint, which gets you the "admin_graphql_api_id" of the individual products, you can use a GraphQL query similar to the following to get the metafields.

{
  product(id: "gid://shopify/Product/1321540321336"){
    metafields(first:10) {
      edges {
		node {
          id
          key
          value
          valueType
          namespace
        }
        cursor
      }
      pageInfo {
        hasNextPage
      }
    }
  }
}

The "cursor" and "pageInfo/hasNextPage" fields are there to help you determine if pagination is necessary on the metafields.

Performing this query in my test store on a product that has 2 metafields costs 5, which means with the GraphQL Admin APIs restoreRate of 50, you could get metafields on 10 products per second without getting throttled.

For more information on the GraphQL Admin API call limits, see the docs here.

Alex | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

fadi
Shopify Partner
11 0 3

Thanks Alex 

That could help.

To get product metafields  using the REST API there is no solution other than firing a single request for each product. Is this right Alex?

HunkyBill
Shopify Partner
4853 60 568

@ Fadi, you get spoon-fed an answer that saves your bacon and you get upset? Chill out. If you can make a call to the RestAPI endpoints, you can make a GraphQL call. And as Alex and I told you, with ONE call you can get 250 products AND their metafields. 

So move on, write your code and be happy someone filled you in and gave you free correct advice. It is common courtesy to be gracious and not defensive, as if I did something to offend you.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

Bharath
Shopify Partner
26 1 11

Hi All,

 

As everyone recommended GraphQL for better querying, we thought of trying this but from our initial testing, it seems like it's not giving a big advantage to us in querying.

 

using a REST API, we can get one product's 250 metafields in one call using "admin/products/{{id}}/metafields.json?limit=250" call. When it comes to GraphQL, the query cost is controlling us to have a maximum of 3 products at a time in the call i.e 

 

{
  products(first: 3) {
    edges {
      node {
        legacyResourceId
        metafields (first: 250) {
          edges {
            node {
              legacyResourceId
              value
              namespace
              key
            }
          }
        }
      }
    }
  }
}
  1. Is there any better way of querying for this purpose?
  2. Also, all resources in GraphQL don't have the main filter supported by REST API i.e ID. So how GraphQL is recommended if we have to fetch details for certain ids?
  3. As per Shopify, admin_graphql_api_id is not constant and it's not recommended to prepare the gid structure programmatically. So Shopify is expecting us to make a REST API call first to know the gids and then use GraphQL for querying?

 

Bharath
Shopify Partner
26 1 11

Hi All, 

 

Can anyone respond to these queries?

steffenmande
Tourist
7 1 2

Kind of amazed that this issue is not made easier after years.

 

In my use case i just need to get a list of all metafields, to know what the partner has created already, so i can update their content.

 

Getting all their metafields is a pain in the ass though.

 

Should be made possible to get all metafrields with a resource owner. ie. all metafields on a owner_resource=product, or variant or image whatever.

AndrewVelo
Visitor
1 0 2

The point that has been made by all the contributors are totally valid and I view the last response from a so called expect as both narrow and unhelpful in the extreme. There is a clear requirement for this resource from a large number of customers, please address the problem rather than dismissing it in a banal manner.

SigProdGroup
Tourist
11 1 3

+1.

Lazy developers trying to push Shopify to bend to their system? This is demeaning, and oblivious to the state of the global economy.

 

Shopify currently lives in the ecomm sector, a whopping 8% of the retail market. Shopify wants to be the platform we use to drive a movement of the remaining 92% of the market, that is big business retail, from brick and mortar to ecommerce. Wal-Mart, Target, etc. these are the kings, not Shopify. To capture that business, Shopify must accommodate big business ERP databases and the breadth of complexity that retail chains demand. That means keeping item data in sync as much as keeping orders in sync. This metafield API issue is one pain point that Shopify has simply failed to accommodate so far.

 

The industry is moving this way. If you view us as pushing Shopify around, that's because we are. If Shopify doesn't oblige, they will simply not be relevant in this small piece of the market. Massive API pulls against metadata fields is not abuse. It's use in this case. And I expect we'll see similar "abuse" against other endpoints before the push is over. It's up to Shopify to figure out how to manage that without folding to performance issues.

 

I hope to see Shopify manage this quickly so that I can rely on Shopify as more business moves onto this platform.

sipsynthemesc
Shopify Partner
7 0 2

Just here to credit SigProdGroup. 

Shopify is the wordpress of e-commerce and has been that since Tobi left as CTO. I only ever find myself stumbling across these posts when trying to get more from the platform. Quite frankly, Shopify nowadays is a f*cking mess, constantly publishing unfinished features to the system or as we bare witnessed with "Shopify Mail" an overhyped pile of trash. Fear not my fellow engineers for the blue haired genderless support team is on your side who have zero f*cking clue.

Anyway, if any of you stumble across this thread and read this post, take a look at Centra which is what I believe to be the future of e-commerce. It flaunts a real headless e-commerce solution, none of this bitchery you get from the Shopify cretins. 

ScreenStaring
Shopify Partner
56 2 10

While still time-consuming here is a script to download the metafields of all products in a store to a JSONL file: https://gist.github.com/sshaw/dea140dd7a6afc1a4de77e7158605ee7. It requires 2 supplementary programs. See the script for more information. 

ScreenStaring
Software Development & Consulting