Upload videos via direct upload URLs

Creates a direct upload that allows video uploads without an API key.

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
 * Upload videos via direct upload URLs
8
 * Creates a direct upload that allows video uploads without an API key.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  Upload_Creator: string,
14
  body: {
15
    allowedOrigins?: string[];
16
    creator?: string;
17
    expiry?: string;
18
    maxDurationSeconds: number;
19
    meta?: { [k: string]: unknown };
20
    requireSignedURLs?: boolean;
21
    scheduledDeletion?: string;
22
    thumbnailTimestampPct?: number;
23
    watermark?: { uid?: string; [k: string]: unknown };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream/direct_upload`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Upload-Creator": Upload_Creator,
35
      "X-AUTH-EMAIL": auth.email,
36
      "X-AUTH-KEY": auth.key,
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48