0

Create User

by
Published Apr 8, 2025
Script buttondown Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Buttondown = {
3
  token: string;
4
};
5
/**
6
 * Create User
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  body: {
12
    permissions: {
13
      subscriber?: "none" | "read" | "write";
14
      email?: "none" | "read" | "write";
15
      sending?: "none" | "read" | "write";
16
      styling?: "none" | "read" | "write";
17
      administrivia?: "none" | "read" | "write";
18
      automations?: "none" | "read" | "write";
19
      surveys?: "none" | "read" | "write";
20
    };
21
    email_address: string;
22
  },
23
) {
24
  const url = new URL(`https://api.buttondown.com/v1/users`);
25

26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "Content-Type": "application/json",
30
      Authorization: "Token " + auth.token,
31
    },
32
    body: JSON.stringify(body),
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