0

Searches for customers that match a supplied query

by
Published Nov 8, 2023

Searches for customers that match a supplied query. 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.

Script shopify Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 416 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Searches for customers that match a supplied query
7
 * Searches for customers that match a supplied query. 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
  order: string | undefined,
13
  query: 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}/customers/search.json`
19
  );
20
  for (const [k, v] of [
21
    ["order", order],
22
    ["query", query],
23
    ["limit", limit],
24
    ["fields", fields],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      "X-Shopify-Access-Token": auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43