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 with 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 of 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.ย
Shopify Agile Integration
Shopify Apptivo Integration
Shopify Erply Integration
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.ย
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"
These are two new attributes that I want to update.ย
โvalueโ: "something new",
"type": "single_line_text_field"
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}
ย
-
- {API_key} โ The API key that you generated.
-
- {admin_API_access_token} โ The admin API access token that you generated.
-
- {shop} โ The name of your development store.
-
- {api-version} โ The supported API version that you want to use.
-
- {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
ย
-
- Paste the URL above.
In the body section, enter this code.ย
Payload:
{
"metafield": {
"id": 19502841069631,
"value": "something new",
"type": "single_line_text_field"
}
}
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"
}
}
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.
Hopefully, you can do it!