0

Commit upload session

by
Published Oct 17, 2025

Close an upload session and create a file from the uploaded chunks. The actual endpoint URL is returned by the [`Create upload session`](e://post-files-upload-sessions) and [`Get upload session`](e://get-files-upload-sessions-id) endpoints.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Commit upload session
7
 * Close an upload session and create a file from the uploaded chunks.
8

9
The actual endpoint URL is returned by the [`Create upload session`](e://post-files-upload-sessions)
10
and [`Get upload session`](e://get-files-upload-sessions-id) endpoints.
11
 */
12
export async function main(
13
  auth: Box,
14
  upload_session_id: string,
15
  digest: string,
16
  if_match: string,
17
  if_none_match: string,
18
  body: {
19
    parts: { part_id?: string; offset?: number; size?: number } & {
20
      sha1?: string;
21
    }[];
22
  },
23
) {
24
  const url = new URL(
25
    `https://api.box.com/2.0/files/upload_sessions/${upload_session_id}/commit`,
26
  );
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      digest: digest,
32
      "if-match": if_match,
33
      "if-none-match": if_none_match,
34
      "Content-Type": "application/json",
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: JSON.stringify(body),
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