Hi there
,
I’m trying to bulk create product variants using Shopify’s GraphQL API. However, I’m running into a series of conflicting and confusing error messages when trying to configure optionValues, particularly when working with a linkedMetafieldValue. (shopify.color-pattern field for eg).
tldr;
- Encountered an error stating: “optionId or optionName must be specified.” during first attempt
- After adding optionName, received a new error: “id or name must be specified.”
- Included name, but the response then stated: “Cannot set name for an option value linked to a metafield.”
- Seeking clarification on the correct configuration for optionValues when utilizing a linkedMetafieldValue.
Further details
First Attempt
curl -X POST https://<store>.myshopify.com/admin/api/2025-01/graphql.json \
-H "Content-Type: application/json" \
-H "X-Shopify-Access-Token: <redacted>" \
-d '{
"query": "
mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
product {
id
}
userErrors {
field
message
code
}
}
}",
"variables":
{
"productId": "gid://shopify/Product/8737843085536",
"variants":
[
{
"barcode": "",
"inventoryItem":
{
"cost": 0,
"countryHarmonizedSystemCodes": null,
"measurement":
{
"weight":
{
"unit": "OUNCES",
"value": 2
}
},
"requiresShipping": true,
"tracked": true
},
"inventoryPolicy": "DENY",
"optionValues":
[
{
"name": "Selling Plans Ski Wax",
"optionName": "Title"
},
{
"linkedMetafieldValue": "gid://shopify/Metaobject/103203111136"
}
],
"price": "24.95",
"requiresComponents": false,
"taxCode": "",
"taxable": true
}
]
}
}'
Response
{
"data":
{
"productVariantsBulkCreate":
{
"product": null,
"userErrors":
[
{
"field":
[
"variants",
"0",
"optionValues",
"1"
],
"message": "optionId or optionName must be specified",
"code": "INVALID_INPUT"
}
]
}
}
}
Second Attempt
Alright, fine! I’ll add the optionID or optionName as suggested in the error message. I added the optionName
curl -X POST https://<store>.myshopify.com/admin/api/2025-01/graphql.json \
-H "Content-Type: application/json" \
-H "X-Shopify-Access-Token: <redacted>" \
-d '{
"query": "
mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
product {
id
}
userErrors {
field
message
code
}
}
}",
"variables":
{
"productId": "gid://shopify/Product/8737843085536",
"variants":
[
{
"barcode": "",
"inventoryItem":
{
"cost": 0,
"countryHarmonizedSystemCodes": null,
"measurement":
{
"weight":
{
"unit": "OUNCES",
"value": 2
}
},
"requiresShipping": true,
"tracked": true
},
"inventoryPolicy": "DENY",
"optionValues":
[
{
"name": "Selling Plans Ski Wax",
"optionName": "Title"
},
{
"linkedMetafieldValue": "gid://shopify/Metaobject/103203111136",
"optionName": "Color"
}
],
"price": "24.95",
"requiresComponents": false,
"taxCode": "",
"taxable": true
}
]
}
}'
Response
{
"data":
{
"productVariantsBulkCreate":
{
"product": null,
"userErrors":
[
{
"field":
[
"variants",
"0",
"optionValues",
"1"
],
"message": "id or name must be specified",
"code": "INVALID_INPUT"
}
]
}
}
}
Third Attempt
OK, I’ll add value id or name.
curl -X POST https://<store>.myshopify.com/admin/api/2025-01/graphql.json \
-H "Content-Type: application/json" \
-H "X-Shopify-Access-Token: <redacted>" \
-d '{
"query": "
mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
product {
id
}
userErrors {
field
message
code
}
}
}",
"variables":
{
"productId": "gid://shopify/Product/8737843085536",
"variants":
[
{
"barcode": "",
"inventoryItem":
{
"cost": 0,
"countryHarmonizedSystemCodes": null,
"measurement":
{
"weight":
{
"unit": "OUNCES",
"value": 2
}
},
"requiresShipping": true,
"tracked": true
},
"inventoryPolicy": "DENY",
"optionValues":
[
{
"name": "Selling Plans Ski Wax",
"optionName": "Title"
},
{
"linkedMetafieldValue": "gid://shopify/Metaobject/103203111136",
"optionName": "Color",
"name": "Red"
}
],
"price": "24.95",
"requiresComponents": false,
"taxCode": "",
"taxable": true
}
]
}
}'
Response
{
"data":
{
"productVariantsBulkCreate":
{
"product": null,
"userErrors":
[
{
"field":
[
"variants",
"0",
"optionValues",
"1"
],
"message": "Cannot set name for an option value linked to a metafield",
"code": "CANNOT_SET_NAME_FOR_LINKED_OPTION_VALUE"
}
]
}
}
}
To summarize:
- The first error suggested that I need to specify optionId or optionName.
- The second error said I need to specify id or name.
- The third error said I can’t set name for an option value linked to a metafield.
The error messages seem to contradict themselves. Am I missing anything obvious here? What would be the proper request payload for adding options with linkedMetafieldValue?
Thanks in advance!