Create Invitation

Invite a User to become a Member of an Organization.

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 Invitation
8
 * Invite a User to become a Member of an Organization.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  organization_identifier: string,
13
  body: {
14
    auto_accept?: boolean;
15
    invited_member_email: string;
16
    roles: {
17
      description?: string;
18
      id: string;
19
      name?: string;
20
      permissions?: string[];
21
      [k: string]: unknown;
22
    }[];
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/organizations/${organization_identifier}/invites`
28
  );
29

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