Upload LOA Document

Submit LOA document (pdf format) under the account.

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 LOA Document
8
 * Submit LOA document (pdf format) under the account.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: { loa_document: string; [k: string]: unknown }
14
) {
15
  const url = new URL(
16
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/addressing/loa_documents`
17
  );
18

19
  const formData = new FormData();
20
  for (const [k, v] of Object.entries(body)) {
21
    if (v !== undefined && v !== "") {
22
      formData.append(k, String(v));
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "X-AUTH-EMAIL": auth.email,
29
      "X-AUTH-KEY": auth.key,
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: formData,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40