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

Returns a list of message files.

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