Skip to main content

Attributes API

Use the Attributes API to read and write custom key-value pairs attached to the cart that persist through checkout. These key-value pairs are a lightweight way to keep buyer choices, operational details, and feature flags on the cart so that extensions and merchant workflows can all access the same values.

Attributes are shared across all extensions and the storefront. Any extension can read or overwrite attributes set by another.

Use metafields instead of attributes

To store custom data, Shopify recommends that you use the Metafields API to write cart metafields.

Metafields are safer than cart attributes as they support edit and view permissions and app-reserved namespaces to prevent other apps from reading and writing data.

  • Track buyer preferences: Store buyers' custom selections like gift wrapping or personalization options as key-value pairs on the cart.
  • Conditionally display UI: Show or hide features based on the cart's current attributes.
  • Store operational data: Capture purchase order numbers, reference codes, or other operational data that the merchant needs for accounting or shipping.
Support
Targets (33)

The shopify global object provides attribute data for the current checkout. Access the following properties on shopify to read custom key-value pairs attached to the cart. Available to purchase extension targets.

Anchor to attributes
attributes
<[]>
required

The custom key-value attributes attached to the cart or checkout. These are set by the buyer or by an extension using applyAttributeChange(). The list is empty if no attributes have been added.

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

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

Updates or removes an attribute on the cart and checkout. On success, the attributes property updates to reflect the change.

Note

This method returns an error if the cart instruction attributes.canUpdateAttributes 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.

The useAttributeValues hook returns an array of attribute values for the specified keys. Use this hook when you need to read the values of only some of the cart's attributes and you know the values' keys.

string[]
required

An array of attribute keys.

(string | undefined)[]

  • Check canUpdateAttributes before rendering mutation UI: Use the instructions property on the global shopify object to verify instructions.attributes.canUpdateAttributes before showing attribute controls. If it's false, hide the feature entirely.
  • Use descriptive, namespaced keys: Choose clear, unique attribute keys to avoid collisions with other extensions. For example, use myApp_giftWrap rather than gift.

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

Was this page helpful?