Save an image in a metafield using GraphQL/REST

Hi there!

I’m building an app, and I need to be able to store an image URL in a metafield on a product that I’m creating/editing.

I know for the product images, I need to post the base64 encoded image when going through REST, but I need the URL to be stored in the metafield.

So is there a way to either a) do this one step, or b) upload the image to the store’s / Shopify’s CDN, retrieve the URL, then post the url to the metafield?

Thanks!

Hey @LePong ,

While base64 encoded images are certainly more reliable, you don’t have to supply a base64 encoded image necessarily through the Product API. For example, here’s a REST call that would both create a product with an image and attach a new metafield to it at the same time (note the current image link doesn’t work, you’d need to replace it) :

{
  "product": {
    "title": "Burton Custom Freestyle 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
		 "metafields": [
      {
        "key": "img_url",
        "value":"http://example.com/rails_logo.gif",
        "value_type": "string",
        "namespace": "global"
      }
    ],
    "images": [
      {
        "src": "http://example.com/rails_logo.gif"
      }
    ]
  }
}

Hey Josh.

So I’m doing something similar to that in my product create flow, I create the product with GQL, then upload the image through REST cause I have the base 64 and not a URL. (I asked about this in this thread for context).

But in this case, I don’t want to upload a product image, I want an image in the metafields, which I use elsewhere on the page. So I can’t upload the image as a product image, and then use that URL.

Does that make sense?