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.

422 error when trying to add/update metafields of a product variant

Solved

422 error when trying to add/update metafields of a product variant

daanvdj
Visitor
2 1 0

Hi!

 

So I'm trying to add/update the metafields that a product variant has.

 

My attempt at doing it through a PUT request:

 

  client.put({
          path: `variants/${response.body.variant.id}`,
          data: {"variant":{"id":`${response.body.variant.id}`,"metafields":[{"key":"new","value":"testingtheput","type":"single_line_text_field","namespace":"global"}]}},
          type: "application/json"
        }).then( response => {
          console.log(response);
        });
 
My attempt to do it through a POST request to add it is as follows:
 
      client.post({
        path: `products/${productId}/variants/${response.body.variant.id}/metafields`,
        data: {"metafield":{"namespace":"test","key":"testingpost","value":"testingthepost","type":"single_line_text_field"}},
        type: "application/json"
      }).then(response => {
        console.log(response);
      });
 
Both PUT and POST both return a 422 error, hinting at the metafield parameters not being correct. I specifically tried to follow the API doc examples for both types of request. The links for the docs used are:
https://shopify.dev/api/admin-rest/2021-10/resources/product-variant#[put]/admin/api/2021-10/variant... -> for the PUT of the variant (specifically "Add a metafield to an existing variant" )
 
 
I'm not sure what else I can try, so I'm hoping someone from this community might be able to help.
 
 
Accepted Solution (1)
daanvdj
Visitor
2 1 0

This is an accepted solution.

Hi Jason,

 

I was able to fix the issue with some good old pair programming with a colleague!

Thanks either way for jumping in on the issue.

I am using the nodeJs boilerplate app that one can generate by using the shopify CLI.

 

For future reference if people may be experiencing the same issue:

- Check what API version the boilerplate app is running on, mine was per default running on a super old version that isn't even referenced in the API docs anymore. You can find this setting in your 'server.js' file when using the NodeJs boilerplate:

 

Shopify.Context.initialize({
  API_KEY: process.env.SHOPIFY_API_KEY,
  API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
  SCOPES: process.env.SCOPES.split(","),
  HOST_NAME: process.env.HOST.replace(/https:\/\/|\/$/g, ""),
  API_VERSION: ApiVersion.October20,
  IS_EMBEDDED_APP: true,
  // This should be replaced with your preferred storage strategy
  SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
});
It uses an enum and the newest version that the enum has (april 21) is referenced in the API docs.  When I switched to the newer version and rechecked what values it was expecting for metafieldsm I found out that the older versions still use 'value_type' instead of 'type'. So if anyone is running into the same issue, try switching to a newer version of the API OR switch to using 'value_type' instead of 'type'. Below is an example of the difference:
 
  const data = {"metafield":{"namespace":"configuration","key":`${arr[0]}`,"value":`${arr[1]}`,"value_type":"string"}};
      const metafield = client.post({
        path: `products/${productId}/variants/${newVariant.body.variant.id}/metafields`,
        data: data,
        type: "application/json"
      }
Above is the correct way to format data for metafields in older versions ^^
 
 
 And below is the way that the most recent docs describe it.
 
  data: {"metafield":{"namespace":"test","key":"testingpost","value":"testingthepost","type":"single_line_text_field"}},
 
 Hope this possibly helps someone in the future!

 

View solution in original post

Replies 2 (2)

Jason
Shopify Partner
11207 226 2317

What tech stack are you using to make the API calls?

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
daanvdj
Visitor
2 1 0

This is an accepted solution.

Hi Jason,

 

I was able to fix the issue with some good old pair programming with a colleague!

Thanks either way for jumping in on the issue.

I am using the nodeJs boilerplate app that one can generate by using the shopify CLI.

 

For future reference if people may be experiencing the same issue:

- Check what API version the boilerplate app is running on, mine was per default running on a super old version that isn't even referenced in the API docs anymore. You can find this setting in your 'server.js' file when using the NodeJs boilerplate:

 

Shopify.Context.initialize({
  API_KEY: process.env.SHOPIFY_API_KEY,
  API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
  SCOPES: process.env.SCOPES.split(","),
  HOST_NAME: process.env.HOST.replace(/https:\/\/|\/$/g, ""),
  API_VERSION: ApiVersion.October20,
  IS_EMBEDDED_APP: true,
  // This should be replaced with your preferred storage strategy
  SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
});
It uses an enum and the newest version that the enum has (april 21) is referenced in the API docs.  When I switched to the newer version and rechecked what values it was expecting for metafieldsm I found out that the older versions still use 'value_type' instead of 'type'. So if anyone is running into the same issue, try switching to a newer version of the API OR switch to using 'value_type' instead of 'type'. Below is an example of the difference:
 
  const data = {"metafield":{"namespace":"configuration","key":`${arr[0]}`,"value":`${arr[1]}`,"value_type":"string"}};
      const metafield = client.post({
        path: `products/${productId}/variants/${newVariant.body.variant.id}/metafields`,
        data: data,
        type: "application/json"
      }
Above is the correct way to format data for metafields in older versions ^^
 
 
 And below is the way that the most recent docs describe it.
 
  data: {"metafield":{"namespace":"test","key":"testingpost","value":"testingthepost","type":"single_line_text_field"}},
 
 Hope this possibly helps someone in the future!