Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

ProductImage API generates duplicated positions

ProductImage API generates duplicated positions

Klopp
Shopify Partner
17 1 2

Hi everyone,

I have an application developed that interacts with the Product Image REST API endpoint to assign external images to Shopify products. Sometimes it works, but something doesn't work for all images. 

For example, I need to assign 6 images to a Shopify product. I loop my array of images and use the following endpoint:

POST /admin/api/2020-07/products/PRODUCT_ID/images.json 
{
  "image": {
    "attachment": "IMAGE_BASE64_CODE",
    "filename": "IMAGE_NAME"
  }
}


The issue I have is related to the image position that is not correctly generated by Shopify. Those 6 calls are a success from Shopify's perspective, they returned an image URL containing the CDN image link, but the position is not always accurate as I expect it to be. For example, for a call, I can have something like this (Image 1 - position 1, Image 2 - position 2, Image 3 - position 3, Image 4 - position 4, Image 5 - position 5, Image 6 - position 6), but a lot of times I have duplicated positions (Image 1 - position 1, Image 2 - position 2, Image 3- position 2, Image 4 - position 3, Image 5 - position 3, Image 6 - position 4). Following this, only 4 images are assigned to the product and visible in the backend (image 1, image 3, image 5, and image 6).

I also tried to use the 'position' attribute, incrementing it for each addition (1, 2, 3, ...), but it doesn't work, having the same error. I think that maybe it's related to timing. Maybe it doesn't have enough time to process the image and its position.

Is there an issue with my approach?
Is there an issue with Shopify's API on this?
Do you think that using a later version of the API will make a difference? I use 2020-07.

Reply 1 (1)

Klopp
Shopify Partner
17 1 2

I'm also thinking that this might be due to the lack of time between the calls.

Having 6 product images, I do a foreach, calling the "/admin/api/2020-07/products/$id_product/images.json" endpoint on each iteration. It takes the first image, uploads it, and then it goes to the second one. If the previous image processing operation is not completed, then I think it takes the same position as the previous one, even if I sent the position parameter which is actually correct.

$query_arr = [
"image" => [
"position" => $image_position,
"attachment" => $base64,
"filename" => $filename
]
];