0

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

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