Retrieve metafields with the Storefront API
You can retrieve metafields with the Storefront API to access additional information from different types of resources. This tutorial describes how to expose metafields to the Storefront API, retrieve them, and hide them from the Storefront API.
RequirementsAnchor link to section titled "Requirements"
LimitationsAnchor link to section titled "Limitations"
You can't create, update, or delete metafields with the Storefront API. If you want to perform these types of operations on metafields, then you need to use the GraphQL Admin API or the REST Admin API.
Exposing metafieldsAnchor link to section titled "Exposing metafields"
By default, the Storefront API can't read metafields. To expose specific metafields to the Storefront API, you need to use the GraphQL Admin API. For each metafield that you want to expose, you need to create a MetafieldStorefrontVisibility record.
MetafieldStorefrontVisibility record exposes all metafields that belong to the specified resource and have a specified
Retrieving metafieldsAnchor link to section titled "Retrieving metafields"
You can retrieve metafields on the following resources with the Storefront API:
|Article||An article in an online store blog.|
|Blog||An online store blog.|
|Collection||A grouping of products.|
|Customer||A customer account with the store. Customer accounts store contact information for the customer, saving logged-in customers the trouble of having to provide it at every checkout.|
|Order||A customer's request to purchase one or more products from a store.|
|Page||A page to hold static HTML content. Each
|Product||An individual item for sale in a Shopify store.|
|ProductVariant||A different version of a product, such as differing sizes or differing colors.|
|Shop||A collection of the general settings and information about a store.|
Hiding metafieldsAnchor link to section titled "Hiding metafields"
After you've exposed metafields with the GraphQL Admin API, and retrieved them with the Storefront API, you can hide metafields from the Storefront API again if you no longer need to access them. For example, you could hide all product metafields that have namespace
testapp and the key
pizza-size-inches from the Storefront API.
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.
Hide metafieldsAnchor link to section titled "Hide metafields"
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 metafieldStorefrontVisibilityDelete mutation.
The following example retrieves a list of
MetafieldStorefrontVisibility records. Each result returns the object's
The next example uses one of the returned IDs to delete the
MetafieldStorefrontVisibility that has the namespace
testapp and the key
- Explore the metafield reference for the Storefront API.