0

Get Customer

by
Published Nov 8, 2023

Get a customer with contact details, marketing consent, stats and addresses.

Script shopify Verified

The script

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

3
/**
4
 * Get Customer
5
 * Get a customer with contact details, marketing consent, order count, amount spent, addresses and last order.
6
 */
7
export async function main(auth: RT.Shopify, customer_id: string) {
8
  const id = customer_id.startsWith("gid://")
9
    ? customer_id
10
    : `gid://shopify/Customer/${customer_id}`
11
  const response = await fetch(
12
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
13
    {
14
      method: "POST",
15
      headers: {
16
        "X-Shopify-Access-Token": auth.token,
17
        "Content-Type": "application/json",
18
        Accept: "application/json",
19
      },
20
      body: JSON.stringify({
21
        query: `query ($id: ID!) { customer(id: $id) { 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 } lastOrder { id name createdAt } addressesV2(first: 20) { nodes { id name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } } } }`,
22
        variables: { id },
23
      }),
24
    }
25
  )
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  const { data, errors } = await response.json()
32
  if (errors) {
33
    throw new Error(JSON.stringify(errors))
34
  }
35

36
  return data.customer
37
}
38

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 single customer
    7
     * Retrieves a single customer.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      customer_id: string,
    13
      fields: string | undefined
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customers/${customer_id}.json`
    17
      );
    18
      for (const [k, v] of [["fields", fields]]) {
    19
        if (v !== undefined && v !== "") {
    20
          url.searchParams.append(k, v);
    21
        }
    22
      }
    23
      const response = await fetch(url, {
    24
        method: "GET",
    25
        headers: {
    26
          "X-Shopify-Access-Token": auth.token,
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36