Create a fork

Create a fork for the authenticated user. **Note**: Forking a Repository happens asynchronously. You may have to wait a short period of time before you can access the git objects. If this takes longer than 5 minutes, be sure to contact [GitHub Support](https://support.github.com/contact?tags=dotcom-rest-api).

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
 * Create a fork
6
 * Create a fork for the authenticated user.
7

8
**Note**: Forking a Repository happens asynchronously. You may have to wait a short period of time before you can access the git objects. If this takes longer than 5 minutes, be sure to contact [GitHub Support](https://support.github.com/contact?tags=dotcom-rest-api).
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  body: {
15
    default_branch_only?: boolean;
16
    name?: string;
17
    organization?: string;
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/forks`);
22

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