0

Create Label

by
Published today

Create a user label. Requires gmail.labels or gmail.modify on the connection.

Script gmail Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
/**
4
 * Create Label
5
 * Create a user label. Nest it under another label with a slash in the name (`Clients/Acme`). Colors must come from Gmail's fixed palette (e.g. `#fb4c2f`, `#16a766`, `#4a86e8`, text `#ffffff`); set both or neither. Requires gmail.labels or gmail.modify on the connection.
6
 */
7
export async function main(
8
  auth: RT.Gmail,
9
  name: string,
10
  label_list_visibility: "labelShow" | "labelShowIfUnread" | "labelHide" = "labelShow",
11
  message_list_visibility: "show" | "hide" = "show",
12
  background_color: string | undefined,
13
  text_color: string | undefined
14
) {
15
  const response = await fetch(
16
    "https://gmail.googleapis.com/gmail/v1/users/me/labels",
17
    {
18
      method: "POST",
19
      headers: {
20
        Authorization: `Bearer ${auth.token}`,
21
        "Content-Type": "application/json",
22
        Accept: "application/json",
23
      },
24
      body: JSON.stringify({
25
        name,
26
        labelListVisibility: label_list_visibility || "labelShow",
27
        messageListVisibility: message_list_visibility || "show",
28
        color:
29
          background_color || text_color
30
            ? { backgroundColor: background_color, textColor: text_color }
31
            : undefined,
32
      }),
33
    }
34
  )
35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41