0
Get Organization Files
One script reply has been approved by the moderators Verified

Get metadata about a specific file by file ID. The file ID is generated by AI21 when you upload the file.

Created by hugo697 32 days ago Viewed 2773 times
0
Submitted by hugo697 Bun
Verified 32 days ago
1
//native
2
type Ai21 = {
3
  apiKey: string;
4
};
5
/**
6
 * Get Organization Files
7
 * Get metadata about a specific file by file ID. The file ID is generated by
8
AI21 when you upload the file.
9
 */
10
export async function main(
11
  auth: Ai21,
12
  name: string | undefined,
13
  path: string | undefined,
14
  status:
15
    | "DB_RECORD_CREATED"
16
    | "UPLOADED"
17
    | "UPLOAD_FAILED"
18
    | "PROCESSED"
19
    | "PROCESSING_FAILED"
20
    | undefined,
21
  label: string | undefined,
22
  limit: string | undefined,
23
  offset: string | undefined,
24
) {
25
  const url = new URL(`https://api.ai21.com/studio/v1/library/files`);
26
  for (const [k, v] of [
27
    ["name", name],
28
    ["path", path],
29
    ["status", status],
30
    ["label", label],
31
    ["limit", limit],
32
    ["offset", offset],
33
  ]) {
34
    if (v !== undefined && v !== "" && k !== undefined) {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Bearer " + auth.apiKey,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51