0

Completes a direct external upload

by
Published Oct 17, 2025

Completes an external upload initialized with /get-presigned-put.

Script discourse Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Discourse = {
3
  apiKey: string;
4
  defaultHost: string;
5
  apiUsername: string;
6
};
7
/**
8
 * Completes a direct external upload
9
 * Completes an external upload initialized with /get-presigned-put.
10
 */
11
export async function main(
12
  auth: Discourse,
13
  body: {
14
    unique_identifier: string;
15
    for_private_message?: string;
16
    for_site_setting?: string;
17
    pasted?: string;
18
  },
19
) {
20
  const url = new URL(
21
    `https://${auth.defaultHost}/uploads/complete-external-upload.json`,
22
  );
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      "Content-Type": "application/json",
28
      "API-KEY": auth.apiKey,
29
      "API-USERNAME": auth.apiUsername,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39