0

Create a new checkout key

by
Published Dec 20, 2024

Not available to projects that use GitLab or GitHub App. Creates a new checkout key. This API request is only usable with a user API token. Please ensure that you have authorized your account with GitHub before creating user keys. This is necessary to give CircleCI the permission to create a user key associated with your GitHub user account. You can find this page by visiting Project Settings > Checkout SSH Keys

Script circleci Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Circleci = {
3
	token: string
4
}
5
/**
6
 * Create a new checkout key
7
 * Not available to projects that use GitLab or GitHub App. Creates a new checkout key. This API request is only usable with a user API token.
8
                           Please ensure that you have authorized your account with GitHub before creating user keys.
9
                           This is necessary to give Circleci the permission to create a user key associated with
10
                           your GitHub user account. You can find this page by visiting Project Settings > Checkout SSH Keys
11
 */
12
export async function main(
13
	auth: Circleci,
14
	project_slug: string,
15
	body: { type: 'user-key' | 'deploy-key' }
16
) {
17
	const url = new URL(`https://circleci.com/api/v2/project/${project_slug}/checkout-key`)
18

19
	const response = await fetch(url, {
20
		method: 'POST',
21
		headers: {
22
			'Content-Type': 'application/json',
23
			'Circle-Token': auth.token
24
		},
25
		body: JSON.stringify(body)
26
	})
27
	if (!response.ok) {
28
		const text = await response.text()
29
		throw new Error(`${response.status} ${text}`)
30
	}
31
	return await response.json()
32
}
33