skip to Main Content

I’m trying to use Shopify’s python API to get variant image url, I have received image_id and trying to do Image.find but I receive error, please help!

image = shopify.Image.find(variant.image_id)
image_url = image.scr

2

Answers


  1. Chosen as BEST ANSWER

    image = shopify.Image.find(variant.image_id, product_id=product.id)


  2. If you already have the Product resource, then you have all the data you need without making an extra API call to Shopify. Simply take the image_id from the variant you want and loop through the images on product.images to see which one matches.

    Example:

    image_id = product.variants[0].image_id # Or however you pick the variant
    
    image_src = None
    for image in product.images:
        if image.id == image_id:
            image_src = image.src
            break
    

    Or, to wrap it into a function:

    def image_src_by_id(product, image_id):
        image_src = None
        for image in product.images:
            if image.id == image_id:
                image_src = image.src
                break
        return image_src
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search