Admin API: How to link variants to images?

k-million
Shopify Partner
1 0 0

Hi, I'm using the Admin GraphQL API to create products in a store. 

According to the docs productCreate (shopify.dev) , I can use "mediaSrc" variant property to match a URL in the "images" array. But I haven't been able to make it work. 

 

This is my current input: 

 

 

  {
            "title": "Columbia Watch Cap II",
            "productType": "Beanie",
            "options": [
                "Color",
                "Size"
            ],
            "variants": [
                {
                    "sku": "1464091013",
                    "price": "25",
                    "compareAtPrice": "25",
                    "inventoryQuantities": [
                        {
                            "availableQuantity": 0,
                            "locationId": "gid://shopify/Location/81914921243"
                        }
                    ],
                    "inventoryItem": {
                        "tracked": true
                    },
                    "options": [
                        "Black, Black",
                        "SIN/TALLA"
                    ],
                    "mediaSrc": [
                        "https://columbia.kmillion.net/img/products/1464091_013_f.jpg"
                    ]
                },
                {
                    "sku": "1464091053",
                    "price": "25",
                    "compareAtPrice": "25",
                    "inventoryQuantities": [
                        {
                            "availableQuantity": 0,
                            "locationId": "gid://shopify/Location/81914921243"
                        }
                    ],
                    "inventoryItem": {
                        "tracked": true
                    },
                    "options": [
                        "Graphite, Tradewinds",
                        "SIN/TALLA"
                    ],
                    "mediaSrc": [
                        "https://columbia.kmillion.net/img/products/1464091_053_f.jpg"
                    ]
                },
            ],
            "images": [
                {
                    "src": "https://columbia.kmillion.net/img/products/1464091_013_f.jpg",
                    "altText": "Columbia Watch Cap II - Columbia"
                },
                {
                    "src": "https://columbia.kmillion.net/img/products/1464091_053_f.jpg",
                    "altText": "Columbia Watch Cap II - Columbia"
                },
                {
                    "src": "https://columbia.kmillion.net/img/products/1464091_271_f_p000.jpeg",
                    "altText": "Columbia Watch Cap II - Columbia"
                },
                {
                    "src": "https://columbia.kmillion.net/img/products/1464091_464_f.jpg",
                    "altText": "Columbia Watch Cap II - Columbia"
                },
                {
                    "src": "https://columbia.kmillion.net/img/products/1464091_858_f_p000.jpeg",
                    "altText": "Columbia Watch Cap II - Columbia"
                }
            ]
        }

I saw another question in the community that recommended using the "imageId" field, buy is now deprecated.

Any Ideas?.

 

Reply 1 (1)

Liam
Shopify Staff
2731 299 769

Hi K-Million,

 

The `mediaSrc` property in the variant is used to match a URL in the images array of the product, but you should be able to use it to associate a specific image with a variant. Can you check that the URLs in the `mediaSrc` of the variant exactly match the URLs in the `images` array. Even a small discrepancy, like a trailing slash, can cause a mismatch. Also, are you seeing any error messages returned in the GraphQL response?

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog