Trying to update a defined metafield via API

I am using Shopifys API to succesfully create new products on Shopify. I am trying to add a step in the workflow to also add the metafields for the product.

The metafields have the namespace “my_fields”.

As an example, I have a metafield with the key: “gender”. If I use the POST API to add the value “mens” to this metafield I get this error: “Webhook HTTP request failed with exception The remote server returned an error: (406) Not Acceptable.”

The metafield “gender” is already defined in my shopify store. If I run the exact same POST request to a an undefined metafield, i.e. “gendering” for example, the request is successful and I can see the metafield has been added, although does only show in the unstructured metafields section.

I’m sure i’m just missing something here, but why can’t I successfully POST the metafield value to a defined metafield such as “gender”?

I thought that maybe I needed to use a PUT request with the metafield ID for gender, but when I ran a GET request it came back with no results. I’m assuming because none of the metafields for this product have any values yet.

Any help much appreciated.

Hi @bundl

Sounds like the issue is related to Shopify’s validation rules for defined metafields :slightly_smiling_face: Since “gender” is a predefined metafield, it expects the correct format and type. Here are a few things to check:

  1. Check the metafield type – Ensure the data type you are sending (string, integer, list, etc.) matches the defined metafield type in Shopify. A mismatch can cause a 406 Not Acceptable error.
  2. Use PUT for updates – If the metafield is already defined but has no value, you likely need to use a PUT request instead of POST. A POST request creates a new metafield, whereas a PUT updates an existing one.
  3. Ensure you are targeting the correct resource – Since your GET request returns no results, verify you are querying the correct product ID and namespace (my_fields).
  4. Manually add a value in Shopify first – If the metafield is defined but has never been used, Shopify might not return it via API. Try setting a value manually in Shopify, then run a GET request to find its metafield ID and update it using PUT.
  5. Check API permissions – Ensure your API has write_products and write_metafields permissions, as Shopify restricts certain actions based on API scope.

If all else fails, try creating a new defined metafield with a different key and test if the issue persists :slightly_smiling_face: