0

Search Products

by
Published Nov 8, 2023

Search products with Shopify's search syntax (title, vendor, status, tag, SKU, collection...).

Script shopify Verified

The script

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

3
/**
4
 * Search Products
5
 * Search products with Shopify's search syntax (e.g. `title:*hoodie* vendor:Acme status:active tag:sale`, `sku:ABC-1`, `collection_id:123`). Leave query empty to list all products. 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:
11
    | "TITLE"
12
    | "CREATED_AT"
13
    | "UPDATED_AT"
14
    | "PUBLISHED_AT"
15
    | "PRODUCT_TYPE"
16
    | "VENDOR"
17
    | "INVENTORY_TOTAL"
18
    | "ID"
19
    | "RELEVANCE"
20
    | undefined,
21
  reverse: boolean | undefined,
22
  first: number | undefined,
23
  after: string | undefined
24
) {
25
  const response = await fetch(
26
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
27
    {
28
      method: "POST",
29
      headers: {
30
        "X-Shopify-Access-Token": auth.token,
31
        "Content-Type": "application/json",
32
        Accept: "application/json",
33
      },
34
      body: JSON.stringify({
35
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: ProductSortKeys, $reverse: Boolean) { products(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId title handle status vendor productType tags createdAt updatedAt publishedAt totalInventory variantsCount { count } priceRangeV2 { minVariantPrice { amount currencyCode } maxVariantPrice { amount currencyCode } } featuredMedia { preview { image { url } } } } pageInfo { hasNextPage endCursor } } }`,
36
        variables: {
37
          first: first || 50,
38
          after: after || undefined,
39
          query: query || undefined,
40
          sortKey: sort_key || undefined,
41
          reverse: reverse || undefined,
42
        },
43
      }),
44
    }
45
  )
46

47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  const { data, errors } = await response.json()
52
  if (errors) {
53
    throw new Error(JSON.stringify(errors))
54
  }
55

56
  return data.products
57
}
58

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