Shopify API - Update A Metafied For A Product

Shopify API – Update A Metafied For A Product

Metafield is used to add more information or resource to the website. It can add to articles, products, customers, orders, and variants. 

Metafields have four required attributes:

  • Key: The name of the metafield. Minimum length: 3 characters. Maximum length: 30 characters.
  • Namespace: A container for a set of metafields. You need to define a custom namespace for your metafields to distinguish them from the metafields used by other apps. Minimum length: 2 characters. Maximum length: 20 characters.
  • Value: The information to be stored as metadata. Maximum length: 512 characters when metafield namespace is equal to tags and key is equal to alt.
  • Value_type: The metafield’s information type. Refer to the full list of types.

You can read Shopify API: How to Create Metafield for a Product to know how to create metafield for a product. 

Metafield is one of many complicated topics by many parameters to understand. You can flexibly use it for many different purposes. 

Today, Beehexa will show you how to update a metafield for a product using Postman in Shopify API. Let’s dive into this subject a little deeper. 

Step 1: Generate API Credentials From Shopify Admin

Follow the process in How to build a custom app for Shopify in 2022? to know how to access Shopify API. 

  • After creating an app, you need to remember the Admin API access token and the API key to update a metafield for a product using Postman. 

Step 2: Update A Metafield For A Product Using Postman

First, you need to collect the product ID and the metafield ID that you want to update.

You will copy these IDs in the response in the video on how to create metafield for a product that Beehexa showed you in the previous video.

Example: Product_id = 6794851549247

                 Metafield_id = 19502841069631

Note: The namespace and key of an existing metafield can’t be changed.

You can see the metafield that Beehexa created in the previous article. 

the code sample
Shopify API - Update A Metafied For A Product 5

In this case, we will change the value and type of the metafield.

Here are two old attributes:

"value": "https://drive.google.com/file/d/1aUzK8PQ7qrDvz6sc1YfuoBl7m9uj1nFQ/view", "type": "string"
Code language: JavaScript (javascript)

These are two new attributes that I want to update. 

“value”: "something new", "type": "single_line_text_field"
Code language: JavaScript (javascript)

Now, we are ready to update the metafield for a product using Postman. 

  • First, log in to your Postman and create a new workspace
  • Then, create a new PUT with this URL:
https://{API_key}:{admin_API_access_token}@{shop}.myshopify.com/admin/api/{api-version}/{resource}
Code language: JavaScript (javascript)
  1. {API_key} – The API key that you generated.
  2. {admin_API_access_token} – The admin API access token that you generated.
  3. {shop} – The name of your development store.
  4. {api-version} – The supported API version that you want to use.
  5. {resource} – A resource endpoint from the REST Admin API.

Or you can copy this example, remember change {resource} to {products/product_id/metafields/metafield_id}:

https://ed86d512a2c95387ffa25f67074a222b:[email protected]/admin/api/2021-10/products/6794851549247/metafields/19502841069631.json
Code language: JavaScript (javascript)
  • Paste the URL above.
update a metafield for a product
Shopify API - Update A Metafied For A Product 6

In the body section, enter this code. 

Payload:

{ "metafield": { "id": 19502841069631, "value": "something new", "type": "single_line_text_field" } }
Code language: JSON / JSON with Comments (json)
body section_update a metafield for a product
Shopify API - Update A Metafied For A Product 7

Then clicks on send
Response: The API will return the updated metafield.

{ "metafield": { "value": "something new", "value_type": "string", "namespace": "newvalue", "key": "Image 1 Path", "id": 19502841069631, "description": null, "owner_id": 6794851549247, "created_at": "2021-11-09T17:39:52+09:00", "updated_at": "2022-03-22T19:23:32+09:00", "owner_resource": "product", "type": "single_line_text_field", "admin_graphql_api_id": "gid://shopify/Metafield/19502841069631" } }
Code language: JSON / JSON with Comments (json)
response_update a metafield for a product
Shopify API - Update A Metafied For A Product 8

The above are all steps to update a metafield for a product using Postman with Shopify API. If you have any questions, please leave a comment below or refer to our Shopify api documentation.

If you still do not understand the tutorial’s content, you can watch the video below for a more overview.

Shopify API – Update a metafield for a product using Postman

Hopefully, you can do it!