Adding Metafields to a Product results in 422 Unprocessable Entity

Lightburn
Shopify Expert
4 0 1

Per the documentation here (http://docs.shopify.com/api/product), the following example should update an existing product's metafields:


PUT /admin/products/#{id}.json

{ "product": { "id": 632910392, "metafields": [ { "key": "new", "value": "newvalue", "value_type": "string", "namespace": "global" } ] } }


 

However, when I do the exact same thing, I receive a (422) Unprocessable Entity error.  What am I doing wrong?

My exact json string via PUT /admin/products/XXXXXX.json:

{

"product": {

"body_html": "Sample Product Descriptions",
"metafields": [

{

"key": "ModelNumber",
"value": "Test",
"value_type": "string",
"namespace": "global"

}

]

}

}
Replies 5 (5)

Lightburn
Shopify Expert
4 0 1

I believe I may have figured it out.  I was under the impression that metafields could be updated in this way, however it appears you can only create new metafields using this method.  Updating metafields appears to require the use of the metafields API.

awd
Shopify Staff (Retired)
83 0 8

Hello, are you trying to update existing metafields in your PUT request? Or are you trying to append new metafields in your PUT request?

To learn more visit the Shopify Help Center or the Community Blog.

Lightburn
Shopify Expert
4 0 1

I was hoping I could both insert and update metafields for a given product with a PUT to /admin/products/XXXXXX.json.  That was not the case, and I have adjusted my code to perform updates via a PUT to /admin/products/XXXXXX/metafields/YYYYYY.json.  My code is working now, however it would be nice if I could use the metafield key without needing to know the id in order to perform an update.  Given a key and a namespace, I would think this would be enough information to determine a unique record.

awd
Shopify Staff (Retired)
83 0 8

The PUT request to metafields you have mentioned above is a good alternative. However, you are using the ID of the metafield to make that request anyways, so why not save some API calls and include it in the Product request? (unless of course you are not updating the product as well). The IDs are required for updates to work properly. 

To learn more visit the Shopify Help Center or the Community Blog.

Lightburn
Shopify Expert
4 0 1

I absolutely understand that, but what I was alluding to was that it would be nice if we didn't need the metafield id in order to perform an update.  Because the metafield id is required, I either need to store the ids (I am updating multiple metafields at the same time) in hidden form fields and somehow associate those with the meta form fields so we can update the correct metafields on save, or perform a GET prior to the update/PUT to retrieve the appropriate IDs.  If I were instead able to send a namespace and key in place if the id, it would definitely be preferred.  It would be nice then if the API would be able to determine whether to perform the insert or update based on the existence of the namespace/key.

P.S.  Thank you for taking the time to respond.