0

Upload files

by
Published Oct 17, 2025

You can upload several files at a time by adding parameters to the root of the request body. The name of each parameter will be used as the label of the file once uploaded. E.g: - parameter name: `package-damaged.png` - parameter value: Content of the file.

Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
type Base64 = string;
8
/**
9
 * Upload files
10
 * You can upload several files at a time by adding parameters to the root of the request body. The name of each parameter will be used as the label of the file once uploaded. E.g:
11
  - parameter name: `package-damaged.png`
12
  - parameter value: Content of the file.
13

14
 */
15
export async function main(
16
  auth: Gorgias,
17
  body: {
18
    your_unique_file_label?: {
19
      base64: Base64;
20
      type:
21
        | "image/png"
22
        | "image/jpeg"
23
        | "image/gif"
24
        | "application/pdf"
25
        | "appication/json"
26
        | "text/csv"
27
        | "text/plain"
28
        | "audio/mpeg"
29
        | "audio/wav"
30
        | "video/mp4";
31
      name: string;
32
    };
33
  },
34
) {
35
  const url = new URL(`https://${auth.domain}.gorgias.com/api/upload`);
36

37
  const formData = new FormData();
38
  for (const [k, v] of Object.entries(body)) {
39
    if (v !== undefined) {
40
      if (["your_unique_file_label"].includes(k)) {
41
        const { base64, type, name } = v as {
42
          base64: Base64;
43
          type: string;
44
          name: string;
45
        };
46
        formData.append(
47
          k,
48
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
49
            type,
50
          }),
51
          name,
52
        );
53
      } else {
54
        formData.append(k, String(v));
55
      }
56
    }
57
  }
58
  const response = await fetch(url, {
59
    method: "POST",
60
    headers: {
61
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
62
    },
63
    body: formData,
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71