Skip to main content

Git SSH Keys

List public SSH keys for the authenticated user

Lists the public SSH keys for the authenticated user's GitHub account. Requires that you are authenticated via Basic Auth or via OAuth with at least read:public_key scope.

get /user/keys

Parameters

NameTypeInDescription
acceptstringheader

Setting toapplication/vnd.github.v3+json is recommended.

per_pageintegerquery

Results per page (max 100)

Default: 30

pageintegerquery

Page number of the results to fetch.

Default: 1

Code samples

Example

Shell
curl \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/user/keys
JavaScript @octokit/core.js
await octokit.request('GET /user/keys', {})
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /user/keys

Response

Status: 200
[ { "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234", "id": 2, "url": "https://api.github.com/user/keys/2", "title": "ssh-rsa AAAAB3NzaC1yc2EAAA", "created_at": "2020-06-11T21:31:57Z", "verified": false, "read_only": false }, { "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJy931234", "id": 3, "url": "https://api.github.com/user/keys/3", "title": "ssh-rsa AAAAB3NzaC1yc2EAAB", "created_at": "2020-07-11T21:31:57Z", "verified": false, "read_only": false } ]

Status codes

HTTP Status CodeDescription
200

OK

304

Not modified

401

Requires authentication

403

Forbidden

404

Resource not found

Create a public SSH key for the authenticated user

Adds a public SSH key to the authenticated user's GitHub account. Requires that you are authenticated via Basic Auth, or OAuth with at least write:public_key scope.

post /user/keys

Parameters

NameTypeInDescription
acceptstringheader

Setting toapplication/vnd.github.v3+json is recommended.

titlestringbody

A descriptive name for the new key.

keystringbody

Required. The public SSH key to add to your GitHub account.

Code samples

Example

Shell
curl \ -X POST \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/user/keys
JavaScript @octokit/core.js
await octokit.request('POST /user/keys', {})
GitHub CLI gh api
gh api \ --method POST \ -H "Accept: application/vnd.github.v3+json" \ /user/keys

Response

Status: 201
{ "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234", "id": 2, "url": "https://api.github.com/user/keys/2", "title": "ssh-rsa AAAAB3NzaC1yc2EAAA", "created_at": "2020-06-11T21:31:57Z", "verified": false, "read_only": false }

Status codes

HTTP Status CodeDescription
201

Created

304

Not modified

401

Requires authentication

403

Forbidden

404

Resource not found

422

Validation failed

Get a public SSH key for the authenticated user

View extended details for a single public SSH key. Requires that you are authenticated via Basic Auth or via OAuth with at least read:public_key scope.

get /user/keys/{key_id}

Parameters

NameTypeInDescription
acceptstringheader

Setting toapplication/vnd.github.v3+json is recommended.

key_idintegerpath

key_id parameter

Code samples

Example

Shell
curl \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/user/keys/KEY_ID
JavaScript @octokit/core.js
await octokit.request('GET /user/keys/{key_id}', { key_id: 'KEY_ID' })
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /user/keys/KEY_ID

Response

Status: 200
{ "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234", "id": 2, "url": "https://api.github.com/user/keys/2", "title": "ssh-rsa AAAAB3NzaC1yc2EAAA", "created_at": "2020-06-11T21:31:57Z", "verified": false, "read_only": false }

Status codes

HTTP Status CodeDescription
200

OK

304

Not modified

401

Requires authentication

403

Forbidden

404

Resource not found

Delete a public SSH key for the authenticated user

Removes a public SSH key from the authenticated user's GitHub account. Requires that you are authenticated via Basic Auth or via OAuth with at least admin:public_key scope.

delete /user/keys/{key_id}

Parameters

NameTypeInDescription
acceptstringheader

Setting toapplication/vnd.github.v3+json is recommended.

key_idintegerpath

key_id parameter

Code samples

Example

Shell
curl \ -X DELETE \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/user/keys/KEY_ID
JavaScript @octokit/core.js
await octokit.request('DELETE /user/keys/{key_id}', { key_id: 'KEY_ID' })
GitHub CLI gh api
gh api \ --method DELETE \ -H "Accept: application/vnd.github.v3+json" \ /user/keys/KEY_ID

Response

Status: 204

Status codes

HTTP Status CodeDescription
204

No Content

304

Not modified

401

Requires authentication

403

Forbidden

404

Resource not found

List public keys for a user

Lists the verified public SSH keys for a user. This is accessible by anyone.

get /users/{username}/keys

Parameters

NameTypeInDescription
acceptstringheader

Setting toapplication/vnd.github.v3+json is recommended.

usernamestringpath
per_pageintegerquery

Results per page (max 100)

Default: 30

pageintegerquery

Page number of the results to fetch.

Default: 1

Code samples

Example

Shell
curl \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/users/USERNAME/keys
JavaScript @octokit/core.js
await octokit.request('GET /users/{username}/keys', { username: 'USERNAME' })
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /users/USERNAME/keys

Response

Status: 200
[ { "id": 1, "key": "ssh-rsa AAA..." } ]

Status codes

HTTP Status CodeDescription
200

OK

Notes