0

Search Collections

by
Published Nov 8, 2023

Search custom and smart collections by title, type, handle or product.

Script shopify Verified

The script

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

3
/**
4
 * Search Collections
5
 * Search custom (manual) and smart (condition-based) collections with Shopify's search syntax (e.g. `title:Summer*`, `collection_type:smart`, `handle:frontpage`, `product_id:123`). Leave query empty to list all collections. Returns one page; pass pageInfo.endCursor as after for the next.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string | undefined,
10
  sort_key: "TITLE" | "UPDATED_AT" | "ID" | "RELEVANCE" | undefined,
11
  reverse: boolean | undefined,
12
  first: number | undefined,
13
  after: 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: `query ($first: Int!, $after: String, $query: String, $sortKey: CollectionSortKeys, $reverse: Boolean) { collections(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId title handle descriptionHtml sortOrder updatedAt productsCount { count } image { url altText } sources { __typename id title } } pageInfo { hasNextPage endCursor } } }`,
26
        variables: {
27
          first: first || 50,
28
          after: after || undefined,
29
          query: query || undefined,
30
          sortKey: sort_key || undefined,
31
          reverse: reverse || undefined,
32
        },
33
      }),
34
    }
35
  )
36

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

41
  const { data, errors } = await response.json()
42
  if (errors) {
43
    throw new Error(JSON.stringify(errors))
44
  }
45

46
  return data.collections
47
}
48

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 custom collections
    7
     * Retrieves a list of custom collections. Note: As of version 2019-10, 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
      ids: string | undefined,
    14
      since_id: string | undefined,
    15
      title: string | undefined,
    16
      product_id: string | undefined,
    17
      handle: string | undefined,
    18
      updated_at_min: string | undefined,
    19
      updated_at_max: string | undefined,
    20
      published_at_min: string | undefined,
    21
      published_at_max: string | undefined,
    22
      published_status: string | undefined,
    23
      fields: string | undefined
    24
    ) {
    25
      const url = new URL(
    26
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/custom_collections.json`
    27
      );
    28
      for (const [k, v] of [
    29
        ["limit", limit],
    30
        ["ids", ids],
    31
        ["since_id", since_id],
    32
        ["title", title],
    33
        ["product_id", product_id],
    34
        ["handle", handle],
    35
        ["updated_at_min", updated_at_min],
    36
        ["updated_at_max", updated_at_max],
    37
        ["published_at_min", published_at_min],
    38
        ["published_at_max", published_at_max],
    39
        ["published_status", published_status],
    40
        ["fields", fields],
    41
      ]) {
    42
        if (v !== undefined && v !== "") {
    43
          url.searchParams.append(k, v);
    44
        }
    45
      }
    46
      const response = await fetch(url, {
    47
        method: "GET",
    48
        headers: {
    49
          "X-Shopify-Access-Token": auth.token,
    50
        },
    51
        body: undefined,
    52
      });
    53
      if (!response.ok) {
    54
        const text = await response.text();
    55
        throw new Error(`${response.status} ${text}`);
    56
      }
    57
      return await response.json();
    58
    }
    59