Skip to main content

Add product data using the new product model

Legacy

The REST Admin API is a legacy API as of October 1, 2024. All apps and integrations should be built with the GraphQL Admin API. For details and migration steps, visit our migration guide.

After you understand the new product model, you can use it to interact with products, variants, and options in a store.


In this guide, you'll learn how to create a new product with multiple options, option values, and variants, using either a single mutation or multiple mutations.

You'll also learn the workflow for creating a product and its variants and operations using updated product operations.

Note

If your app or use case aligns with the database sync workflow, then use the productSet mutation to add data in a single operation.


You want to create a product with the following data:

  • Two options:

    • Color
    • Size
  • Each option has three option values:

    • Color has Red, Green, and Blue
    • Size has Small, Medium, and Large
  • Each option value has a variant, for nine product variants total, representing all possible combinations of option values. For example:

    • Red / Small
    • Blue / Large

Anchor to Shape of the product dataShape of the product data

The following diagram represents the desired shape of the product data:

The relationship between option value, variant, option, and values, which comprise data for a product

The diagram displays the "OptionValues" that are assigned to each variant, and the "Values" that belong to each "Option" relationship of the product's variants to their option values. On the right, there's the relationship of the product's options to their option values. The values that are represented in "OptionValues" correspond with a value in the product's options.

The following is the corresponding data structure:

Product data structure

{
"id": "gid://shopify/Product/456",
"title": "My Cool Socks",
"variants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/20",
"title": "Red / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Small"
}
]
}
},
{
"node": {
"id": "gid://shopify/ProductVariant/21",
"title": "Green / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Small"
}
]
}

Anchor to Several mutations to create a product with options and variantsSeveral mutations to create a product with options and variants

To create a product with incremental mutations, run the productCreate mutation to create a standalone product variant and the productVariantsBulkCreate mutation to overwrite this data.

Anchor to Create the standalone variantCreate the standalone variant

The following example creates a product with color and size options, including the options' values. The productCreate mutation creates the product, the options, the option values, and a single variant that's assigned the first combination of option values. This is the standalone variant.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation CreateProductWithOptions($productInput: ProductInput!) {
productCreate(input: $productInput) {
product {
id
title
description
options {
id
name
position
optionValues {
id
name
hasVariants
}
}
variants(first: 250) {
edges {
node {
id
title
price
sku
}
}
}
}
userErrors {
field
message
}
}
}

Arguments

{
"productInput": {
"title": "My cool socks",
"productOptions": [
{
"name": "Color",
"values": [
{ "name": "Red" },
{ "name": "Green" },
{ "name": "Blue" }
]
},
{
"name": "Size",
"values": [
{ "name": "Small" },
{ "name": "Medium" },
{ "name": "Large" }
]
}
]
}
}

JSON response

{
"data": {
"productCreate": {
"product": {
"id": "gid://shopify/Product/456",
"title": "My cool socks",
"description": "",
"options": [
{
"id": "gid://shopify/ProductOption/101",
"name": "Color",
"position": 1,
"optionValues": [
{
"id": "gid://shopify/ProductOptionValue/1",
"name": "Red",
"hasVariants": true
},
{
"id": "gid://shopify/ProductOptionValue/2",
"name": "Green",
"hasVariants": false
},
{
"id": "gid://shopify/ProductOptionValue/3",
"name": "Blue",
"hasVariants": false
}
]
},
{
"id": "gid://shopify/ProductOption/102",
"name": "Size",
"position": 2,
"optionValues": [
{
"id": "gid://shopify/ProductOptionValue/4",
"name": "Small",
"hasVariants": true
},
{
"id": "gid://shopify/ProductOptionValue/5",
"name": "Medium",
"hasVariants": false
},
{
"id": "gid://shopify/ProductOptionValue/6",
"name": "Large",
"hasVariants": false
}
]
}
],
"variants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/101",
"title": "Red / Small",
"price": "0.00",
"sku": ""
}
}
]
}
},
"userErrors": []
}
}
}

In this example, the standalone variant has a Color value of Red and a Size value of Small. The title is automatically set to Red / Small.

When options have more than one optionValue, the productCreate mutation sets the first variant to have the first option value and retains all other option values as orphaned option values.

Tip

Orphaned option values still have a GID associated with them. In subsequent requests, you can reference orphaned option values by ID, such as for data integrity purposes, local storage, and reflection in the UI.

After running the mutation successfully and obtaining the product ID, you can use the productVariantsBulkCreate mutation to create the remaining product variants and map them to the existing options.

