0
Update a SSH key
One script reply has been approved by the moderators Verified

Updates a specific SSH public key on a user's account

Note: Only the 'comment' field can be updated using this API. To modify the key or comment values, you must delete and add the key again.

Example:

$ curl -X PUT -H "Content-Type: application/json" -d '{"label": "Work key"}' https://api.bitbucket.org/2.0/users/{ed08f5e1-605b-4f4a-aee4-6c97628a673e}/ssh-keys/{b15b6026-9c02-4626-b4ad-b905f99f763a}
Created by hugo697 197 days ago Viewed 5870 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 197 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update a SSH key
7
 * Updates a specific SSH public key on a user's account
8

9
Note: Only the 'comment' field can be updated using this API. To modify the key or comment values, you must delete and add the key again.
10

11
Example:
12

13
```
14
$ curl -X PUT -H "Content-Type: application/json" -d '{"label": "Work key"}' https://api.bitbucket.org/2.0/users/{ed08f5e1-605b-4f4a-aee4-6c97628a673e}/ssh-keys/{b15b6026-9c02-4626-b4ad-b905f99f763a}
15
```
16
 */
17
export async function main(
18
  auth: Bitbucket,
19
  key_id: string,
20
  selected_user: string,
21
  body: ({ type: string; [k: string]: unknown } & {
22
    uuid?: string;
23
    key?: string;
24
    comment?: string;
25
    label?: string;
26
    created_on?: string;
27
    last_used?: string;
28
    links?: { self?: { href?: string; name?: string } };
29
    [k: string]: unknown;
30
  }) & {
31
    owner?: { type: string; [k: string]: unknown } & {
32
      links?: {
33
        avatar?: { href?: string; name?: string };
34
        [k: string]: unknown;
35
      };
36
      created_on?: string;
37
      display_name?: string;
38
      username?: string;
39
      uuid?: string;
40
      [k: string]: unknown;
41
    };
42
    [k: string]: unknown;
43
  }
44
) {
45
  const url = new URL(
46
    `https://api.bitbucket.org/2.0/users/${selected_user}/ssh-keys/${key_id}`
47
  );
48

49
  const response = await fetch(url, {
50
    method: "PUT",
51
    headers: {
52
      "Content-Type": "application/json",
53
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
54
    },
55
    body: JSON.stringify(body),
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.json();
62
}
63