The Metafield resource allows you to add additional information to store's resources. Metafields can be used in several ways, such as to add a summary to a blog post. You can also use metafields to share information with other Haravan apps.
Resources that can have metafields
Metafields can be added to the following resources:
Metafields have four required properties: a key, a namespace, a value and a value_type. The key is the identifier for a metafield. The namespace functions a container for a set of metafields which help to distinguish between metadata that you created and metadata created by others with a similair namespace. The value is the content of the metafield. The value_type describe the state of the value, whether it is a 'string', an 'integer' or an 'json'. There also exists an optional description property that provides additional information about the metafield.
What can you do with Metafield?
The Haravan API lets you do the following with the Metafield resource. More detailed versions of these general actions may be available:
- GET https://apis.haravan.com/com/metafields.json Retrieves metafields that belong to a store
- GET https://apis.haravan.com/com/products/#{id}/metafields.json Retrieves metafields that belong to a product
- GET https://apis.haravan.com/com/metafields.json?metafield[owner_id]=850703190&metafield[owner_resource]=product_image Retrieves metafields that belong to a product image
- GET https://apis.haravan.com/com/metafields/count.json Retrieves a count of metafields that belong to a store
- GET https://apis.haravan.com/com/products/#{id}/metafields/count.json Retrieves a count of metafields that belong to a product
- GET https://apis.haravan.com/com/metafields/#{id}.json Retrieves a single store metafield by its ID
- GET https://apis.haravan.com/com/products/#{id}/metafields/#{id}.json Retrieves a single product metafield by its ID
- POST https://apis.haravan.com/com/metafields.json Create a new metafield for a store
- POST https://apis.haravan.com/com/products/#{id}/metafields.json Create a new metafield for a product
- PUT https://apis.haravan.com/com/metafields/#{id}.json Update a store metafield
- PUT https://apis.haravan.com/com/products/#{id}/metafields/#{id}.json Update a product metafield
- DELETE https://apis.haravan.com/com/metafields/#{id}.json Delete a store metafield
- DELETE https://apis.haravan.com/com/products/#{id}/metafields/#{id}.json Delete a product metafield
Metafield Properties
Metafield types
Each metafield and metafield definition has a value_type, which defines the type of information that it can store.
When using the API to read and write metafields, the value is always entered and stored as a string, regardless of value_type.
Metafields and definitions can have the following value types:
Endpoints
page | Page to show the result. |
limit | Amount of results. (default: 50) (maximum: 50) |
since_id | Restrict results to after the specified ID |
created_at_min | Show metafields created after date (format: 2021-11-30T08:40:33.594Z) |
created_at_max | Show metafields created before date (format: 2021-11-30T08:40:33.594Z) |
updated_at_min | Show metafields last updated after date (format: 2021-11-30T08:40:33.594Z) |
updated_at_max | Show metafields last updated before date (format: 2021-11-30T08:40:33.594Z) |
namepace | Show metafields with given namespace |
key | Show metafields with given key |
value_type | string : Show only metafields with string value types integer : Show only metafields with integer value types json : Show only metafields with json value types |
fields | comma-separated list of fields to include in the response |
Get all metafields that belong to a store
GET https://apis.haravan.com/com/metafields.json
HTTP/1.1 200 OK
{ "metafields": [ { "created_at": "2021-11-30T08:40:33.594Z", "description": null, "id": 721389482, "key": "Wash", "namespace": "instructions", "owner_id": 690933842, "updated_at": "2021-12-13T06:32:35.731Z", "value": "Cold", "value_type": "string", "owner_resource": "shop" } ] }
Get all metafields after the specified ID
GET https://apis.haravan.com/com/metafields.json?since_id=721389482
HTTP/1.1 200 OK
{ "metafields": [ { "created_at": "2021-12-15T10:37:38.363Z", "description": null, "id": 996577826, "key": "warehouse", "namespace": "inventory", "owner_id": 690933842, "updated_at": "2021-12-15T10:38:37.54Z", "value": 25, "value_type": "integer", "owner_resource": "shop" } ] }
Get metafields that belong to a product.
Get all metafields that belong to a product.
GET https://apis.haravan.com/com/products/632910392/metafields.json
HTTP/1.1 200 OK
{ "metafields": [ { "created_at": "2021-11-30T08:40:33.594Z", "description": "French product title", "id": 845366454, "key": "title_fr", "namespace": "translations", "owner_id": 632910392, "updated_at": "2021-12-13T06:32:35.731Z", "value": "produit", "value_type": "string", "owner_resource": "product" } ] }
Get all metafields that belong to the images of a product.
HTTP/1.1 200 OK
{ "metafields": [ { "created_at": "2021-11-30T08:40:33.594Z", "description": "French product image title", "id": 625663657, "key": "title_fr", "namespace": "translation", "owner_id": 850703190, "updated_at": "2021-12-13T06:32:35.731Z", "value": "tbn", "value_type": "string", "owner_resource": "product_image" } ] }
Get a count of metafields that belong to a store.
Get a count of all metafields for a store.
GET https://apis.haravan.com/com/metafields/count.json
HTTP/1.1 200 OK
{ "count": 1 }
Get a count of metafields that belong to a product.
Get a count of all metafields that belong to a product.
GET https://apis.haravan.com/com/products/632910392/metafields/count.json
HTTP/1.1 200 OK
{ "count": 1 }
fields | Comma-separated list of fields to include in the response. |
Get a single store metafield by ID. A metafield belonging to any resource can be found this way.
GET https://apis.haravan.com/com/metafields/721389482.json
HTTP/1.1 200 OK
{ "metafield": { "created_at": "2021-11-30T08:40:33.594Z", "description": null, "id": 721389482, "key": "app_key", "namespace": "affiliates", "owner_id": 690933842, "updated_at": "2021-12-13T06:32:35.731Z", "value": "app_key", "value_type": "string", "owner_resource": "shop" } }
Get a single product metafield by its ID.
Get a single product metafield using the metafield's nested resource path.
GET https://apis.haravan.com/com/products/632910392/metafields/845366454.json
HTTP/1.1 200 OK
{ "metafield": { "created_at": "2021-11-30T08:40:33.594Z", "description": "French product title", "id": 845366454, "key": "title_fr", "namespace": "translations", "owner_id": 632910392, "updated_at": "2021-12-13T06:32:35.731Z", "value": "produit", "value_type": "string", "owner_resource": "product" } }
Create a new metafield for a store.
POST https://apis.haravan.com/com/metafields.json
{ "metafield": { "namespace": "inventory", "key": "warehouse", "value": 25, "value_type": "integer" } }
HTTP/1.1 201 Created
{ "metafield": { "created_at": "2016-09-15T09:53:02.887Z", "description": null, "id": 1003594533, "key": "warehouse", "namespace": "inventory", "owner_id": 1000045110, "owner_resource": "shop", "value": "25", "value_type": "integer", "updated_at": "2021-12-21T06:58:17.290Z" } }
Trying to create a metafield without a key will return an error.
POST https://apis.haravan.com/com/metafields.json
{ "metafield": { "key": null } }
HTTP/1.1 422 Unprocessable Entity
{ "errors": "Key can't be blank" }
Create a new metafield for a product.
Create a new metafield for a product.
POST https://apis.haravan.com/com/products/1037394594/metafields.json
{ "metafield": { "namespace": "inventory", "key": "warehouse", "value": 25, "value_type": "integer" } }
HTTP/1.1 201 Created
{ "metafield": { "created_at": "2021-12-21T07:41:41.298Z", "description": null, "id": 1015890158, "key": "warehouse", "namespace": "inventory", "owner_id": 1037394594, "owner_resource": "product", "value": "25", "value_type": "integer", "updated_at": "2021-12-21T07:41:41.298Z" } }
Create a new metafield for a product with "value_type": "json". A JSON-formatted string.
POST https://apis.haravan.com/com/products/1037394594/metafields.json
{ "metafield": { "namespace": "inventory", "key": "warehouse", "value": "[{\"k\":\"v1\"},{\"k\":\"v2\"}]", "value_type": "json" } }
HTTP/1.1 201 Created
{ "metafield": { "created_at": "2021-12-22T04:33:44.121Z", "description": null, "id": 1015892658, "key": "warehouse", "namespace": "inventory", "owner_id": 1037394594, "owner_resource": "product", "value": "[{\"k\":\"v1\"},{\"k\":\"v2\"}]", "value_type": "json", "updated_at": "2021-12-22T04:33:44.121Z" } }
Another example JSON-formatted string.
POST https://apis.haravan.com/com/products/1037394594/metafields.json
{ "metafield": { "namespace": "inventory", "key": "warehouse", "value": "{\"price\":10000,\"stock\":\"instock\",\"meta\":[{\"k\":\"v1\"},{\"k\":\"v2\"}]}", "value_type": "json" } }
HTTP/1.1 201 Created
{ "metafield": { "created_at": "2021-12-22T04:33:44.121Z", "description": null, "id": 1015892658, "key": "warehouse", "namespace": "inventory", "owner_id": 1037394594, "owner_resource": "product", "value": "{\"price\":10000,\"stock\":\"instock\",\"meta\":[{\"k\":\"v1\"},{\"k\":\"v2\"}]}", "value_type": "json", "updated_at": "2021-12-22T04:46:38.624Z" } }
Code Liquid in product.liquid:
{%- if product.metafields.inventory != blank -%} type: {{ product.metafields.inventory.warehouse.type }} <br/> price: {{ product.metafields.inventory.warehouse.value.price }} <br/> stock: {{ product.metafields.inventory.warehouse.value.stock }} <br/> {%- for field in product.metafields.inventory.warehouse.value.meta -%} {{ field.k }} <br/> {%- endfor -%} {%- endif -%}
Update an existing store metafield. A metafield belonging to any resource can be updated this way. Namespace and key of an existing metafield cannot be changed.
PUT https://apis.haravan.com/com/metafields/1003594533.json
{ "metafield": { "id": 1003594533, "value": "something new", "value_type": "string" } }
HTTP/1.1 200 OK
{ "metafield": { "created_at": "2016-09-15T09:53:02.887Z", "description": null, "id": 1003594533, "key": "warehouse", "namespace": "inventory", "owner_id": 1000045110, "owner_resource": "shop", "value": "something new", "value_type": "string", "updated_at": "2021-12-21T07:47:01.191Z" } }
Update an existing metafield belonging to a product using the metafield's nested resource path. Namespace and key of an existing metafield cannot be changed.
PUT https://apis.haravan.com/com/products/1037394594/metafields/1015890158.json
{ "metafield": { "id": 1015890158, "value": "titre", "value_type": "string" } }
HTTP/1.1 200 OK
{ "metafield": { "created_at": "2021-12-21T07:41:41.298Z", "description": null, "id": 1015890158, "key": "warehouse", "namespace": "inventory", "owner_id": 1037394594, "owner_resource": "product", "value": "titre", "value_type": "string", "updated_at": "2021-12-21T07:52:47.536Z" } }
Delete an existing store metafield by id. A metafield belonging to any resource can be deleted this way.
DELETE https://apis.haravan.com/com/metafields/721389482.json
HTTP/1.1 200 OK
{}
Delete existing metafield belonging to a product using the nested resource path.
DELETE https://apis.haravan.com/com/products/632910392/metafields/845366454.json
HTTP/1.1 200 OK
{}