Update a codespace for the authenticated user

Updates a codespace owned by the authenticated user. Currently only the codespace's machine type and recent folders can be modified using this endpoint. If you specify a new machine type it will be applied the next time your codespace is started. You must authenticate using an access token with the `codespace` scope to use this endpoint. GitHub Apps must have write access to the `codespaces` repository permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Update a codespace for the authenticated user
6
 * Updates a codespace owned by the authenticated user. Currently only the codespace's machine type and recent folders can be modified using this endpoint.
7

8
If you specify a new machine type it will be applied the next time your codespace is started.
9

10
You must authenticate using an access token with the `codespace` scope to use this endpoint.
11

12
GitHub Apps must have write access to the `codespaces` repository permission to use this endpoint.
13
 */
14
export async function main(
15
  auth: Github,
16
  codespace_name: string,
17
  body: {
18
    display_name?: string;
19
    machine?: string;
20
    recent_folders?: string[];
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://api.github.com/user/codespaces/${codespace_name}`
26
  );
27

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