Create a live input

Creates a live input, and returns credentials that you or your users can use to stream live video to Cloudflare Stream.

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
 * Create a live input
8
 * Creates a live input, and returns credentials that you or your users can use to stream live video to Cloudflare Stream.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    defaultCreator?: string;
15
    deleteRecordingAfterDays?: number;
16
    meta?: { [k: string]: unknown };
17
    recording?: {
18
      allowedOrigins?: string[];
19
      mode?: "off" | "automatic";
20
      requireSignedURLs?: boolean;
21
      timeoutSeconds?: number;
22
      [k: string]: unknown;
23
    };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream/live_inputs`
29
  );
30

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