Can't get empty Shopify product/collection metafields ID

Hello,

I’m currently facing an issue: I want to perform a PUT request to update some metafields, but when a metafield is empty (i.e., hasn’t been created yet), I can’t retrieve its id.

Here’s an example:

In this case, I updated the title successfully, but the description remains the same as the product description — meaning it’s empty.

As a result, the response only includes the id of the title_tag metafield. The description_tag metafield is missing because it doesn’t exist yet.

What’s the best way to handle this ?
Should I check first if the metafield exists and then POST it if missing, or is there a better approach to ensure all metafields are always present?

Thanks in advance!

Check First, Then POST or PUT

  1. Check if the metafield exists (GET /admin/api/2024-01/metafields.json?namespace=global&key=description_tag)
  2. If it exists, update it via PUT.
  3. If it doesn’t, create it via POST.

:right_arrow: This is the safest and most scalable method.

Option 2: Always Try POST First with update=true

  • Shopify’s metafield POST request allows a parameter like update=true in some APIs (REST/GraphQL). This will create or update in one go if supported.

But! this may not work reliably for all metafield types or via REST, especially when used in n8n, so Option 1 is usually preferred.

Hope this helps

1 Like

Thanks for the answer but this is not working for me cause i can’t POST a title metafield for a product I tried everything but it still doesn’t work can you tell me how to post a new metafields title in n8n ?
Withe graphQL ? HTTP Request ? and what do you put in your query ?

If u wanna create a metafield for the title using GraphQL in n8n. You could try these then:

HTTP Request Node Setup

  • Method: POST
  • URL: https://your-store-name.myshopify.com/admin/api/2024-01/graphql.json
  • Authentication: Use HTTP Header with your X-Shopify-Access-Token
  • Headers:
  • {
    “Content-Type”: “application/json”,
    “X-Shopify-Access-Token”: “your_access_token”
    }

And then,
GraphQL Query to Create a Metafield
{
“query”: “mutation MetafieldCreate($input: MetafieldInput!) { metafieldCreate(input: $input) { metafield { id namespace key value type } userErrors { field message } } }”,
“variables”: {
“input”: {
“namespace”: “global”,
“key”: “title_tag”,
“value”: “Your Title Here”,
“type”: “single_line_text_field”,
“ownerId”: “gid://shopify/Collection/723425165694”
}
}
}

^^^^This one’s for adding a title_tag metafield to a collection

  • If the metafield already exists with that namespace/key pair for the owner, this will throw an error. So if you want to make it safer, you should first query to check existence, then:
    • Use metafieldCreate if it doesn’t exist
    • Use metafieldUpdate if it does

Use this to check the existence of metafield:
query {
collection(id: “gid://shopify/Collection/723425165694”) {
metafield(namespace: “global”, key: “title_tag”) {
id
value
}
}
}

2 Likes

Thanks a lot !!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.