Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

REST Api: unable to update product's metafields

Solved

REST Api: unable to update product's metafields

egiuliani
Shopify Partner
7 0 4

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)
swalkinshaw
Shopify Staff
20 4 12

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.

 

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

View solution in original post

Replies 4 (4)

swalkinshaw
Shopify Staff
20 4 12

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.

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

egiuliani
Shopify Partner
7 0 4

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
20 4 12

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.

 

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

ReverseTurtle
Shopify Partner
2 0 0

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.