0

Get Metafields

by
Published Nov 8, 2023

List the metafields of any resource that has them (product, variant, customer, order, collection, shop, ...), given its GID (e.g. `gid://shopify/Product/123`), optionally limited to one namespace. Returns one page; pass pageInfo.endCursor as after for the next.

Script shopify Verified

The script

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

3
/**
4
 * Get Metafields
5
 * List the metafields of any resource that has them (product, variant, customer, order, collection, shop, ...), given its GID (e.g. `gid://shopify/Product/123`), optionally limited to one namespace. Returns one page; pass pageInfo.endCursor as after for the next.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  owner_id: string,
10
  namespace: string | undefined,
11
  first: number | undefined,
12
  after: string | undefined
13
) {
14
  const response = await fetch(
15
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
16
    {
17
      method: "POST",
18
      headers: {
19
        "X-Shopify-Access-Token": auth.token,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({
24
        query: `query ($id: ID!, $namespace: String, $first: Int!, $after: String) { node(id: $id) { id ... on HasMetafields { metafields(namespace: $namespace, first: $first, after: $after) { nodes { id namespace key type value jsonValue createdAt updatedAt } pageInfo { hasNextPage endCursor } } } } }`,
25
        variables: {
26
          id: owner_id,
27
          namespace: namespace || undefined,
28
          first: first || 50,
29
          after: after || undefined,
30
        },
31
      }),
32
    }
33
  )
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  const { data, errors } = await response.json()
40
  if (errors) {
41
    throw new Error(JSON.stringify(errors))
42
  }
43
  if (!data.node) {
44
    throw new Error(`No resource found for ${owner_id}`)
45
  }
46

47
  return data.node.metafields
48
}
49

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
     * Retrieves a list of metafields that belong to a resource
    7
     * Retrieves a list of metafields that belong to a resource. Note: As of version 2019-07, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      limit: string | undefined,
    13
      since_id: string | undefined,
    14
      created_at_min: string | undefined,
    15
      created_at_max: string | undefined,
    16
      updated_at_min: string | undefined,
    17
      updated_at_max: string | undefined,
    18
      namespace: string | undefined,
    19
      key: string | undefined,
    20
      value_type: string | undefined,
    21
      fields: string | undefined
    22
    ) {
    23
      const url = new URL(
    24
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/metafields.json`
    25
      );
    26
      for (const [k, v] of [
    27
        ["limit", limit],
    28
        ["since_id", since_id],
    29
        ["created_at_min", created_at_min],
    30
        ["created_at_max", created_at_max],
    31
        ["updated_at_min", updated_at_min],
    32
        ["updated_at_max", updated_at_max],
    33
        ["namespace", namespace],
    34
        ["key", key],
    35
        ["value_type", value_type],
    36
        ["fields", fields],
    37
      ]) {
    38
        if (v !== undefined && v !== "") {
    39
          url.searchParams.append(k, v);
    40
        }
    41
      }
    42
      const response = await fetch(url, {
    43
        method: "GET",
    44
        headers: {
    45
          "X-Shopify-Access-Token": auth.token,
    46
        },
    47
        body: undefined,
    48
      });
    49
      if (!response.ok) {
    50
        const text = await response.text();
    51
        throw new Error(`${response.status} ${text}`);
    52
      }
    53
      return await response.json();
    54
    }
    55