Retrieve variant metafields in bulk

Tim_Richardson
Shopify Partner
12 1 1

Hi, using the REST API I can retrieve metafields for a specific variant.

How do I get metafields for all variants, other than one get per variant?

Replies 2 (2)
Kevin_A
Shopify Staff
Shopify Staff
318 42 61

Hey @Tim_Richardson 

I believe you need to go through the product to retrieve its metafields. Have you thought about using the GraphQL API at all? You could do something like this in GraphQL:

{
  productVariants(first: 5) {
    edges {
      node {
        metafields(first: 5) {
          edges {
            node {
              description
            }
          }
        }
      }
    }
  }
}

Kevin_A | Solutions Engineer @ 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 Shopify.dev or the Shopify Web Design and Development Blog

Gregarican
Shopify Partner
1033 86 282

To pull metafields for all product variants, you'd need to perform a bulk operation mutation via the GraphQL API. Something outlined here --> https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api

Here's an example where the bulk operations request is pulling the first 5 metafields for all product variants. When the operation has completed, the resultant JSONL file can be downloaded. I found this a lot more efficient compared to iterating through the cursor-paginated responses via either the GraphQL or REST API's!

mutation {
  bulkOperationRunQuery(
   query: """
    {
      productVariants {
        edges {
          node {
            id
            title
            metafields(first: 5) {
              edges {
                node {
                  id
                  description
                }
              }
            }
          }
        }
      }
    }
    """
  ) {
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}