How to add a preset metafield to a product via API in Shopify?

How to add a preset metafield to a product via API in Shopify?

threestarjaacie
New Member
5 0 0

I have encountered a problem while trying to add a pre-set metafield to a product via API in my store. 
Below are the details of the issue:

  1. Metafield Creation:

    • I created a Product Metafield in my Shopify store.
    • Metafield Type: Single Line Text Field
    • Setting Enabled: Limit to Preset Choices
  2. API Request:

    • I attempted to add this metafield to a product using the following API request:
      jsonCopy code
      { "metafield": { "namespace": "custom", "key": "era", "type": "list.single_line_text_field", "value": [ "40s" ] } }
  3. Error Response:

    • I received the following error response:
      jsonCopy code
      { "errors": { "value": [ "can't be blank." ] } }

 

Here's my request:

url -X POST "<store>/admin/api/2023-10/products/<id>/metafields.json" -H "X-Shopify-Access-Token: <token>" -H "Content-Type: application/json" -d '{"metafield": {"namespace": "custom","key": "Era","type": "list.single_line_text_field","value":["40s"]}}'

I tried posting different type:
{
"errors": {
"type": ["'single_line_text_field' must be consistent with the definition's type: 'list.single_line_text_field'."],
"value": [" does not exist in provided choices: [\"40s\", \"50s\", \"60s\", \"70s\", \"80s\", \"90s\", \"2000s\"]."]
}
}



 

Replies 6 (6)

threestarjaacie
New Member
5 0 0
I have done get request for product which I manually in Shopify UI added metafields

"namespace": "custom",
"key": "era",
"id": 42597399298391,
"value": "[\"90s\",\"70s\",\"80s\"]",
"description": null,
"created_at": "2023-10-16T17:29:34+02:00",
"updated_at": "2023-10-16T17:29:34+02:00",
"owner_resource": "product",
"type": "list.single_line_text_field",

wil_dev
Shopify Partner
12 0 2

Hi,

 

https://shopify.dev/docs/api/admin-graphql/2023-10/objects/Metafield

 

value
String!

The data stored in the metafield. Always stored as a string, regardless of the metafield's type.

 

I think your value is an array, so you need to remove the  [ ] in your value ?

 

 

threestarjaacie
New Member
5 0 0

Thank you for reply. 
I tried this before and it does not work. Response:

{
"errors": {
"value": ["must be an array."]
}
}


threestarjaacie
New Member
5 0 0

Also: see Shopify instructions for lists.

list.single_line_text_fieldA list of single-line text fields.
[ 
"VIP shipping method",
"Standard shipping method"
]
yesno


EasifyApps-Zoe
Shopify Partner
582 15 49

Hi @threestarjaacie,

If you're looking to implement custom fields in your Shopify store without the need for coding, I'd recommend exploring the Easify Product Options app. It offers a Free plan and makes setting up custom options a breeze, far more efficient than working with Shopify metafields 😊.

EasifyAppsZoe_0-1697600851179.png

EASIFY - MAKING SHOPIFY SIMPLE & SWEET!
Easify Product Options: Create custom product options 10X faster & easier!
Easify Product Attachments: Effortlessly add downloadable PDF files (or any other format) to Shopify pages!
Try for Free | 24/7 Live Chat Support
threestarjaacie
New Member
5 0 0

Can I also post these fields via API?
In general it does not solve/answer my question. Still hoping on some answer.