0

Update User

by
Published Nov 7, 2023

Update a user by id; only the fields you fill are changed. `tags` replaces the user's tags. Set any other user field through `fields`, e.g. `{ "user_fields": { "plan": "gold" }, "suspended": true }`.

Script zendesk Verified

The script

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

3
/**
4
 * Update User
5
 * Update a user by id; only the fields you fill are changed. `tags` replaces the user's tags. Set any other user field through `fields`, e.g. `{ "user_fields": { "plan": "gold" }, "suspended": true }`.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  user_id: number,
10
  name: string | undefined,
11
  email: string | undefined,
12
  external_id: string | undefined,
13
  role: "end-user" | "agent" | "admin" | undefined,
14
  phone: string | undefined,
15
  organization_id: number | undefined,
16
  tags: string[] | undefined,
17
  fields: { [key: string]: any } | undefined
18
) {
19
  const user = {
20
    ...fields,
21
    name: name || undefined,
22
    email: email || undefined,
23
    external_id: external_id || undefined,
24
    role: role || undefined,
25
    phone: phone || undefined,
26
    organization_id: organization_id ?? undefined,
27
    tags: tags?.length ? tags : undefined,
28
  }
29

30
  const response = await fetch(
31
    `https://${auth.subdomain}.zendesk.com/api/v2/users/${user_id}`,
32
    {
33
      method: "PUT",
34
      headers: {
35
        Authorization: auth.token
36
          ? `Bearer ${auth.token}`
37
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
38
        "Content-Type": "application/json",
39
        Accept: "application/json",
40
      },
41
      body: JSON.stringify({ user }),
42
    }
43
  )
44

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

49
  return await response.json()
50
}
51

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 540 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * Update User
    8
     * Update User
    9
     */
    10
    export async function main(
    11
      auth: Zendesk,
    12
      user_id: string,
    13
      body: {
    14
        user?: {
    15
          custom_role_id?: string;
    16
          email?: string;
    17
          external_id?: string;
    18
          identities?: { type?: string; value?: string; [k: string]: unknown }[];
    19
          name?: string;
    20
          organization?: { name?: string; [k: string]: unknown };
    21
          organization_id?: string;
    22
          role?: string;
    23
          [k: string]: unknown;
    24
        };
    25
        [k: string]: unknown;
    26
      }
    27
    ) {
    28
      const url = new URL(
    29
        `https://${auth.subdomain}.zendesk.com/api/v2/users/${user_id}`
    30
      );
    31
    
    
    32
      const response = await fetch(url, {
    33
        method: "PUT",
    34
        headers: {
    35
          "Content-Type": "application/json",
    36
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    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