0

Set Metafield

by
Published Nov 8, 2023

Create or update a metafield on any resource (owner GID, e.g. `gid://shopify/Product/123`). value is always a string (JSON-encode lists and objects). type (e.g. single_line_text_field, number_integer, boolean, json, list.single_line_text_field) is required unless a metafield definition exists for the namespace and key, even when updating an existing metafield.

Script shopify Verified

The script

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

3
/**
4
 * Set Metafield
5
 * Create or update a metafield on any resource (owner GID, e.g. `gid://shopify/Product/123`). value is always a string (JSON-encode lists and objects). type (e.g. single_line_text_field, number_integer, boolean, json, list.single_line_text_field) is required unless a metafield definition exists for the namespace and key, even when updating an existing metafield.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  owner_id: string,
10
  namespace: string,
11
  key: string,
12
  value: string,
13
  type: string | undefined
14
) {
15
  const response = await fetch(
16
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
17
    {
18
      method: "POST",
19
      headers: {
20
        "X-Shopify-Access-Token": auth.token,
21
        "Content-Type": "application/json",
22
        Accept: "application/json",
23
      },
24
      body: JSON.stringify({
25
        query: `mutation ($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { id namespace key type value jsonValue createdAt updatedAt } userErrors { field message } } }`,
26
        variables: {
27
          metafields: [
28
            {
29
              ownerId: owner_id,
30
              namespace,
31
              key,
32
              value,
33
              type: type || undefined,
34
            },
35
          ],
36
        },
37
      }),
38
    }
39
  )
40

41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  const { data, errors } = await response.json()
46
  if (errors) {
47
    throw new Error(JSON.stringify(errors))
48
  }
49
  if (data.metafieldsSet.userErrors.length) {
50
    throw new Error(JSON.stringify(data.metafieldsSet.userErrors))
51
  }
52

53
  return data.metafieldsSet
54
}
55

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Creates a new metafield for a resource
    7
     * Creates a new metafield for a resource.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      body: {
    13
        metafield?: {
    14
          key?: string;
    15
          namespace?: string;
    16
          value?: number;
    17
          value_type?: string;
    18
          [k: string]: unknown;
    19
        };
    20
        [k: string]: unknown;
    21
      }
    22
    ) {
    23
      const url = new URL(
    24
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/metafields.json`
    25
      );
    26
    
    
    27
      const response = await fetch(url, {
    28
        method: "POST",
    29
        headers: {
    30
          "Content-Type": "application/json",
    31
          "X-Shopify-Access-Token": auth.token,
    32
        },
    33
        body: JSON.stringify(body),
    34
      });
    35
      if (!response.ok) {
    36
        const text = await response.text();
    37
        throw new Error(`${response.status} ${text}`);
    38
      }
    39
      return await response.json();
    40
    }
    41