Retrieves all customers returned by a customer saved search

Retrieves all customers returned by a customer saved search.

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieves all customers returned by a customer saved search
7
 * Retrieves all customers returned by a customer saved search.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  customer_saved_search_id: string,
13
  order: string | undefined,
14
  limit: string | undefined,
15
  fields: string | undefined
16
) {
17
  const url = new URL(
18
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customer_saved_searches/${customer_saved_search_id}/customers.json`
19
  );
20
  for (const [k, v] of [
21
    ["order", order],
22
    ["limit", limit],
23
    ["fields", fields],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      "X-Shopify-Access-Token": auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42