Retrieve variant metafields in bulk

Highlighted
Shopify Partner
10 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?

0 Likes
Highlighted
Shopify Staff
Shopify Staff
144 18 22

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 | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
Highlighted
Shopify Partner
529 38 109

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
    }
  }
}
0 Likes