Skip to main content

Metafields API

Requires access to protected customer data for some properties.

The Metafields API provides access to app-owned metafields and the ability to write cart metafields. Use this API to read metafield values that your app owns and that you've configured in shopify.extension.toml, and to apply changes to cart metafields during checkout.

Cart metafields written with applyMetafieldChange() are shared across all extensions. Changes made by one extension are visible to others.

  • Read app-owned metafields: Access metafield values defined in your extension configuration to personalize the checkout experience.
  • Store data on the cart: Save custom key-value pairs to the cart that persist through the checkout flow and carry over to the resulting order.
  • Clear stored cart data: Remove a previously written cart metafield when the buyer changes their selection or the data is no longer needed.
Support
Targets (33)

The shopify global object provides metafield data for the current checkout. Access the following properties on shopify to read app-owned metafields and checkout metafields. Available to purchase extension targets.

Anchor to appMetafields
appMetafields
<[]>
required

The metafields requested in the shopify.extension.toml file. These metafields are updated when there's a change in the merchandise items being purchased by the customer.

App owned metafields are supported and are returned using the $app format. The fully qualified reserved namespace format such as app--{your-app-id}[--{optional-namespace}] isn't supported. See app owned metafields for more information.

Requires access to protected customer data.

Anchor to metafields
metafields
<[]>
required

The metafields that apply to the current checkout.

Metafields are stored locally on the client and are applied to the order object after the checkout completes.

These metafields are shared by all extensions running on checkout, and persist for as long as the customer is working on this checkout.

Once the order is created, you can query these metafields using the GraphQL Admin API

Tip

> Cart metafields are only available on carts created via the Storefront API version 2023-04 or later.

The shopify global object provides methods to modify cart metafields. Access the following methods on shopify to apply metafield changes. Available to purchase.checkout extension targets.

Anchor to applyMetafieldChange
applyMetafieldChange
(change: ) => Promise<>
required

Creates, updates, or removes a metafield on the checkout. On success, the metafields property updates to reflect the change.

Note

This method returns an error if the cart instruction metafields.canSetCartMetafields is false, or the buyer is using an accelerated checkout method, such as Apple Pay or Google Pay.

Anchor to Available Preact hooksAvailable Preact hooks

The following Preact hooks provide a convenience wrapper that makes it easier to perform common tasks without writing your own logic.

Returns the metafields configured with shopify.extension.toml.

Anchor to filters
filters
Default: {}

[]

  • Check cart instructions before writing: Use instructions.metafields.canSetCartMetafields and instructions.metafields.canDeleteCartMetafield from the Cart Instructions API to verify that metafield operations are permitted before calling applyMetafieldChange.
  • Use the $app namespace for app-owned data: Metafields under the $app namespace are exclusively controlled by your app. Other extensions can't read or modify them, making this the safest place to store app-specific data.

  • Cart metafield changes aren't available when the buyer uses an accelerated checkout method such as Apple Pay or Google Pay.

Was this page helpful?