Retrieve metafields with the Storefront API
You can retrieve metafields with the Storefront API to access additional information from different types of resources. This guide describes how to expose metafields to the Storefront API, retrieve them, and hide them from the Storefront API.
RequirementsAnchor link to section titled "Requirements"
- You've completed the Getting started with the Storefront API guide.
You're familiar with querying products and collections.
You've created resources that support metafields in your store, and you've created metafields for those resources.
You're familiar with how metafields work.
Step 1: Expose metafieldsAnchor link to section titled "Step 1: Expose metafields"
To create a
MetafieldStorefrontVisibility record, you can use the
metafieldStorefrontVisibilityCreate mutation in the GraphQL Admin API. The input object for the mutation uses the following fields:
namespace— The namespace of the metafields to be visible to the Storefront API.
key— The key of the metafields to be visible to the Storefront API.
ownerType— The core resource that owns this metafield. For example,
The following example creates a
MetafieldStorefrontVisibility record that exposes all product metafields that have the namespace
testapp and the key
Step 2: Retrieve metafieldsAnchor link to section titled "Step 2: Retrieve metafields"
After exposing metafields, you can retrieve them with the Storefront API by using the
metafield field. You can retrieve a single metafield for a product or a product variant. To specify the metafield that you want to retrieve, use the
In the following example, you have a product called “Amazing Frozen Pizza” and you've created metafields that store the size of the pizza and the pizza's expiration date. You want to display those values on the storefront according to each metafield's type. The following example shows how to retrieve the value and type for each metafield using the Storefront API.
Step 3: Hide metafields (optional)Anchor link to section titled "Step 3: Hide metafields (optional)"
If you no longer need to access a metafield with the Storefront API, then you can hide it again by using the GraphQL Admin API to delete the
MetafieldStorefrontVisibility record that you created.
To delete a
MetafieldStorefrontVisibility record, you need to provide its ID to the
The following example retrieves a list of
MetafieldStorefrontVisibility records. Each result returns the object's
The following example uses one of the returned IDs to delete the
MetafieldStorefrontVisibility that has the namespace
testapp and the key
- Learn how to use metafields to store information related to your app and attach that information to Shopify API resources.
- Explore the metafield reference for the Storefront API.