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.

How to link an existing media file to a product

How to link an existing media file to a product

stolle
Tourist
3 0 4

Hey!

Could someone please help me out - i can't find it. 
I'm creating products via the Admin API and would like to link/add the very same image to all of these products. 
The image is uploaded already to Shopify and i do have a  

gid://shopify/MediaImage/<id>

id. For me it looks like that all mutations around product and images do upload a new instance of the product. Even if i provide a url that is hosted on shopify.

How can i link/set an existing image to a product?

I'm fine with either Rest or Graphql admin api

Replies 8 (8)

stolle
Tourist
3 0 4

What i can also see using the Shopify UI directly there is this query:

{
  "operationName": "ProductFileUpdate",
  "variables": {
    "input": [
      {
        "id": "gid://shopify/MediaImage/12345",
        "referencesToAdd": [
          "gid://shopify/Product/123456"
        ]
      }
    ]
  },
  "query": "mutation ProductFileUpdate($input: [FileUpdateInput!]!) {\n  fileUpdate(files: $input) {\n    userErrors {\n      code\n      message\n      __typename\n    }\n    files {\n      id\n      __typename\n    }\n    __typename\n  }\n}\n"
}

 

but i can't find anything like this on Admin API. The `referencesToAdd` is missing everywhere.

LetterT
Shopify Partner
53 5 19

For GraphQL, it's seems overly complicated 😭, but here is an overview:

https://shopify.dev/docs/apps/online-store/media/products

 

I assume you're using productCreateMedia, but you should check this out:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productCreateMedia?example=Add+new+medi...

 

then you probably want to use productUpdateMedia

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productUpdateMedia?example=Update+a+pro...

 

you may need to re-order their position by productReorderMedia

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productreordermedia?example=Reorder+a+p...

 

... finally ... if you're dealing with variants, here's more reading 😢

https://shopify.dev/docs/apps/online-store/media/product-variants

 

good luck!

Don't be shy, click that like button!
stolle
Tourist
3 0 4

Hey @LetterT ! Thanks for the reply!
By reading the endpoints i don't think there is something available at all add an existing image to a product. The productCreateMedia only has an Input with
```

originalSource
String! required

The original source of the media object. This might be an external URL or a staged upload URL.
```
It will always create and upload a new media even if you provide a shopify URL.

The productUpdateMedia EP will only update existing media on a product but not allow linking an existing media to a new product.

Would you know if this is achievable via REST?

I'll be dealing with a 500-1000 products and more and would love to avoid to upload the very same image over and over 😅

 

 

LetterT
Shopify Partner
53 5 19

Actually I don't think all the mutations require an image to be uploaded. In fact I think they're very granular - I just wish it wasn't sooo complicated, but whatever ...

 

Assuming you've got through Step 1 of:

https://shopify.dev/docs/apps/online-store/media/products

(which actually has 2 distinct parts to it: the stagedUploadsCreate GraphQL mutation and a POST of the image) ... now you've got your resourceUrl

 

Taking this below as the starting point:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productCreateMedia?example=Add+new+medi...

but adding id to media { ... }, the goal is to get the id of the media created, which we'll call media_id (we'll need this later), (which you have).

 

 

 

mutation productCreateMedia($media: [CreateMediaInput!]!, $productId: ID!) {
  productCreateMedia(media: $media, productId: $productId) {
    media {
      mediaContentType
      status
      id
    }
    mediaUserErrors {
      field
      message
    }
    product {
      id
    }
  }
}

 

 

 

Input variables:

 

 

{
    "media": {
        "mediaContentType": "IMAGE",
        "originalSource": <PUT YOUR resourceUrl HERE>,
    },
    "productId": <PUT YOUR productId HERE>,
}

 

 

 

Now for the productUpdateMedia mutation:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productUpdateMedia?example=Update+a+pro...

 

The input variables don't need any more urls or anything else to be uploaded. It seems that all you need is your productId and the media_id

 

Plan A:

I haven't use productUpdateMedia myself ... but perhaps you can then use productUpdateMedia with media_id on all those 500-1000 productIds of your choosing?

 

Plan B:

If Shopify doesn't like that, then how about this:

There's the resourceUrl from Step 1. Perhaps you can just productCreateMedia on all productIds. Again, there's no necessity to POST any more images because you say you're using the same image, and the resourceUrl is the same.

 

Curious to see what works ...

Don't be shy, click that like button!

ES_AB
Shopify Partner
6 0 0

@stolle  Have you find out a solution, im having the same issue. Using Graphql admin api, and getting duplicate images over and over in my Shopify Files Center evertime i try to link images.

1.fileCreate

2.productCreateMedia, providing the URL from the media uploaded to shopify Content files

 

Tried  productUpdateMedia supplying mediaimageid from the images uploaded feom step 1. still no success.

 

Let us know if you found a solution

Pecko
Shopify Partner
1 0 0

Hi, I am dealing exactly with the same problem. How did you solve it? Thanks for help

johnnybravo
Shopify Partner
1 0 0

Someone please help!

Michel00
Visitor
3 0 0

I'm having the same issue.

I'd like to add images I already have in my library to my product, but it will create duplicates.

Seems like something that should be possible, right?

 

My suggestion:

How about letting the originalSource argument take shopify GIDs?

It'd look like this:

mutation UpdateProductImages {
  productUpdate(
    product: {
      id: "gid://shopify/Product/123412341234", 
      title: "New Title"
    },  
    media: [
      {
      originalSource: "gid://shopify/MediaImage/123412341234",
      mediaContentType: IMAGE,
      alt: "The beautiful image"
    }, {
      originalSource: "gid://shopify/MediaImage/123412341234",
      mediaContentType: IMAGE,
      alt: "The great image"
    }
    ]
  ) {
    product {
      title
    }
}