0

Creates a user

by
Published Oct 17, 2025
Script discourse Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Discourse = {
3
  apiKey: string;
4
  defaultHost: string;
5
  apiUsername: string;
6
};
7
/**
8
 * Creates a user
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  body: {
14
    name: string;
15
    email: string;
16
    password: string;
17
    username: string;
18
    active?: false | true;
19
    approved?: false | true;
20
    user_fields?: { "1"?: false | true };
21
    external_ids?: {};
22
  },
23
) {
24
  const url = new URL(`https://${auth.defaultHost}/users.json`);
25

26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "Content-Type": "application/json",
30
      "API-KEY": auth.apiKey,
31
      "API-USERNAME": auth.apiUsername,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41