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.

Creating metafields on product via REST api

Creating metafields on product via REST api

BillyH
Visitor
1 0 0

We are having issues using the metafields on a product on the /admin/api/2020-07/products.json rest api endpoint.
If "metafields" is either not included, or is set to an empty array, the product is creating without issue.
However when trying to create a product with metafields we are receiving the following error :

{"errors":{"metafields":"expected Hash to be a Array"}}


This is happening on both creates and updates.

 

Sample request payload : 

{
    "product": {
        "product_type": "Physical Product",
        "title": "Metafield Example Product",
        "body_html": "",
        "variants": [],
        "images": [],
        "tags": "",
        "metafields": [
            {
                "key": "option_one",
                "field": "metaOptionOne",
                "value": "Wishlist potential: High",
                "namespace": "custom_fields",
                "value_type": "string"
            },
        ],
        "published": true,
    },
}

 

Replies 2 (2)

edaroit
Shopify Staff
2 1 1

Hello @BillyH,

I've tried to replicate the issue without success. Looking at the application logs, I've noticed four 400 Bad Request from Sep 22 through Sep 24 on this endpoint.

These bad requests and the error message you've received can be explained if the payload sent metafields as a JSON object rather than an array.

Payload:

 

{
	"product": {
		"product_type": "Physical Product",
		"title": "Metafield Example Product",
		"body_html": "",
		"variants": [],
		"images": [],
		"tags": "",
		"metafields": {
			"key": "option_one",
			"field": "metaOptionOne",
			"value": "Wishlist potential: High",
			"namespace": "custom_fields",
			"value_type": "string"
		},
		"published": true
	}
}

 


Response:

 

{
  "errors": {
    "metafields": "expected Hash to be a Array"
  }
}

 

 

 

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

PaulNewton
Shopify Partner
7721 678 1620

Welcome the forums @edaroit  strong start hitting it out the park with a first post.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org