How to get a file with its ID gid://shopify/MediaImage/XXX with an API query?

AndrewLeb
Tourist
44 0 2

How to get a file with its ID gid://shopify/MediaImage/XXX with an API query? This only works for product images, but I can't extract another image not assigned to products.

 

Admin API access scopeswrite_files, read_files, read_products
Webhook version2024-01
Replies 3 (3)

Liam
Shopify Staff
2700 289 739

Hi Andrew,

 

What's the full query you're trying to run, and what image file format are the images that are not product images?

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

AndrewLeb
Tourist
44 0 2

Hi,

 

Working now, just had to change permissions to write_files, read_files, read_products, read_themes, write_products

shayarigirl
Visitor
1 0 0

 

While there's no direct way to retrieve a file using its GID via a single API query, here are the workarounds you can use:

1. For Product Images:

  • Use the product images connection on a Product node in a GraphQL query:
GraphQL
content_copy
  • Filter the results based on the GID within your code.

2. For Non-Product Images:

a. Using the REST Admin API:

  • Perform a GET request to /admin/api/2024-01/files.json with the following parameters:
    • fields: id, src (or other desired fields)
    • asset_key: gid://shopify/MediaImage/XXX (replace with the actual GID)

b. Using GraphQL (with potential limitations):

  • Use the node query with the MediaImage type, but be aware that it might not work reliably for all images:
 

Key Considerations:

  • Admin API Access Scopes: Ensure your app has the necessary scopes: write_files, read_files, and read_products.
  • Webhook Version: Use the correct webhook version (2024-01 in your case).
  • Image Attachment: If the image is attached to a product, variant, or other resource, you might be able to retrieve it through that resource's API endpoint.

Additional Tips:

  • If you're still facing issues, consult Shopify's official documentation or seek help from their support team.
  • Consider using Shopify's API libraries or tools to simplify your interactions with the API.