0

Create or Update User

by
Published Nov 7, 2023

Create a user, or update the existing one matched by `email` or `external_id` (only the fields you fill are changed). `name` is required when the user is new. Set any other user field through `fields`, e.g. `{ "user_fields": { "plan": "gold" }, "time_zone": "Paris" }`.

Script zendesk Verified

The script

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

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

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

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

48
  return await response.json()
49
}
50

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 533 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * Create Or Update User
    8
     * Creates a user if the user does not already exist, or updates an existing user
    9
    identified by e-mail address or external ID.
    10
     */
    11
    export async function main(
    12
      auth: Zendesk,
    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/create_or_update`
    30
      );
    31
    
    
    32
      const response = await fetch(url, {
    33
        method: "POST",
    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