List files

Returns a list of files that belong to the user's organization.

Script openai Verified

by hugo697 ยท 12/1/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 372 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * List files
7
 * Returns a list of files that belong to the user's organization.
8
 */
9
export async function main(auth: Openai, purpose: string | undefined) {
10
  const url = new URL(`https://api.openai.com/v1/files`);
11
  for (const [k, v] of [["purpose", purpose]]) {
12
    if (v !== undefined && v !== "") {
13
      url.searchParams.append(k, v);
14
    }
15
  }
16
  const response = await fetch(url, {
17
    method: "GET",
18
    headers: {
19
      "OpenAI-Organization": auth.organization_id,
20
      Authorization: "Bearer " + auth.api_key,
21
    },
22
    body: undefined,
23
  });
24
  if (!response.ok) {
25
    const text = await response.text();
26
    throw new Error(`${response.status} ${text}`);
27
  }
28
  return await response.json();
29
}
30