Skip to main content
POST
Create or Upsert Metafield

Create or Upsert Metafield

This endpoint upserts: if a metafield with the same (ownerType, ownerId, namespace, key) already exists and your app owns it, its value is updated; otherwise a new metafield is created. There is at most one metafield per (resource, namespace, key). The body can be flat or wrapped in { "metafield": { ... } }.

Request

Required scope

write_metafields

Body parameters

string
required
Your app’s namespace: $app (stored as app--<your app id>), $app:<suffix>, app_<your_handle> (e.g. app_loyalty_engine), or a namespace your app created. Writing to a namespace that belongs to the merchant (e.g. custom) or to another app is refused with 403 — see Namespaces and ownership.
string
required
Field key. Unique per (ownerType, ownerId, namespace).
string
Any metafield type from the type catalogue — single_line_text_field, number_integer, json, money, product_reference, … — including list.<type> (e.g. list.single_line_text_field). Also string (free text). If omitted, the existing metafield’s type, then the field definition’s type, then string is used. An unknown type is rejected with 422.
any
required
The value, validated against type (and against the field definition’s validations when one exists) — an invalid value is rejected with 422. Scalars are sent as-is (string, number, boolean). Compound types (weight, dimension, volume, money, rating, lists, JSON) are sent as JSON. json values may also be sent pre-serialized as a string; they are stored as sent. An empty string is rejected with 422. To represent “nothing selected”, delete the metafield or store an explicit sentinel (e.g. "*").
string
required
One of shop, product, variant, collection, customer, order, page, blog, article, cart. Lowercase only. owner_resource is accepted as an alias.
string
The id of the owning resource. Required for every owner type except shop, where it defaults to the store. For product, variant and collection either id form works (numeric id or UUID); the metafield is stored under the UUID, which is what the storefront reads. owner_id is accepted as an alias.

Examples by type

Scalar text

Boolean

Rich text (HTML, rendered unescaped in Aqua)

Measurement

Money

Reference

List

Shop-level app settings

Response

The response is the standard envelope. On success the created/updated metafield is returned under data.metafield.
integer
201 if created, 200 if updated.
string
success or error.
object
The metafield. id and metafieldId are the same value (use either with Update and Delete). value is the stored string form (scalars stringified, compound types JSON-encoded). namespace is the stored name ($app comes back as app--<your app id>).

Example response

Cache invalidation

A successful upsert automatically expires the cached copy of the owning resource and any cached page HTML that rendered it. The next storefront render sees the new value.

Error codes