0

Create a new Custom Field on a Board

by
Published Oct 30, 2023

Create a new Custom Field on a board.

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Create a new Custom Field on a Board
7
 * Create a new Custom Field on a board.
8
 */
9
export async function main(
10
  auth: Trello,
11
  body: {
12
    idModel: string;
13
    modelType: "board";
14
    name: string;
15
    type: "checkbox" | "list" | "number" | "text" | "date";
16
    options?: string;
17
    pos: ("top" | "bottom") | number;
18
    display_cardFront?: boolean;
19
    [k: string]: unknown;
20
  }
21
) {
22
  const url = new URL(`https://api.trello.com/1/customFields`);
23
  for (const [k, v] of [
24
    ["key", auth.key],
25
    ["token", auth.token],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: undefined,
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45