Shopify metafields and metaobjects
Shopify metafields and metaobjects
Shopify has two different ways to store custom data. EcartAPI exposes them as two resources. This guide explains what each one is and how they connect. The request and response of every endpoint are in the API Reference, not here.
| Resource | API Reference |
|---|---|
| Metafields | Metafields |
| Metaobject entries | Get a metaobject |
| Metaobject definitions | Get a metaobject definition |
Before you startMetaobjects need their own Shopify scopes. A token that can read products and metafields cannot read metaobjects until the store authorizes them. The scopes are listed below.
Four different things
| Name | What it is | Belongs to | EcartAPI resource |
|---|---|---|---|
| Metafield | One custom value attached to an owner: a product, a variant, an order or the shop. It has a namespace, a key, a type and a value. | That owner | Metafield values and metafield definitions |
| Metafield definition | The schema of a metafield: its name, namespace, key, type and which owner it can be attached to. | The shop | GET /metafields/{metafieldId} |
| Metaobject | A standalone record with its own fields. It is not attached to a product or an order. The same record can be referenced by many products. | The shop, identified by type + handle | GET /metaobjects/{metaobjectId} |
| Metaobject definition | The schema of a metaobject type: its name, the fields it has, and who can read it. | The shop | GET /metaobjects/definitions/{definitionId} |
flowchart LR P[Product] --> MF[Metafield shopify.disclosure] MF -->|stores only a GID| MO[Metaobject 718862254401] MD[Metaobject definition] -->|describes the fields of| MO MFD[Metafield definition] -->|describes| MF
A metafield always has an owner. A metaobject does not. That is why a metaobject id cannot be loaded from /metafields/{metafieldId}: that endpoint reads a metafield definition, and the id belongs to a different resource.
Why a product metafield returns a metaobject id
Some metafield types do not store the content. They store a pointer to a metaobject.
GET /metafields/products/15457358381377 can answer:
{
"id": "193157197332801",
"key": "disclosure",
"namespace": "shopify",
"type": "list.disclosure_reference",
"value": ["gid://shopify/Metaobject/718862254401"]
}list.disclosure_reference is a reference type. The value gid://shopify/Metaobject/718862254401 means "the content lives in metaobject 718862254401". The metafield response is complete: Shopify stores the pointer, not the disclosure text.
Those are two different ids:
| Id in the response | What it is | Where to use it |
|---|---|---|
193157197332801 | The metafield definition id. | GET /metafields/193157197332801 |
718862254401 | The metaobject id, the number inside the GID. | GET /metaobjects/718862254401 |
The same thing happens with metaobject_reference, list.metaobject_reference, product_reference, file_reference and the other *_reference types. The value is a GID, or a list of GIDs.
To read the content, take the numeric id from the GID and call Get a metaobject:
GET /api/v2/metaobjects/718862254401
Do not call GET /metafields/718862254401. That id is not a metafield definition, so the API answers 404 Resource metafieldDefinition not found.
Sending the GID itself in the metaobject path also fails. GET /metaobjects/gid://shopify/Metaobject/718862254401 answers 422. The path accepts only the numeric id. The GID stays inside fields[].value when the field is a reference.
Scopes
Request these four scopes on the Shopify app, and have the store authorize the app again. Adding them to the app configuration does not change a token that was issued before.
| Scope | What it unlocks |
|---|---|
read_metaobjects | Required to read entries. |
write_metaobjects | Create, update, upsert and delete entries. |
read_metaobject_definitions | Required to list and get definitions. |
write_metaobject_definitions | Create, update and delete definitions. |
write_metaobjects does not replace read_metaobjects, and write_metaobject_definitions does not replace read_metaobject_definitions. Ask for the read scopes explicitly.
Without read_metaobjects, GET /metaobjects/{id} answers 404 Resource metaobject not found. Shopify returns an empty result and no error, so a missing scope and a missing id look the same.
Without read_metaobject_definitions, GET /metaobjects/definitions answers 200 with definitions: []. It does not answer 403.
Authorize the four scopes in one step. A store that authorizes only the read scopes has to authorize the app again later to create entries or definitions.
Metaobject definitions
A definition is the form. Every entry of that type has the same fields.
A store list mixes three kinds. The type tells you which one you are looking at.
| Kind | How type looks | What you do with it |
|---|---|---|
| Yours | ecart_test, size_chart | Create, update and delete the definition and its entries. |
| Shopify's | shopify--color-pattern, shopify--disclosure-custom | Read them. Disclosures, colors and taxonomy values are definitions Shopify created. Updating or deleting one usually answers 400 or 401, because Shopify owns it. |
| App-reserved | $app:warranty_info | Reserved for the app. This is the only kind that accepts access.admin. |
metaobjectsCount is how many entries that type has, as a numeric string ("0", "6"). It saves you a list call when you only need the count. null means Shopify did not return a count.
fieldDefinitions[].validations are Shopify's rules for the value (max, choices, metaobject_definition_id, and others). EcartAPI returns them and does not check them. Shopify rejects a value that breaks a validation when you create or update the entry.
Create a definition with type, name and at least one field definition. Each field definition has key, name and type. type is a Shopify metafield type, such as single_line_text_field, multi_line_text_field, number_integer or product_reference.
{
"definition": {
"type": "ecart_test",
"name": "Ecart Test",
"displayNameKey": "title",
"access": { "storefront": "PUBLIC_READ" },
"fieldDefinitions": [
{ "key": "title", "name": "Title", "type": "single_line_text_field", "required": true },
{ "key": "body", "name": "Body", "type": "multi_line_text_field" }
]
}
}displayNameKey is the field EcartAPI uses as displayName on each entry.
Access
| Field | Allowed values | Where it works |
|---|---|---|
access.storefront | NONE, PUBLIC_READ | Any definition. |
access.admin | MERCHANT_READ, MERCHANT_READ_WRITE | Only on an app-reserved type, type starting with $app:. |
Sending access.admin on a normal type such as ecart_test answers 400:
Admin access can only be specified on metaobject definitions that have an app-reserved type.
The response can still show access.admin: "PUBLIC_READ_WRITE". That is Shopify's value for a merchant-owned definition. You do not send it on create.
Changing the fields later
Update a definition does not take a new type. Send only what changes. Field changes go in three lists, and at least one property is required:
| List | Effect |
|---|---|
createFieldDefinitions | Add a field. key, name and type are required. |
updateFieldDefinitions | Change the name, description, required flag or validations of a field. The type of an existing field cannot be changed. |
deleteFieldDefinitions | Remove fields by key. |
Delete a definition also deletes every entry of that type. The call does not ask for a second confirmation.
Metaobject entries
An entry is one record of a definition. You address it by numeric id, or by the pair type + handle.
| Call | When to use it |
|---|---|
Create POST /metaobjects | The handle is new. If that handle already exists, Shopify rejects the call. |
Upsert POST /metaobjects/upsert | You know type and handle and you do not have the id. The first call creates the entry. The same call later updates that same id. |
Update PUT /metaobjects/{id} | You already have the numeric id. Fields you do not send keep their current values. |
Delete DELETE /metaobjects/{id} | Remove one entry. The answer is { "success": true, "id": "<numeric id>" }. |
handle is unique inside a type. Two types can use the same handle.
Point a product at an entry
Creating the metaobject does not attach it to a product. Shopify stores that link in a reference metafield, and metafieldsSet is the mutation that writes it. value is always a string: one GID for metaobject_reference, or a JSON array of GIDs for a list type such as list.metaobject_reference or list.disclosure_reference.
EcartAPI already does that with Set metafield values for a product POST /metafields/products/{productId}. The same call exists for an order (POST /metafields/orders/{orderId}) and for a variant (POST /metafields/products/{productId}/variants/{variantId}). The metafield definition must already exist. This call does not send a type, and Shopify requires type only when the definition is missing.
value follows the same JSON rule as any other metafield value on this endpoint. The shape comes from the metafield type, not from how many entries you want to store.
metaobject_reference stores one GID, so value is a JSON string and has no brackets. custom.ficha is that type:
{
"metafield": {
"namespace": "custom",
"key": "ficha",
"ownership": "merchant",
"value": "\"gid://shopify/Metaobject/655852437673\""
}
}list.metaobject_reference and list.disclosure_reference store a JSON array of GIDs. The array stays an array when it holds a single entry. shopify.disclosure is list.disclosure_reference, and a product metafield of that type returns ["gid://shopify/Metaobject/718862254401"]. Send that same text back:
{
"metafield": {
"namespace": "shopify",
"key": "disclosure",
"ownership": "merchant",
"value": "[\"gid://shopify/Metaobject/718862254401\"]"
}
}A list.metaobject_reference uses the same array, with one GID or several. A metaobject_reference uses the single GID.
Send ownership: "merchant" for a merchant namespace such as custom or shopify. Without it, EcartAPI prefixes the namespace with $app: and the value lands on the app metafield instead of the one you meant. Shopify still answers 400 when the app is not allowed to write that namespace. Reading is unchanged: the product metafield returns the GID, and GET /metaobjects/{id} returns the fields.
Field values are sent unchanged
fields[].value is a string, an empty string, or null. EcartAPI does not convert it. The definition says what the string must contain, and this endpoint does not look the definition up before sending the value.
| Field type | What to send in value |
|---|---|
| Text, number, boolean, json | The text Shopify expects for that type. |
List, such as list.single_line_text_field | A JSON array as text, for example "[\"red\",\"blue\"]". |
Reference, such as product_reference or metaobject_reference | The full GID. A numeric id is not accepted and is not converted. |
A product reference looks like this:
{ "key": "product", "value": "gid://shopify/Product/9008390570153" }9008390570153 alone is not a valid reference. The same rule applies when you read the entry: the response returns that GID as a string. It does not embed the product.
Quotes and line breaks are normal JSON. This request value:
{ "key": "body", "value": "Line 1 with \"quotes\"\nLine 2" }is the text Line 1 with "quotes" followed by a new line and Line 2. The backslashes are not stored. The response shows the same escaping because that is how JSON writes the string.
Status and the publishable capability
status is ACTIVE or DRAFT. EcartAPI maps it to Shopify's publishable capability.
Shopify accepts status only when the definition was created with that capability enabled. On a definition that does not have it, create and update answer:
400 Capability is not enabled: publishable
Leave status out for those definitions. The response then has "status": null. The entries are still created and updated.
Lists and pages
List entries requires type. List definitions does not.
| Query | Meaning |
|---|---|
limit | Page size, from 1 to 250. Default 50. |
page | Cursor from the previous response, page.next or page.previous. |
A page looks like { "next": null, "previous": null } when there is nothing else to read. A missing or invalid type, or a limit outside 1–250, answers 422 with ecartapiError: true.
Errors you will see
| Status | When |
|---|---|
422 ecartapiError: true | The body or the query is invalid, or {id} is not a numeric id. Shopify is not called. |
404 Resource metaobject not found | The entry does not exist, or the token cannot read metaobjects. |
404 Resource metaobjectDefinition not found | The definition does not exist, or the token cannot read definitions. |
200 with an empty definitions or metaobjects array | Shopify returned no rows. For definitions, this is also what a missing read_metaobject_definitions scope looks like. |
400 | Shopify rejected the change. The message is Shopify's, for example a duplicate handle, an unknown field key, Capability is not enabled: publishable, or admin access on a type that is not $app:. |
401 | Shopify answered ACCESS_DENIED for a write. |
Updated 1 day ago