0

Search Customers

by
Published Nov 8, 2023

Search customers with Shopify's search syntax, or list them all.

Script shopify Verified

The script

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

3
/**
4
 * Search Customers
5
 * Search customers with Shopify's search syntax (e.g. `email:jane@example.com`, `country:France`, `tag:vip orders_count:>3`, or free text matching name, email and phone). Leave query empty to list them all. Returns one page; pass pageInfo.endCursor as after to get the next one.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string | undefined,
10
  sort_key:
11
    | "UPDATED_AT"
12
    | "CREATED_AT"
13
    | "NAME"
14
    | "LOCATION"
15
    | "ID"
16
    | "RELEVANCE"
17
    | undefined,
18
  reverse: boolean | undefined,
19
  first: number | undefined,
20
  after: string | undefined
21
) {
22
  const response = await fetch(
23
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
24
    {
25
      method: "POST",
26
      headers: {
27
        "X-Shopify-Access-Token": auth.token,
28
        "Content-Type": "application/json",
29
        Accept: "application/json",
30
      },
31
      body: JSON.stringify({
32
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: CustomerSortKeys, $reverse: Boolean) { customers(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId displayName firstName lastName state tags note locale verifiedEmail taxExempt numberOfOrders amountSpent { amount currencyCode } createdAt updatedAt defaultEmailAddress { emailAddress marketingState marketingOptInLevel } defaultPhoneNumber { phoneNumber } defaultAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } } pageInfo { hasNextPage endCursor } } }`,
33
        variables: {
34
          first: first || 50,
35
          after: after || undefined,
36
          query: query || undefined,
37
          sortKey: sort_key || undefined,
38
          reverse: reverse || undefined,
39
        },
40
      }),
41
    }
42
  )
43

44
  if (!response.ok) {
45
    throw new Error(`${response.status} ${await response.text()}`)
46
  }
47

48
  const { data, errors } = await response.json()
49
  if (errors) {
50
    throw new Error(JSON.stringify(errors))
51
  }
52

53
  return data.customers
54
}
55

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
     * 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