0

Update username

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
 * Update username
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  username: string,
14
  body: { new_username: string },
15
) {
16
  const url = new URL(
17
    `https://${auth.defaultHost}/u/${username}/preferences/username.json`,
18
  );
19

20
  const response = await fetch(url, {
21
    method: "PUT",
22
    headers: {
23
      "Content-Type": "application/json",
24
      "API-KEY": auth.apiKey,
25
      "API-USERNAME": auth.apiUsername,
26
    },
27
    body: JSON.stringify(body),
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.text();
34
}
35