0
List assistants
One script reply has been approved by the moderators Verified

Returns a list of assistants.

Created by hugo697 392 days ago Viewed 9561 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 392 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * List assistants
7
 * Returns a list of assistants.
8
 */
9
export async function main(
10
  auth: Openai,
11
  limit: string | undefined,
12
  order: "asc" | "desc" | undefined,
13
  after: string | undefined,
14
  before: string | undefined
15
) {
16
  const url = new URL(`https://api.openai.com/v1/assistants`);
17
  for (const [k, v] of [
18
    ["limit", limit],
19
    ["order", order],
20
    ["after", after],
21
    ["before", before],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      "OpenAI-Organization": auth.organization_id,
31
      Authorization: "Bearer " + auth.api_key,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41