0
Create watermark profiles via basic upload
One script reply has been approved by the moderators Verified

Creates watermark profiles using a single HTTP POST multipart/form-data request.

Created by hugo697 447 days ago Viewed 13311 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 447 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create watermark profiles via basic upload
8
 * Creates watermark profiles using a single `HTTP POST multipart/form-data` request.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    file: string;
15
    name?: string;
16
    opacity?: number;
17
    padding?: number;
18
    position?: string;
19
    scale?: number;
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream/watermarks`
25
  );
26

27
  const formData = new FormData();
28
  for (const [k, v] of Object.entries(body)) {
29
    if (v !== undefined && v !== "") {
30
      formData.append(k, String(v));
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "X-AUTH-EMAIL": auth.email,
37
      "X-AUTH-KEY": auth.key,
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: formData,
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