Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello,
we created a custom metafield in order to store images (list.file_reference type) attached to products. We don't this image to be Product images as they're different. We've uploaded them in the assets (MediaImages) as we didn't find a way to upload them through the REST API or through the GraphQL API.
However, we have retrieved the full list of MediaImages which give us the location of the MediaImage (this kind of link "gid://shopify/MediaImage/41589743616331"). But it seems that even though we followed the documentation of the API, it's not possible to create a link to this MediaImage in the metafield. We have +3000 products where we would like to link images to the custom metafield, which is too much to by doing one by one.
Here is my POST request through the REST API:
const ProductId = 7988994605279; const namespace = 'custom'; const key = 'photo_detouree'; const value = [ "gid://shopify/MediaImage/41589743616331" ]; const valueType = 'list.file_reference'; const accessToken = process.env.SHOPIFY_ACCESS_TOKEN; const storeName = process.env.SHOPIFY_STORE_NAME; fetch(`https://${storeName}.myshopify.com/admin/api/2023-04/products/${ProductId}/metafields.json`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': accessToken }, body: JSON.stringify({ metafield: { namespace: namespace, key: key, type: valueType, value: value } }) }).then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
When no resource is already in the metafield, I get a response :
{ errors: { value: [ "can't be blank." ] } }
We did try in PUT as well - it doesn't work neither, and when a resource already exists, we get the following answer:
{
metafield: {
owner_id: 7988XXXXXXX,
namespace: 'custom',
key: 'photo_detouree',
value: '["gid://shopify/MediaImage/41589743616331"]',
id: 311959XXXXXXX,
description: null,
created_at: '2023-04-19T19:19:59+02:00',
updated_at: '2023-04-19T19:19:59+02:00',
owner_resource: 'product',
type: 'list.file_reference',
admin_graphql_api_id: 'gid://shopify/Metafield/31195998224715'
}
}
So my questions are:
- is it possible to upload all the images to MediaImages through API?
- is it possible to add images to the custom metafield somehow (if it's not by re-using the gid location link as we tried)?
Thanks in advance!
Oly dev team
Did you ever get a solution to this?
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024