0

Update a collection

by
Published Oct 17, 2025
Script gitbook Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gitbook = {
3
  token: string;
4
};
5
/**
6
 * Update a collection
7
 *
8
 */
9
export async function main(
10
  auth: Gitbook,
11
  collectionId: string,
12
  body: {
13
    title?: string;
14
    description?: string;
15
    defaultLevel?:
16
      | "admin"
17
      | "create"
18
      | "edit"
19
      | "review"
20
      | "comment"
21
      | "read"
22
      | "inherit";
23
  },
24
) {
25
  const url = new URL(`https://api.gitbook.com/v1/collections/${collectionId}`);
26

27
  const response = await fetch(url, {
28
    method: "PATCH",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41