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