0

Create User

by
Published 4 days ago

Create a user in your org with a profile, optional credentials, user type, and group memberships.

Script okta Verified

The script

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

3
/**
4
 * Create User
5
 * Create a user in your org. `profile` holds the Okta user profile (firstName, lastName, email, login, ...). Optionally set `credentials` (e.g. { password: { value } }), assign a user type via `type_id`, and add the user to groups via `group_ids`. When `activate` is true (the default) the user is activated on creation.
6
 */
7
export async function main(
8
  auth: RT.Okta,
9
  profile: { [key: string]: any },
10
  activate: boolean | undefined,
11
  credentials: { [key: string]: any } | undefined,
12
  type_id: string | undefined,
13
  group_ids: string[] | undefined
14
) {
15
  const url = new URL(`${auth.org_url}/api/v1/users`)
16
  if (activate !== undefined)
17
    url.searchParams.append("activate", String(activate))
18

19
  const body: { [key: string]: any } = { profile }
20
  if (credentials !== undefined) body.credentials = credentials
21
  if (type_id !== undefined && type_id !== "") body.type = { id: type_id }
22
  if (group_ids !== undefined && group_ids.length > 0) body.groupIds = group_ids
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      Authorization: `SSWS ${auth.token}`,
28
      "Content-Type": "application/json",
29
      Accept: "application/json",
30
    },
31
    body: JSON.stringify(body),
32
  })
33

34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40