Initiate video uploads using TUS

Initiates a video upload using the TUS protocol. On success, the server responds with a status code 201 (created) and includes a `location` header to indicate where the content should be uploaded. Refer to https://tus.io for protocol details.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Initiate video uploads using TUS
8
 * Initiates a video upload using the TUS protocol. On success, the server responds with a status code 201 (created) and includes a `location` header to indicate where the content should be uploaded. Refer to https://tus.io for protocol details.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  Tus_Resumable: string,
14
  Upload_Creator: string,
15
  Upload_Length: string,
16
  Upload_Metadata: string
17
) {
18
  const url = new URL(
19
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream`
20
  );
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Tus-Resumable": Tus_Resumable,
26
      "Upload-Creator": Upload_Creator,
27
      "Upload-Length": Upload_Length,
28
      "Upload-Metadata": Upload_Metadata,
29
      "X-AUTH-EMAIL": auth.email,
30
      "X-AUTH-KEY": auth.key,
31
      "Content-Type": "application/json",
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42