Skip to main content

Attributes API guide

Permissions​

To edit and create attributes, a user needs to have the following permissions:

  • MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES
  • MANAGE_PAGE_TYPES_AND_ATTRIBUTES

To assign attributes to products and pages, a user needs only the following permissions: MANAGE_PRODUCTS and MANAGE_PAGES.

Input types​

Each attribute has an input type defined upon attribute creation. Input types determine the data type you can enter as the attribute values in the dashboard. Available input types are defined in the AttributeInputTypeEnum enum. Below is the list of available input types:

NameDescriptionExample
DROPDOWNList of predefined choices; rendered as a single-select dropdown.Store a color of a variant with predefined choices: orange, black, blue, etc.
MULTISELECTList of predefined choices; rendered as a multi-select dropdown.Add multiple tags to a product or a page.
FILEAllows to store a file as an attribute value; rendered as a file input.Store a product manual as a PDF file; store a hero image for a page.
REFERENCEValues are references to other entities such as products, variants, pages, categories or collections.Render a list of related products on a product page.
SINGLE_REFERENCEStores a reference to other entity such as product, variant, page, collection or category.Render a related collection on a product page.
NUMERICValues are numbers; optionally, a unit can be provided to represent measures and dimensions.Dimensions of a product represented with three numeric attributes: length, width, depth.
RICH_TEXTValue is stored as rich-text content; rendered as a rich-text editor.Additional content blocks for a page or product.
PLAIN_TEXTValue is stored as plain text; rendered as a text input.Stores unformatted text for simple labels, e.g., "Material: 100% Cotton"
SWATCHStores a color code or an image.Stores a color or an image to visually represent options like colors or patterns.
BOOLEANAllows storing boolean values.Yes/no properties, e.g. "Product is fair trade certified: yes/no".
DATEAllows storing date values.Store release date of a product.
DATE_TIMEAllows storing date time values.Store release date with time of a product.

Fetching all attributes​

To fetch the list of attributes, use the attributes query:

{
attributes(first: 5) {
edges {
node {
name
slug
inputType
entityType
referenceTypes {
...on ProductType {
id
slug
}
...on PageType {
id
slug
}
}
unit
choices(first: 5) {
edges {
node {
name
slug
}
}
}
}
}
}
}

The attributes field supports pagination and filtering, e.g., searching attributes by name. For each attribute, we fetch the following properties, some of which are used only with specific input types:

  • name - Name of the attribute, use it to render attribute in the frontend.
  • slug - A unique attribute handle you can use to fetch an attribute or reference it in filters.
  • inputType - Represents the input type.
  • entityType - Used only with REFERENCE and SINGLE_REFERENCE input types; represents the type of entities associated as references, e.g., PAGE or PRODUCT.
  • referenceTypes - Allowed target types for reference attributes. For attributes with the PRODUCT or PRODUCT_VARIANT entity type, this lists allowed product types; for attributes with the PAGE entity type, this lists allowed page types. Not supported for CATEGORY or COLLECTION reference attributes.
  • unit - Used only with NUMERIC input types; represents the optional measurement unit.
  • choices - Used only with DROPDOWN, MULTISELECT and SWATCH; a list of predefined choices you can use as values. The choices field supports pagination and filtering, e.g., searching choices by name.

Fetching assigned attributes​

Attributes assigned to an object are represented by the AssignedAttribute GraphQL interface. Below is an example of fetching the size attribute (of single-choice type) and multiple references to pages connected to the product.

{
product(slug: "colored-parrot-cushion", channel: "default-channel") {
name
size: assignedAttribute(slug: "size") {
... on AssignedSingleChoiceAttribute {
value {
slug
}
}
}
materialReferences: assignedAttribute(slug: "material-ref") {
... on AssignedMultiPageReferenceAttribute {
value {
title
}
}
}
}
}

Similarly, you can fetch assigned attributes for ProductVariant and Page types.

Filtering by attributes​

A user can use a slug to filter products by attributes. The query parameters can be encoded in URL and will be the same in all languages.

An example of a products query:

{
products(
first: 5
where: {attributes: [{slug: "bucket-size", value: {name: {eq: "25l"}}}]}
channel: "default-channel"
) {
edges {
node {
name
}
}
}
}

Response:

{
"data": {
"products": {
"edges": [
{
"node": {
"name": "Hyperspace Turquoise Paint"
}
},
{
"node": {
"name": "Light Speed Yellow Paint"
}
},
{
"node": {
"name": "Nebula Night Sky Paint"
}
},
{
"node": {
"name": "Red Dwarf Red Paint"
}
},
{
"node": {
"name": "Space Dust Navy Paint"
}
}
]
}
}
}

Setting allowed reference types​

The attribute’s inputType must be REFERENCE or SINGLE_REFERENCE, and entityType must be PRODUCT, PRODUCT_VARIANT, or PAGE to use referenceTypes.

Creating an attribute with allowed reference types​

Create a PRODUCT reference attribute with restricted product types with the use of attributeCreate mutation:

mutation {
attributeCreate(
input: {
name: "Related Products"
slug: "related-products"
type: PRODUCT_TYPE
inputType: REFERENCE
entityType: PRODUCT
referenceTypes: ["UHJvZHVjdFR5cGU6MQ=="]
}
) {
attribute {
id
referenceTypes {
...on ProductType {
id
slug
}
}
}
errors { field code message }
}
}

Create a PAGE single-reference attribute with restricted page types:

mutation {
attributeCreate(
input: {
name: "Related Page"
slug: "related-page"
type: PAGE_TYPE
inputType: SINGLE_REFERENCE
entityType: PAGE
referenceTypes: ["UGFnZVR5cGU6MQ=="]
}
) {
attribute {
id
referenceTypes {
...on PageType {
id
slug
}
}
}
errors { field code message }
}
}

Update allowed reference types​

Adjust the allowed product types for a PRODUCT reference attribute with the use of attributeUpdate mutation:

mutation {
attributeUpdate(
id: "QXR0cmlidXRlOjE=",
input: { referenceTypes: ["UHJvZHVjdFR5cGU6MQ==", "UHJvZHVjdFR5cGU6Mg=="] }
) {
attribute {
id
referenceTypes {
...on ProductType {
id
slug
}
}
}
errors { field code message }
}
}

Clear restrictions (disables type validation for future updates/new values; existing references are not modified):

mutation {
attributeUpdate(id: "QXR0cmlidXRlOjE=", input: { referenceTypes: [] }) {
attribute {
id
referenceTypes {
...on ProductType {
id
slug
}
}
}
errors { field code message }
}
}