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.
Requirements
Anchor link to section titled "Requirements"- You've reviewed and met the requirements that are listed on the Getting started with the Storefront API page.
- 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.
Limitations
Anchor 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 metafields
Anchor 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.
Each MetafieldStorefrontVisibility
record exposes all metafields that belong to the specified resource and have a specified namespace
and key
combination.
Retrieving metafields
Anchor link to section titled "Retrieving metafields"You can retrieve metafields on the following resources with the Storefront API:
Resource | Description |
---|---|
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 Page object represents a custom page on the online store. |
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 metafields
Anchor 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 metafields
Anchor 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,PRODUCT
.
The following example creates a MetafieldStorefrontVisibility
record that exposes all product metafields that have the namespace testapp
and the key pizza-size-inches
:
Step 2: Retrieve metafields
Anchor 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 namespace
and key
arguments.
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 metafields
Anchor 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 id
, namespace
, key
, and ownerType
.
The next example uses one of the returned IDs to delete the MetafieldStorefrontVisibility
that has the namespace testapp
and the key pizza-size-inches
.
- Explore the metafield reference for the Storefront API.