0

Get a user by external_id

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
 * Get a user by external_id
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  external_id: string,
14
) {
15
  const url = new URL(
16
    `https://${auth.defaultHost}/u/by-external/${external_id}.json`,
17
  );
18

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