0

Update customer

by
Published Oct 17, 2025

Updates a customer based on the unique customer ID.

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Update customer
7
 * Updates a customer based on the unique customer ID.
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  id: string,
12
  replace: string | undefined,
13
  body: {
14
    name?: string;
15
    company?: string;
16
    externalId?: string;
17
    username?: string;
18
    signedUpAt?: string;
19
    lastActivityAt?: string;
20
    lastCustomerActivityAt?: string;
21
    lastSeenAt?: string;
22
    avatarUrl?: string;
23
    externalIds?: { externalId: string; verified?: false | true }[];
24
    sharedExternalIds?: { externalId: string; verified?: false | true }[];
25
    emails?: {
26
      type?: "home" | "work" | "other";
27
      email: string;
28
      verified?: false | true;
29
    }[];
30
    sharedEmails?: {
31
      type?: "home" | "work" | "other";
32
      email: string;
33
      verified?: false | true;
34
    }[];
35
    phones?: {
36
      type?: "home" | "work" | "other" | "mobile" | "fax";
37
      phone: string;
38
      verified?: false | true;
39
    }[];
40
    sharedPhones?: {
41
      type?: "home" | "work" | "other" | "mobile" | "fax";
42
      phone: string;
43
      verified?: false | true;
44
    }[];
45
    whatsapps?: { type?: "mobile"; phone: string; verified?: false | true }[];
46
    facebookIds?: { pageId: string; userId: string; name?: string }[];
47
    instagramIds?: {
48
      pageId: string;
49
      threadId: string;
50
      username: string;
51
      instagramId?: string;
52
    }[];
53
    socials?: {
54
      type: "twitter" | "facebook" | "instagram" | "linkedin" | "pinterest";
55
      userid?: string;
56
      username: string;
57
      url?: string;
58
      verified?: false | true;
59
    }[];
60
    sharedSocials?: {
61
      type: "twitter" | "facebook" | "instagram" | "linkedin" | "pinterest";
62
      userid?: string;
63
      username: string;
64
      url?: string;
65
      verified?: false | true;
66
    }[];
67
    urls?: { type?: "other" | "website" | "blog"; url: string }[];
68
    locations?: {
69
      type?: "home" | "work" | "other";
70
      name?: string;
71
      address?: string;
72
      address2?: string;
73
      address3?: string;
74
      latitude?: number;
75
      longitude?: number;
76
      countryCode?: string;
77
      countryName?: string;
78
      regionCode?: string;
79
      regionName?: string;
80
      cityName?: string;
81
      zipCode?: string;
82
      areaCode?: string;
83
    }[];
84
    locale?: string;
85
    timeZone?: string;
86
    tags?: string[];
87
    sentiment?: { polarity: 0 | 1 | -1; confidence: number };
88
    custom?: {};
89
    birthdayAt?: string;
90
    gender?: "m" | "f";
91
    createdAt?: string;
92
    importedAt?: string;
93
    rev?: number;
94
    defaultLang?: string;
95
  },
96
) {
97
  const url = new URL(`https://api.kustomerapp.com/v1/customers/${id}`);
98
  for (const [k, v] of [["replace", replace]]) {
99
    if (v !== undefined && v !== "" && k !== undefined) {
100
      url.searchParams.append(k, v);
101
    }
102
  }
103
  const response = await fetch(url, {
104
    method: "PUT",
105
    headers: {
106
      "Content-Type": "application/json",
107
      Authorization: "Bearer " + auth.apiKey,
108
    },
109
    body: JSON.stringify(body),
110
  });
111
  if (!response.ok) {
112
    const text = await response.text();
113
    throw new Error(`${response.status} ${text}`);
114
  }
115
  return await response.json();
116
}
117