Unable to upload image for variant

I am attempting to create a variant using the mutation productVariantsBulkCreate and include image data within the media object. However, the image is not uploading to the media object. Could you please advise on how to properly upload the image?

{
“productId”: “gid://shopify/Product/7713411891262”,
“strategy”: “REMOVE_STANDALONE_VARIANT”,
“variants”: [
{
“price”: 62.0,
“compareAtPrice”: 10.0,
“barcode”: “VA_UPC_1234527”,
“taxable”: false,
“mediaSrc”: [
https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Flag_of_India.svg/800px-Flag_of_India.svg.png
],
“inventoryItem”: {
“measurement”: {
“weight”: {
“unit”: “GRAMS”,
“value”: 10.0
}
},
“requiresShipping”: true,
“sku”: “FAA_GQL_VARIANT-6”,
“tracked”: true,
“cost”: “25.00”
},
“inventoryQuantities”: {
“availableQuantity”: 100,
“locationId”: “gid://shopify/Location/16655024190”
},
“optionValues”: [
{
“name”: “Yellow”,
“optionName”: “Color”
}
]
}
],
“media”: [
{ “alt”: null,
“mediaContentType”: “IMAGE”,
“originalSource”: “https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/the-blue-angels-aerobatic-flight-team-performs-during-fleet-news-photo-868260132-1555957865.jpg
}
]
}

Any chance you figured this out? I am having the same issue.

Hi Webgility,

The productVariantsBulkCreate mutation is indeed used to create multiple variants, but it does not directly handle media uploads. Instead, you should use the productCreateMedia mutation to upload images and associate them with a product.

Here’s a step-by-step guide on how to achieve this:

  1. Upload the Image: First, you need to upload the image using the productCreateMedia mutation. This mutation allows you to upload media files and associate them with a product.
   mutation CreateProductMedia($media: [CreateMediaInput!]!) {
     productCreateMedia(media: $media) {
       media {
         id
         alt
         mediaContentType
         preview {
           status
         }
       }
       userErrors {
         field
         message
       }
     }
   }
  1. Create Variants: After uploading the media, use the productVariantsBulkCreate mutation to create the variants. You can reference the media ID obtained from the previous step to associate the image with the variants.
   mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) {
     productVariantsBulkCreate(productId: $productId, variants: $variantsInput) {
       productVariants {
         id
         title
         selectedOptions {
           name
           value
         }
         media {
           nodes {
             id
           }
         }
       }
       userErrors {
         field
         message
       }
     }
   }

Example variables for creating variants:

   {
     "productId": "gid://shopify/Product/456",
     "variantsInput": [
       {
         "price": 19.99,
         "optionValues": [
           { "name": "Red", "optionName": "Color" },
           { "name": "Medium", "optionName": "Size" }
         ],
         "mediaIds": ["gid://shopify/MediaImage/123456789"]
       }
     ]
   }

Hope this helps!

I am Creating product by productCreate mutation in which there is media object present and I am sending product image and variant images over there. Now suppose If I have 4Variants and each have images then How would I link which mediaId is for which variant only by mediaId.

Do I need to run productCreateMedia mutation for each and every Variant first and then run mutation for Create Variant productVariantsBulkCreate .

Variant1
mutation productCreateMedia
mutation productVariantsBulkCreate

Variant2
mutation productCreateMedia
mutation productVariantsBulkCreate

Variant3
mutation productCreateMedia
mutation productVariantsBulkCreate

I just started going through this. productCreateMedia is depreciated in 2024-10. I am working on the best process to go about this tonight / this week. I will keep everyone posted on how I go about this.

For now, I am going to check out this thread and test what they are saying:

1 Like

productCreateMedia mutation is unfortunately deprecated. I am using productUpdate mutation instead.

To link each mediaId to its corresponding variant when using the productCreate mutation, the following approach can be applied:

If the alt is identical for each media, you can use the following approach:

  1. Retrieve Media IDs: After the productCreate mutation executes, you’ll receive the media objects in the response. Use the alt field as a reference to identify which mediaId corresponds to which variant.
  2. Map Media IDs: Create a mapping (e.g., a dictionary or object) where the alt values serve as keys and their corresponding mediaId values from the response act as the values.
  • Example: { "gray-helmet-for-bikers": "gid://shopify/MediaImage/12345", "black-helmet-for-bikers": "gid://shopify/MediaImage/67890" }
  1. Assign Media to Variants: When creating the variants, use the alt value from the variant details to look up the corresponding mediaId from the mapping created in Step 3.