0
Get File By Id
One script reply has been approved by the moderators Verified

Retrieve a list of documents in the user's library. Optionally specify a filter to find only files with matching labels or paths. This method returns only metadata about files; to download a file, call GET .../files/{file_id}/download

When specifying qualifiers with your request, only files that match all qualifiers will be returns. So, for example, if you specify label='financial' and status='UPLOADED', only files with the label "financial" AND status UPLOADED will be returned.

Created by hugo697 32 days ago Viewed 2769 times
0
Submitted by hugo697 Bun
Verified 32 days ago
1
//native
2
type Ai21 = {
3
  apiKey: string;
4
};
5
/**
6
 * Get File By Id
7
 * Retrieve a list of documents in the user's library. Optionally specify a
8
filter to find only files with matching labels or paths. This method
9
returns only metadata about files; to download a file, call
10
`GET .../files/{file_id}/download`
11

12
When specifying qualifiers with your request, only files that match
13
_all_ qualifiers will be returns. So, for example, if you specify
14
`label='financial'` and `status='UPLOADED'`, only files with the label
15
"financial" AND status UPLOADED will be returned.
16
 */
17
export async function main(auth: Ai21, file_id: string) {
18
  const url = new URL(
19
    `https://api.ai21.com/studio/v1/library/files/${file_id}`,
20
  );
21

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