0

Creates a new topic, a new post, or a private message

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 new topic, a new post, or a private message
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  body: {
14
    title?: string;
15
    raw: string;
16
    topic_id?: number;
17
    category?: number;
18
    target_recipients?: string;
19
    target_usernames?: string;
20
    archetype?: string;
21
    created_at?: string;
22
    reply_to_post_number?: number;
23
    embed_url?: string;
24
    external_id?: string;
25
  },
26
) {
27
  const url = new URL(`https://${auth.defaultHost}/posts.json`);
28

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