Rename the filename of a product image, programmatically

If I wanted to rename the files of product images, the only place I’ve found so far is the image editor linked from the media section of the product page in admin.

Is the same functionality available anywhere else? Or through an API? or a csv upload?
(btw: matrixify doesn’t do it)

I’d love to automate file renaming after uploading based on certain parameters.
Is this possible?

Thanks

Hello! The fileUpdate GraphQL mutation would enable you to update the name of a file. Please refer to shopify.dev for API docs and examples.

1 Like

That worked. Thanks!
The Assistant bot threw me off initially.

1 Like

I can update the alt information using fileUpdate, but when I update filename, it prompts me: Error: Field ‘filename’ doesn’t exist on type ‘File’. Can you tell me what the problem is? Thank you very much.

##########################
##########################
def update_image_name(file_id, new_alt, new_name):
query = “”"
mutation FileUpdate($input: [FileUpdateInput!]!) {
fileUpdate(files: $input) {
userErrors {
code
field
message
}
files {
alt
filename
}
}
}
“”"

variables = {
    'input': [
        {
            'id': file_id,
            'alt': new_alt,
            'filename': new_name
        }
    ]
}

:wave: can you please share the request_id so that we can help further

hey there, sorry for the confusion. I see a few things that need changing with the API call you are making.

  1. Unfortunately we do not currently expose filename in the File GraphQL type. However, you can query the image’s url and see the new filename reflected in the URL
mutation {
  fileUpdate(files: [{
    id: "[ID]",
    filename: "new_name.jpg",
  }]) {
    files {
      id
      ... on MediaImage {
        image {
          url
        }
      }
    }
  }
}
  1. In the input to fileUpdate, the filename value needs to include the file extension (.jpg, .png etc.) as well. In your example above, you have filename: new_name, it should probably be something like filename: new_name.jpg (or whatever the extension is).

I hope this helps