Upload Files
One script reply has been approved by the moderators Verified

Uploads a file that can be attached to a ticket comment.

Created by hugo697 844 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 298 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Upload Files
8
 * Uploads a file that can be attached to a ticket comment.
9
 */
10
export async function main(auth: Zendesk) {
11
  const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/uploads`);
12

13
  const response = await fetch(url, {
14
    method: "POST",
15
    headers: {
16
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
17
    },
18
    body: undefined,
19
  });
20
  if (!response.ok) {
21
    const text = await response.text();
22
    throw new Error(`${response.status} ${text}`);
23
  }
24
  return await response.json();
25
}
26