FROM CACHE - en_header

REST Api: unable to update product's metafields

Solved
egiuliani
Tourist
5 0 0

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?

Accepted Solution (1)

Accepted Solutions
swalkinshaw
Shopify Staff
Shopify Staff
20 4 10

This is an accepted solution.

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.

 

View solution in original post

Replies 3 (3)
swalkinshaw
Shopify Staff
Shopify Staff
20 4 10

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.

egiuliani
Tourist
5 0 0

I understand, so probably I'm using a wrong metafield Id.

I thought that the id was the one specified here:

egiuliani_0-1631027231556.png

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

 

swalkinshaw
Shopify Staff
Shopify Staff
20 4 10

This is an accepted solution.

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.