0

Creates a category

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 category
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  body: {
14
    name: string;
15
    color?: string;
16
    text_color?: string;
17
    parent_category_id?: number;
18
    allow_badges?: false | true;
19
    slug?: string;
20
    topic_featured_links_allowed?: false | true;
21
    permissions?: { everyone?: number; staff?: number };
22
    search_priority?: number;
23
    form_template_ids?: unknown[];
24
  },
25
) {
26
  const url = new URL(`https://${auth.defaultHost}/categories.json`);
27

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