Metafields API
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.
Anchor to Use casesUse cases
- 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.
Supported targets
- purchase.
address-autocomplete. format-suggestion - purchase.
address-autocomplete. suggest - purchase.
checkout. actions. render-before - purchase.
checkout. block. render - purchase.
checkout. cart-line-item. render-after - purchase.
checkout. cart-line-list. render-after - purchase.
checkout. chat. render - purchase.
checkout. contact. render-after - purchase.
checkout. delivery-address. render-after - purchase.
checkout. delivery-address. render-before - purchase.
checkout. footer. render-after - purchase.
checkout. header. render-after - purchase.
checkout. payment-method-list. render-after - purchase.
checkout. payment-method-list. render-before - purchase.
checkout. pickup-location-list. render-after - purchase.
checkout. pickup-location-list. render-before - purchase.
checkout. pickup-location-option-item. render-after - purchase.
checkout. pickup-point-list. render-after - purchase.
checkout. pickup-point-list. render-before - purchase.
checkout. reductions. render-after - purchase.
checkout. reductions. render-before - purchase.
checkout. shipping-option-item. details. render - purchase.
checkout. shipping-option-item. render-after - purchase.
checkout. shipping-option-list. render-after - purchase.
checkout. shipping-option-list. render-before - purchase.
thank-you. announcement. render - purchase.
thank-you. block. render - purchase.
thank-you. cart-line-item. render-after - purchase.
thank-you. cart-line-list. render-after - purchase.
thank-you. chat. render - purchase.
thank-you. customer-information. render-after - purchase.
thank-you. footer. render-after - purchase.
thank-you. header. render-after
Supported targets
- purchase.
address-autocomplete. format-suggestion - purchase.
address-autocomplete. suggest - purchase.
checkout. actions. render-before - purchase.
checkout. block. render - purchase.
checkout. cart-line-item. render-after - purchase.
checkout. cart-line-list. render-after - purchase.
checkout. chat. render - purchase.
checkout. contact. render-after - purchase.
checkout. delivery-address. render-after - purchase.
checkout. delivery-address. render-before - purchase.
checkout. footer. render-after - purchase.
checkout. header. render-after - purchase.
checkout. payment-method-list. render-after - purchase.
checkout. payment-method-list. render-before - purchase.
checkout. pickup-location-list. render-after - purchase.
checkout. pickup-location-list. render-before - purchase.
checkout. pickup-location-option-item. render-after - purchase.
checkout. pickup-point-list. render-after - purchase.
checkout. pickup-point-list. render-before - purchase.
checkout. reductions. render-after - purchase.
checkout. reductions. render-before - purchase.
checkout. shipping-option-item. details. render - purchase.
checkout. shipping-option-item. render-after - purchase.
checkout. shipping-option-list. render-after - purchase.
checkout. shipping-option-list. render-before - purchase.
thank-you. announcement. render - purchase.
thank-you. block. render - purchase.
thank-you. cart-line-item. render-after - purchase.
thank-you. cart-line-list. render-after - purchase.
thank-you. chat. render - purchase.
thank-you. customer-information. render-after - purchase.
thank-you. footer. render-after - purchase.
thank-you. header. render-after
Anchor to PropertiesProperties
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 appMetafieldsappMetafieldsappMetafieldsSubscribableSignalLike<AppMetafieldEntry[]>SubscribableSignalLike<AppMetafieldEntry[]>requiredrequired
The metafields requested in the
shopify.extension.tomlfile. 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
$appformat. The fully qualified reserved namespace format such asapp--{your-app-id}[--{optional-namespace}]isn't supported. See app owned metafields for more information.Requires access to protected customer data.
SubscribableSignalLike
Represents a reactive signal interface that provides both immediate value access and subscription-based updates. Enables real-time synchronization with changing data through the observer pattern. This interface extends `ReadonlySignalLike` with deprecated fields that are still supported for backwards compatibility.
- current
The current value of the signal. Equivalent to `.value`, accessing this property subscribes to changes when used in a reactive context.
T - destroy
Cleans up the subscription and releases any resources held by this signal. After calling `destroy()`, the signal stops receiving updates from the main thread.
() => Promise<void> - subscribe
Subscribes to value changes and calls the provided function whenever the value updates. Returns an unsubscribe function to clean up the subscription. Use to automatically react to changes in the signal's value.
(fn: (value: T) => void) => () => void - value
The current value of the signal. This property provides immediate access to the current value without requiring subscription setup. Use for one-time value checks or initial setup.
T
AppMetafieldEntry
An entry that pairs a Shopify resource with one of its [metafields](/docs/apps/build/custom-data/metafields). Each entry contains a `target` identifying which resource the metafield belongs to and a `metafield` object with the actual data.
- metafield
The metafield data, including the namespace, key, value, and content type. Use the `namespace` and `key` together to uniquely identify the metafield within its resource.
AppMetafield - target
The Shopify resource that this metafield is attached to, including the resource type (such as `'product'` or `'customer'`) and its globally-unique ID. {% 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`.
AppMetafieldEntryTarget
AppMetafield
Represents a custom [metafield](/docs/apps/build/custom-data/metafields) attached to a resource such as a product, variant, customer, or shop.
- key
The identifier for the metafield within its namespace, such as `'ingredients'` or `'shipping_weight'`.
string - namespace
The namespace that the metafield belongs to. Namespaces group related metafields and prevent naming collisions between different apps. App owned metafield namespaces are returned using the `$app` format. See [app owned metafields](/docs/apps/build/custom-data/ownership#reserved-prefixes) for more information.
string - type
The metafield's [type name](/docs/apps/build/custom-data/metafields/list-of-data-types), such as `'single_line_text_field'` or `'json'`. This is the full type identifier, whereas `valueType` is a simplified category.
string - value
The value of a metafield, stored as a string regardless of the underlying type. For JSON metafields, parse the string to access structured data.
string - valueType
The metafield's information type. - `'boolean'`: A true or false value. - `'float'`: A decimal number value. - `'integer'`: A whole number value. - `'json_string'`: A JSON-encoded string value. - `'string'`: A plain text value.
'boolean' | 'float' | 'integer' | 'json_string' | 'string'
AppMetafieldEntryTarget
The Shopify resource that a metafield is attached to. Each entry identifies a specific resource by its type and globally-unique ID, so you can trace where the data comes from.
- id
The globally-unique identifier of the Shopify resource, in [GID](/docs/api/usage/gids) format. Use this value to match the metafield to a specific resource in your extension or when querying the [Storefront API](/docs/api/storefront).
string - type
The kind of Shopify resource this metafield belongs to: - `'customer'`: The customer who placed the order. - `'product'`: A product in the merchant's catalog. - `'shop'`: The merchant's shop. - `'shopUser'`: A staff member or collaborator account on the shop. - `'variant'`: A specific variant of a product. - `'company'`: A [B2B](/docs/apps/build/b2b) company associated with the order. - `'companyLocation'`: A location belonging to a [B2B](/docs/apps/build/b2b) company. - `'cart'`: The cart associated with the checkout. {% include /apps/checkout/privacy-icon.md %} Requires level 1 access to [protected customer data](/docs/apps/store/data-protection/protected-customer-data) when the type is `customer`, `company` or `companyLocation`.
| 'customer' | 'product' | 'shop' | 'shopUser' | 'variant' | 'company' | 'companyLocation' | 'cart'
Anchor to MethodsMethods
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 applyMetafieldChangeapplyMetafieldChangeapplyMetafieldChange(change: MetafieldChange) => Promise<MetafieldChangeResult>(change: MetafieldChange) => Promise<MetafieldChangeResult>requiredrequired
Creates, updates, or removes a cart metafield on the checkout. On success, the
metafieldsproperty updates to reflect the change.Cart metafields are copied to order metafields at order creation time if there's a matching order metafield definition with the
cart to order copyablecapability enabled.NoteThis method returns an error if the cart instruction
is false, or the buyer is using an accelerated checkout method, such as Apple Pay or Google Pay.Note:This method returns an error if the cart instruction
is false, or the buyer is using an accelerated checkout method, such as Apple Pay or Google Pay.Note: This method returns an error if the <a href="/docs/api/checkout-ui-extensions/latest/apis/cart-instructions#properties-propertydetail-instructions">cart instruction</a> <code><span class="PreventFireFoxApplyingGapToWBR">metafields.can<wbr/>Set<wbr/>Cart<wbr/>Metafields</span></code> is false, or the buyer is using an accelerated checkout method, such as Apple Pay or Google Pay.
MetafieldChange
The input for `applyMetafieldChange()`. Use the `type` property to specify the operation. - `MetafieldRemoveCartChange` (`type: 'removeCartMetafield'`): Removes an existing cart [metafield](/docs/apps/build/custom-data/metafields). - `MetafieldUpdateCartChange` (`type: 'updateCartMetafield'`): Creates or updates a cart metafield.
MetafieldRemoveCartChange | MetafieldUpdateCartChangeMetafieldRemoveCartChange
Removes a cart [metafield](/docs/apps/build/custom-data/metafields). Pass this to `applyMetafieldChange()` to delete a metafield by key and namespace.
- key
The name of the metafield to remove.
string - namespace
The namespace of the metafield to remove.
string - type
Identifies this as a cart metafield removal. Set this when creating a change to delete an existing metafield by key and namespace.
'removeCartMetafield'
MetafieldUpdateCartChange
Creates or updates a cart [metafield](/docs/apps/build/custom-data/metafields). Pass this to `applyMetafieldChange()` to set a metafield value. If a metafield with the provided key and namespace doesn't already exist, then it gets created.
- metafield
The metafield data to set on the cart. If a metafield with this key and namespace already exists, then its value is replaced.
{ key: string; namespace?: string; value: string; type: string; } - type
Identifies this as a cart metafield creation or update. Set this when creating a change to set a metafield value.
'updateCartMetafield'
MetafieldChangeResult
The result of calling `applyMetafieldChange()`. Use the `type` property to determine whether the change succeeded or failed.
MetafieldChangeResultSuccess | MetafieldChangeResultErrorMetafieldChangeResultSuccess
The result of a successful metafield change. The `type` property is `'success'`.
- type
Indicates that the metafield change was applied successfully.
'success'
MetafieldChangeResultError
The result of a failed metafield change. Check the `message` property for details about what went wrong.
- message
A message that explains the error. This message is useful for debugging. It isn't localized and shouldn't be displayed to the buyer. Render your own localized error text rather than displaying this message to the buyer.
string - type
Indicates that the metafield change couldn't be applied. Check the `message` property for details.
'error'
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 useAppMetafields-parametersParameters
- Anchor to filtersfiltersfiltersAppMetafieldFiltersAppMetafieldFiltersDefault: {}Default: {}
AppMetafieldFilters
- id
string - key
string - namespace
To filter for app owned metafields, use the `$app` format. The fully qualified reserved namespace format such as `app--{your-app-id}[--{optional-namespace}]` is not supported. See [app owned metafields](/docs/apps/build/custom-data/ownership#reserved-prefixes) for more information.
string - type
'customer' | 'product' | 'shop' | 'shopUser' | 'variant' | 'company' | 'companyLocation' | 'cart'
Anchor to Best practicesBest practices
- Check cart instructions before writing: Use
instructions.metafields.canSetCartMetafieldsandinstructions.metafields.canDeleteCartMetafieldfrom the Cart Instructions API to verify that metafield operations are permitted before callingapplyMetafieldChange. - Use the
$appnamespace for app-owned data: Metafields under the$appnamespace are exclusively controlled by your app. Other extensions can't read or modify them, making this the safest place to store app-specific data.
Anchor to LimitationsLimitations
- Cart metafield changes aren't available when the buyer uses an accelerated checkout method such as Apple Pay or Google Pay.