0

Create Metaobject

by
Published today

Create a metaobject entry of a definition. On definitions with the publishable capability, new entries are DRAFT unless status is set. fields maps field keys to values (e.g. `{ "title": "Hello", "rating": 5 }`); non-string values are JSON-encoded. Needs the write_metaobjects scope.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_type = string
4

5
export async function type(auth: RT.Shopify) {
6
  const response = await fetch(
7
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
8
    {
9
      method: "POST",
10
      headers: {
11
        "X-Shopify-Access-Token": auth.token,
12
        "Content-Type": "application/json",
13
        Accept: "application/json",
14
      },
15
      body: JSON.stringify({
16
        query: `query { metaobjectDefinitions(first: 250) { nodes { type name fieldDefinitions { key } } } }`,
17
      }),
18
    }
19
  )
20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23
  const { data, errors } = await response.json()
24
  if (errors) {
25
    throw new Error(JSON.stringify(errors))
26
  }
27
  return data.metaobjectDefinitions.nodes.map(
28
    (d: {
29
      type: string
30
      name: string
31
      fieldDefinitions: { key: string }[]
32
    }) => ({
33
      value: d.type,
34
      label: `${d.name} (${d.type}): ${d.fieldDefinitions.map((f) => f.key).join(", ")}`,
35
    })
36
  )
37
}
38

39
/**
40
 * Create Metaobject
41
 * Create a metaobject entry of a definition. On definitions with the publishable capability, new entries are DRAFT unless status is set. fields maps field keys to values (e.g. `{ "title": "Hello", "rating": 5 }`); non-string values are JSON-encoded. Needs the write_metaobjects scope.
42
 */
43
export async function main(
44
  auth: RT.Shopify,
45
  type: DynSelect_type,
46
  fields: { [key: string]: any },
47
  handle: string | undefined,
48
  status: "ACTIVE" | "DRAFT" | undefined
49
) {
50
  const response = await fetch(
51
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
52
    {
53
      method: "POST",
54
      headers: {
55
        "X-Shopify-Access-Token": auth.token,
56
        "Content-Type": "application/json",
57
        Accept: "application/json",
58
      },
59
      body: JSON.stringify({
60
        query: `mutation ($metaobject: MetaobjectCreateInput!) { metaobjectCreate(metaobject: $metaobject) { metaobject { id handle type displayName updatedAt capabilities { publishable { status } } fields { key type value } } userErrors { field message } } }`,
61
        variables: {
62
          metaobject: {
63
            type,
64
            handle: handle || undefined,
65
            fields: Object.entries(fields).map(([key, value]) => ({
66
              key,
67
              value: typeof value === "string" ? value : JSON.stringify(value),
68
            })),
69
            capabilities: status ? { publishable: { status } } : undefined,
70
          },
71
        },
72
      }),
73
    }
74
  )
75

76
  if (!response.ok) {
77
    throw new Error(`${response.status} ${await response.text()}`)
78
  }
79

80
  const { data, errors } = await response.json()
81
  if (errors) {
82
    throw new Error(JSON.stringify(errors))
83
  }
84
  if (data.metaobjectCreate.userErrors.length) {
85
    throw new Error(JSON.stringify(data.metaobjectCreate.userErrors))
86
  }
87

88
  return data.metaobjectCreate
89
}
90