Create Or Update User

Creates a user if the user does not already exist, or updates an existing user identified by e-mail address or external ID.

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 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