Export a codespace for the authenticated user

Triggers an export of the specified codespace and returns a URL and ID where the status of the export can be monitored. If changes cannot be pushed to the codespace's repository, they will be pushed to a new or previously-existing fork instead. You must authenticate using a personal access token with the `codespace` scope to use this endpoint. GitHub Apps must have write access to the `codespaces_lifecycle_admin` repository permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Export a codespace for the authenticated user
6
 * Triggers an export of the specified codespace and returns a URL and ID where the status of the export can be monitored.
7

8
If changes cannot be pushed to the codespace's repository, they will be pushed to a new or previously-existing fork instead.
9

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

12
GitHub Apps must have write access to the `codespaces_lifecycle_admin` repository permission to use this endpoint.
13
 */
14
export async function main(auth: Github, codespace_name: string) {
15
  const url = new URL(
16
    `https://api.github.com/user/codespaces/${codespace_name}/exports`
17
  );
18

19
  const response = await fetch(url, {
20
    method: "POST",
21
    headers: {
22
      Authorization: "Bearer " + auth.token,
23
    },
24
    body: undefined,
25
  });
26
  if (!response.ok) {
27
    const text = await response.text();
28
    throw new Error(`${response.status} ${text}`);
29
  }
30
  return await response.json();
31
}
32