# Metafields The API for interacting with metafields. ## StandardApi The base API object provided to `purchase` extension targets. ### Docs_Standard_MetafieldsApi ### appMetafields The metafields requested in the [`shopify.extension.toml`](/docs/api/checkout-ui-extensions/configuration) file. These metafields are updated when there's a change in the merchandise items being purchased by the customer. {% include /apps/checkout/privacy-icon.md %} Requires access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data). > Tip: > Cart metafields are only available on carts created via the Storefront API version `2023-04` or later.* ### metafields 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](/docs/admin-api/graphql/reference/orders/order#metafield-2021-01) ### AppMetafieldEntry A metafield associated with the shop or a resource on the checkout. ### metafield The metadata information. ### target The target that is associated to the metadata. {% include /apps/checkout/privacy-icon.md %} Requires access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data) when the type is `customer`, `company` or `companyLocation`. ### AppMetafield Represents a custom metadata attached to a resource. ### key The key name of a metafield. ### namespace The namespace for a metafield. ### type The metafield's type name. ### value The value of a metafield. ### valueType The metafield’s information type. ### AppMetafieldEntryTarget The metafield owner. ### id The numeric owner ID that is associated with the metafield. ### type The type of the metafield owner. {% include /apps/checkout/privacy-icon.md %} Requires access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data) when the type is `customer`, `company` or `companyLocation`. ### Metafield Metadata associated with the checkout. ### key The name of the metafield. It must be between 3 and 30 characters in length (inclusive). ### namespace A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. This must be between 2 and 20 characters in length (inclusive). ### value The information to be stored as metadata. ### valueType The metafield’s information type. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout) ## useAppMetafields Returns the metafields configured with `shopify.extension.toml`. ### UseAppMetafieldsGeneratedType Returns the metafields configured with `shopify.extension.toml`. #### Returns: AppMetafieldEntry[] #### Params: - filters: AppMetafieldFilters export function useAppMetafields< Target extends RenderExtensionTarget = RenderExtensionTarget, >(filters: AppMetafieldFilters = {}): AppMetafieldEntry[] { const appMetafields = useSubscription(useApi().appMetafields); return useMemo(() => { if (filters.key && !filters.namespace) { throw new CheckoutUIExtensionError( 'You must pass in a namespace with a key', ); } const filterKeys = Object.keys(filters) as AppMetafieldFilterKeys[]; if (filterKeys.length) { return appMetafields.filter((app) => { return filterKeys.every((key) => { if (key === 'id' || key === 'type') { return app.target[key] === filters[key]; } return app.metafield[key] === filters[key]; }); }); } return appMetafields; }, [filters, appMetafields]); } ### AppMetafieldFilters ### id ### key ### namespace ### type ### AppMetafieldEntry A metafield associated with the shop or a resource on the checkout. ### metafield The metadata information. ### target The target that is associated to the metadata. {% include /apps/checkout/privacy-icon.md %} Requires access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data) when the type is `customer`, `company` or `companyLocation`. ### AppMetafield Represents a custom metadata attached to a resource. ### key The key name of a metafield. ### namespace The namespace for a metafield. ### type The metafield's type name. ### value The value of a metafield. ### valueType The metafield’s information type. ### AppMetafieldEntryTarget The metafield owner. ### id The numeric owner ID that is associated with the metafield. ### type The type of the metafield owner. {% include /apps/checkout/privacy-icon.md %} Requires access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data) when the type is `customer`, `company` or `companyLocation`. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout) ## useMetafield Returns a single filtered `Metafield` or `undefined`. ### UseMetafieldGeneratedType Returns a single filtered `Metafield` or `undefined`. #### Returns: Metafield | undefined #### Params: - filters: MetafieldFilter export function useMetafield(filters: MetafieldFilter): Metafield | undefined { const {namespace, key} = filters; if (!namespace || !key) { throw new CheckoutUIExtensionError( 'You must pass in both a namespace and key', ); } const metafields = useMetafields({namespace, key}); return metafields.length ? metafields[0] : undefined; } ### MetafieldFilter ### key ### namespace ### Metafield Metadata associated with the checkout. ### key The name of the metafield. It must be between 3 and 30 characters in length (inclusive). ### namespace A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. This must be between 2 and 20 characters in length (inclusive). ### value The information to be stored as metadata. ### valueType The metafield’s information type. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout) ## useMetafields Returns the current array of `metafields` applied to the checkout. You can optionally filter the list. ### UseMetafieldsGeneratedType Returns the current array of `metafields` applied to the checkout. You can optionally filter the list. #### Returns: Metafield[] #### Params: - filters: MetafieldsFilters export function useMetafields< Target extends RenderExtensionTarget = RenderExtensionTarget, >(filters?: MetafieldsFilters): Metafield[] { const metaFields = useSubscription(useApi().metafields); return useMemo(() => { if (filters) { const {namespace, key} = filters; if (!namespace) { throw new CheckoutUIExtensionError( 'You must pass in a namespace with a key', ); } const filteredResults = metaFields.filter( (metafield) => metafield.namespace === namespace && (!key || metafield.key === key), ); return filteredResults; } return metaFields; }, [filters, metaFields]); } ### MetafieldsFilters ### key ### namespace ### Metafield Metadata associated with the checkout. ### key The name of the metafield. It must be between 3 and 30 characters in length (inclusive). ### namespace A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. This must be between 2 and 20 characters in length (inclusive). ### value The information to be stored as metadata. ### valueType The metafield’s information type. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout) ## CheckoutApi The API object provided to `purchase.checkout` extension targets. ### Docs_Checkout_MetafieldsApi ### applyMetafieldChange Performs an update on a piece of metadata attached to the checkout. If successful, this mutation results in an update to the value retrieved through the [`metafields`](/docs/api/checkout-ui-extensions/apis/metafields#standardapi-propertydetail-metafields) property. > Note: This method will return an error if the [cart instruction](/docs/api/checkout-ui-extensions/apis/cart-instructions#standardapi-propertydetail-instructions) `metafields.canSetCartMetafields` is false, or the buyer is using an accelerated checkout method, such as Apple Pay, Google Pay, or Meta Pay. ### MetafieldChange MetafieldRemoveChange | MetafieldUpdateChange | MetafieldRemoveCartChange | MetafieldUpdateCartChange ### MetafieldRemoveChange Removes a metafield. ### key The name of the metafield to remove. ### namespace The namespace of the metafield to remove. ### type The type of the `MetafieldRemoveChange` API. ### Metafield Metadata associated with the checkout. ### key The name of the metafield. It must be between 3 and 30 characters in length (inclusive). ### namespace A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. This must be between 2 and 20 characters in length (inclusive). ### value The information to be stored as metadata. ### valueType The metafield’s information type. ### MetafieldUpdateChange Updates a metafield. If a metafield with the provided key and namespace does not already exist, it gets created. ### key The name of the metafield to update. ### namespace The namespace of the metafield to add. ### type The type of the `MetafieldUpdateChange` API. ### value The new information to store in the metafield. ### valueType The metafield’s information type. ### MetafieldRemoveCartChange Removes a cart metafield. ### key The name of the metafield to remove. ### namespace The namespace of the metafield to remove. ### type The type of the `MetafieldRemoveChange` API. ### CartMetafield Represents a custom metadata attached to a resource. ### key The key name of a metafield. ### namespace The namespace for a metafield. ### type The metafield's type name. ### value The value of a metafield. ### MetafieldUpdateCartChange Updates a cart metafield. If a metafield with the provided key and namespace does not already exist, it gets created. ### metafield ### type The type of the `MetafieldUpdateChange` API. ### MetafieldChangeResult MetafieldChangeResultSuccess | MetafieldChangeResultError ### MetafieldChangeResultSuccess ### type The type of the `MetafieldChangeResultSuccess` API. ### MetafieldChangeResultError ### message A message that explains the error. This message is useful for debugging. It is **not** localized, and therefore should not be presented directly to the buyer. ### type The type of the `MetafieldChangeResultError` API. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout) ## useApplyMetafieldsChange Returns a function to mutate the `metafields` property of the checkout. ### UseApplyMetafieldsChangeGeneratedType Returns a function to mutate the `metafields` property of the checkout. #### Returns: (change: MetafieldChange) => Promise export function useApplyMetafieldsChange< Target extends RenderExtensionTarget = RenderExtensionTarget, >(): (change: MetafieldChange) => Promise { const api = useApi(); if ('applyMetafieldChange' in api) { return api.applyMetafieldChange; } throw new ExtensionHasNoMethodError( 'applyMetafieldChange', api.extension.target, ); } ### MetafieldChange MetafieldRemoveChange | MetafieldUpdateChange | MetafieldRemoveCartChange | MetafieldUpdateCartChange ### MetafieldRemoveChange Removes a metafield. ### key The name of the metafield to remove. ### namespace The namespace of the metafield to remove. ### type The type of the `MetafieldRemoveChange` API. ### Metafield Metadata associated with the checkout. ### key The name of the metafield. It must be between 3 and 30 characters in length (inclusive). ### namespace A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. This must be between 2 and 20 characters in length (inclusive). ### value The information to be stored as metadata. ### valueType The metafield’s information type. ### MetafieldUpdateChange Updates a metafield. If a metafield with the provided key and namespace does not already exist, it gets created. ### key The name of the metafield to update. ### namespace The namespace of the metafield to add. ### type The type of the `MetafieldUpdateChange` API. ### value The new information to store in the metafield. ### valueType The metafield’s information type. ### MetafieldRemoveCartChange Removes a cart metafield. ### key The name of the metafield to remove. ### namespace The namespace of the metafield to remove. ### type The type of the `MetafieldRemoveChange` API. ### CartMetafield Represents a custom metadata attached to a resource. ### key The key name of a metafield. ### namespace The namespace for a metafield. ### type The metafield's type name. ### value The value of a metafield. ### MetafieldUpdateCartChange Updates a cart metafield. If a metafield with the provided key and namespace does not already exist, it gets created. ### metafield ### type The type of the `MetafieldUpdateChange` API. ### MetafieldChangeResult MetafieldChangeResultSuccess | MetafieldChangeResultError ### MetafieldChangeResultSuccess ### type The type of the `MetafieldChangeResultSuccess` API. ### MetafieldChangeResultError ### message A message that explains the error. This message is useful for debugging. It is **not** localized, and therefore should not be presented directly to the buyer. ### type The type of the `MetafieldChangeResultError` API. ## Related - [Targets](/docs/api/checkout-ui-extensions/targets) - [Components](/docs/api/checkout-ui-extensions/components) - [Configuration](/docs/api/checkout-ui-extensions/configuration) - [Tutorials](/apps/checkout)