0

Update Customer

by
Published Nov 8, 2023

Update a customer's email, phone, name, note or tags.

Script shopify Verified

The script

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

3
/**
4
 * Update Customer
5
 * Update a customer's email, phone, name, note or tags. Only the fields you fill are changed; tags replace the existing list. Use the address actions to change addresses.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  customer_id: string,
10
  email: string | undefined,
11
  phone: string | undefined,
12
  first_name: string | undefined,
13
  last_name: string | undefined,
14
  note: string | undefined,
15
  tags: string[] | undefined
16
) {
17
  const id = customer_id.startsWith("gid://")
18
    ? customer_id
19
    : `gid://shopify/Customer/${customer_id}`
20

21
  const response = await fetch(
22
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
23
    {
24
      method: "POST",
25
      headers: {
26
        "X-Shopify-Access-Token": auth.token,
27
        "Content-Type": "application/json",
28
        Accept: "application/json",
29
      },
30
      body: JSON.stringify({
31
        query: `mutation ($input: CustomerInput!) { customerUpdate(input: $input) { customer { 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 } } userErrors { field message } } }`,
32
        variables: {
33
          input: {
34
            id,
35
            email: email || undefined,
36
            phone: phone || undefined,
37
            firstName: first_name || undefined,
38
            lastName: last_name || undefined,
39
            note: note || undefined,
40
            tags: tags?.length ? tags : undefined,
41
          },
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
  if (data.customerUpdate.userErrors.length) {
57
    throw new Error(JSON.stringify(data.customerUpdate.userErrors))
58
  }
59

60
  return data.customerUpdate
61
}
62

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
     * Updates a customer
    7
     * Updates a customer.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      customer_id: string,
    13
      body: {
    14
        customer?: {
    15
          id?: number;
    16
          metafields?: {
    17
            key?: string;
    18
            namespace?: string;
    19
            value?: string;
    20
            value_type?: string;
    21
            [k: string]: unknown;
    22
          }[];
    23
          [k: string]: unknown;
    24
        };
    25
        [k: string]: unknown;
    26
      }
    27
    ) {
    28
      const url = new URL(
    29
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customers/${customer_id}.json`
    30
      );
    31
    
    
    32
      const response = await fetch(url, {
    33
        method: "PUT",
    34
        headers: {
    35
          "Content-Type": "application/json",
    36
          "X-Shopify-Access-Token": auth.token,
    37
        },
    38
        body: JSON.stringify(body),
    39
      });
    40
      if (!response.ok) {
    41
        const text = await response.text();
    42
        throw new Error(`${response.status} ${text}`);
    43
      }
    44
      return await response.json();
    45
    }
    46