Hi,
I am unable to update the metafield value when I update a product, below a simple example:
/admin/api/2021-07/products/{product_id}.json
{
"metafields":[
{
"key":"short_description",
"value":"TEST",
"value_type":"string",
"namespace":"information",
"id":34504878
}
],
"id":7315601883310
}
I get always this error:
{“(422 Unprocessable Entity) key: must be unique within this namespace on this resource”}
Can someone help me?
The error implies that you’re trying to create another metafield on the product (with id 7315601883310) with the identical namespace and key. Since you’re supplying a metafield id as well, my only guess is that the ID doesn’t match the existing one. If it did match, it should act as an update to the existing metafield which is fine.
Any individual resource (such as Product 7315601883310) can only have a single metafield under a namespace and key.
I understand, so probably I’m using a wrong metafield Id.
I thought that the id was the one specified here:
Instead can you confirm me that each metafield has a specific ID for each product? Do I have to retrieve it by making a call to the product get API?
Thanks so much
That ID in the URL is the metafield definition ID, not the metafield itself 
Instead can you confirm me that each metafield has a specific ID for each product?
Yes each metafield has its own unique ID, regardless of what resource/owner type it’s attached to.
Do I have to retrieve it by making a call to the product get API?
That’s the main way if you’re doing it all programatically. You’d call /products/<id>/metafields.json. You should be able to filter them with the namespace and key query params. See https://shopify.dev/api/admin/rest/reference/metafield#endpoints-2021-10
If you’d like to use GraphQL, we have some more powerful options like the metafieldsSet mutation which automatically creates or updates and doesn’t require an ID at all, which is simpler.
I feel like that’s what the key+namespace combo is for, if I have a parent ID and pass a key+namespace combo, that should be just as good as the ID. If it’s there, update it, if not add it.