Save an image in a metafield using GraphQL/REST

Save an image in a metafield using GraphQL/REST

LePong
Excursionist
25 1 19

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!

Replies 2 (2)

Josh
Shopify Staff
1134 84 234

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"
      }
    ]
  }
}

Josh | 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 the Shopify Help Center or the Shopify Blog

LePong
Excursionist
25 1 19

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?