You can create up to 250 variants in a single batch using this mutation. In cases where you need to create a higher volume of variants, you need to run the mutation multiple times. Learn how to use fragments and cursor-based pagination to help do this efficiently.

To return only the variants that were added, and not all of the product variants, the example uses the productVariants field.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variantsInput) {
productVariants {
id
title
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}

Arguments

{
"productId": "gid://shopify/Product/456",
"variantsInput": [
{
"price": 4.99,
"optionValues": [
{ "name": "Red", "optionName": "Color"},
{ "name": "Medium", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Red", "optionName": "Color"},
{ "name": "Large", "optionName": "Size"}
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Green", "optionName": "Color" },
{ "name": "Small", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Green", "optionName": "Color" },
{ "name": "Medium", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Green", "optionName": "Color" },
{ "name": "Large", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Blue", "optionName": "Color" },
{ "name": "Small", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Blue", "optionName": "Color" },
{ "name": "Medium", "optionName": "Size" }
]
},
{
"price": 4.99,
"optionValues": [
{ "name": "Blue", "optionName": "Color" },
{ "name": "Large", "optionName": "Size" }
]
}
]
}

JSON response

{
"data": {
"productVariantsBulkCreate": {
"productVariants": [
{
"id": "gid://shopify/ProductVariant/102",
"title": "Red / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/103",
"title": "Red / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Large"
}
]
},
{
"id": "gid://shopify/ProductVariant/104",
"title": "Green / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Small"
}
]
},
{
"id": "gid://shopify/ProductVariant/105",
"title": "Green / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/106",
"title": "Green / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Large"
}
]
},
{
"id": "gid://shopify/ProductVariant/107",
"title": "Blue / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Small"
}
]
},
{
"id": "gid://shopify/ProductVariant/108",
"title": "Blue / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/109",
"title": "Blue / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Large"
}
]
}
],
"userErrors": []
}
}
}

Anchor to Overwrite the standalone variantOverwrite the standalone variant

To overwrite the standalone variant that was created in the initial productCreate mutation, run the productVariantsBulkCreate mutation with a strategy of REMOVE_STANDALONE_VARIANT.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation productVariantsBulkCreate {
productVariantsBulkCreate(
productId: "gid://shopify/Product/1234",
strategy: REMOVE_STANDALONE_VARIANT,
variants: [
# Variants to be created
]
)
}

JSON response

{
"data": {
"productVariantsBulkCreate": {
"productVariants": [
{
"id": "gid://shopify/ProductVariant/101",
"title": "Red / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/102",
"title": "Red / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/103",
"title": "Red / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "Large"
}
]
},
{
"id": "gid://shopify/ProductVariant/104",
"title": "Green / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Small"
}
]
},
{
"id": "gid://shopify/ProductVariant/105",
"title": "Green / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/106",
"title": "Green / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Green"
},
{
"name": "Size",
"value": "Large"
}
]
},
{
"id": "gid://shopify/ProductVariant/107",
"title": "Blue / Small",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Small"
}
]
},
{
"id": "gid://shopify/ProductVariant/108",
"title": "Blue / Medium",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Medium"
}
]
},
{
"id": "gid://shopify/ProductVariant/109",
"title": "Blue / Large",
"selectedOptions": [
{
"name": "Color",
"value": "Blue"
},
{
"name": "Size",
"value": "Large"
}
]
}
],
"userErrors": []
}
}
}

Anchor to API version 2024-01 and lowerAPI version 2024-01 and lower

If you're using API version 2024-01 and lower, and you need to create a product with many variants that are active at several locations, especially with a lot of collections and tags, then you should first create the product with just the variants.

After the product is created, you can activate the variants at locations and add the other related objects to the product. This reduces the size of each mutation and increases the likelihood that it will complete before the operation times out.

The following example shows how you might break up product creation and object association into multiple steps:

  1. Create the product with variants. Don't specify any tags or collections on the product, and don't specify inventory quantities for each variant.

  2. After the product is created, add tags to the product using the tagsAdd mutation, and add collections using the collectionsAddProducts mutation.

  3. Use the inventoryBulkToggleActivation mutation on each inventory item to activate it at the appropriate locations.

  4. After activating the variants at the locations, adjust inventory quantities for the inventory items using the inventoryAdjustQuantities mutation.


Edit data

Learn how to edit product data, including variants and options.

Sync data

Learn how you can sync product data from an external source into Shopify.


Was this page helpful?