0

Create a space

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
 * Create a space
7
 *
8
 */
9
export async function main(
10
  auth: Gitbook,
11
  organizationId: string,
12
  body:
13
    | ({
14
        title?: string;
15
        emoji?: string;
16
        parent?: string;
17
        language?:
18
          | "en"
19
          | "fr"
20
          | "de"
21
          | "es"
22
          | "it"
23
          | "pt"
24
          | "pt-br"
25
          | "ru"
26
          | "ja"
27
          | "zh"
28
          | "ko"
29
          | "ar"
30
          | "hi"
31
          | "nl"
32
          | "pl"
33
          | "tr"
34
          | "sv"
35
          | "no"
36
          | "da"
37
          | "fi"
38
          | "el"
39
          | "cs"
40
          | "hu"
41
          | "ro"
42
          | "th"
43
          | "vi"
44
          | "id"
45
          | "ms"
46
          | "he"
47
          | "uk"
48
          | "sk"
49
          | "bg"
50
          | "hr"
51
          | "lt"
52
          | "lv"
53
          | "et"
54
          | "sl";
55
        editMode?: "live" | "locked";
56
      } & {})
57
    | ({
58
        title?: string;
59
        emoji?: string;
60
        parent?: string;
61
        language?:
62
          | "en"
63
          | "fr"
64
          | "de"
65
          | "es"
66
          | "it"
67
          | "pt"
68
          | "pt-br"
69
          | "ru"
70
          | "ja"
71
          | "zh"
72
          | "ko"
73
          | "ar"
74
          | "hi"
75
          | "nl"
76
          | "pl"
77
          | "tr"
78
          | "sv"
79
          | "no"
80
          | "da"
81
          | "fi"
82
          | "el"
83
          | "cs"
84
          | "hu"
85
          | "ro"
86
          | "th"
87
          | "vi"
88
          | "id"
89
          | "ms"
90
          | "he"
91
          | "uk"
92
          | "sk"
93
          | "bg"
94
          | "hr"
95
          | "lt"
96
          | "lv"
97
          | "et"
98
          | "sl";
99
        editMode?: "live" | "locked";
100
      } & { template: { id: string; params?: { contentRefs?: {} } } })
101
    | ({
102
        title?: string;
103
        emoji?: string;
104
        parent?: string;
105
        language?:
106
          | "en"
107
          | "fr"
108
          | "de"
109
          | "es"
110
          | "it"
111
          | "pt"
112
          | "pt-br"
113
          | "ru"
114
          | "ja"
115
          | "zh"
116
          | "ko"
117
          | "ar"
118
          | "hi"
119
          | "nl"
120
          | "pl"
121
          | "tr"
122
          | "sv"
123
          | "no"
124
          | "da"
125
          | "fi"
126
          | "el"
127
          | "cs"
128
          | "hu"
129
          | "ro"
130
          | "th"
131
          | "vi"
132
          | "id"
133
          | "ms"
134
          | "he"
135
          | "uk"
136
          | "sk"
137
          | "bg"
138
          | "hr"
139
          | "lt"
140
          | "lv"
141
          | "et"
142
          | "sl";
143
        editMode?: "live" | "locked";
144
      } & {
145
        computedSource:
146
          | ({
147
              type: "builtin:openapi";
148
              dependencies: {
149
                spec:
150
                  | { ref: { kind: "openapi"; spec: string } }
151
                  | {
152
                      ref: { kind: "openapi"; spec: string };
153
                      value: {
154
                        object: "openapi-spec";
155
                        id: string;
156
                        slug: string;
157
                        lastVersion?: string;
158
                      };
159
                    };
160
              };
161
            } & { props: { models: false | true } })
162
          | {
163
              type: "builtin:translation";
164
              props: {};
165
              dependencies: {
166
                translation:
167
                  | { ref: { kind: "translation"; translation: string } }
168
                  | {
169
                      ref: { kind: "translation"; translation: string };
170
                      value: { space: string; revision: string };
171
                    };
172
              };
173
            }
174
          | { type: string; props: {}; dependencies?: {} };
175
      }),
176
) {
177
  const url = new URL(`https://api.gitbook.com/v1/orgs/${organizationId}/spaces`);
178

179
  const response = await fetch(url, {
180
    method: "POST",
181
    headers: {
182
      "Content-Type": "application/json",
183
      Authorization: "Bearer " + auth.token,
184
    },
185
    body: JSON.stringify(body),
186
  });
187
  if (!response.ok) {
188
    const text = await response.text();
189
    throw new Error(`${response.status} ${text}`);
190
  }
191
  return await response.json();
192
}
193