REST API endpoints for Git SSH keys - GitHub Docs (original) (raw)

Use the REST API to manage Git SSH keys of authenticated users.

About Git SSH key administration

If a request URL does not include a {username} parameter then the response will be for the signed-in user (and you must pass authentication information with your request). Additional private information, such as whether a user has two-factor authentication enabled, is included when authenticated through OAuth with the user scope.

List public SSH keys for the authenticated user

Lists the public SSH keys for the authenticated user's GitHub account.

OAuth app tokens and personal access tokens (classic) need the read:public_key scope to use this endpoint.

Fine-grained access tokens for "List public SSH keys for the authenticated user"

This endpoint works with the following fine-grained token types:

The fine-grained token must have the following permission set:

Parameters for "List public SSH keys for the authenticated user"

Headers

Name, Type, Description
accept string Setting to application/vnd.github+json is recommended.

Query parameters

Name, Type, Description
per_page integer The number of results per page (max 100). For more information, see "Using pagination in the REST API."Default: 30
page integer The page number of the results to fetch. For more information, see "Using pagination in the REST API."Default: 1

HTTP response status codes for "List public SSH keys for the authenticated user"

Status code Description
200 OK
304 Not modified
401 Requires authentication
403 Forbidden
404 Resource not found

Code samples for "List public SSH keys for the authenticated user"

Request example

curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/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 } ]

Create a public SSH key for the authenticated user

Adds a public SSH key to the authenticated user's GitHub account.

OAuth app tokens and personal access tokens (classic) need the write:gpg_key scope to use this endpoint.

Fine-grained access tokens for "Create a public SSH key for the authenticated user"

This endpoint works with the following fine-grained token types:

The fine-grained token must have the following permission set:

Parameters for "Create a public SSH key for the authenticated user"

Headers

Name, Type, Description
accept string Setting to application/vnd.github+json is recommended.

Body parameters

Name, Type, Description
title string A descriptive name for the new key.
key string RequiredThe public SSH key to add to your GitHub account.

HTTP response status codes for "Create a public SSH key for the authenticated user"

Status code Description
201 Created
304 Not modified
401 Requires authentication
403 Forbidden
404 Resource not found
422 Validation failed, or the endpoint has been spammed.

Code samples for "Create a public SSH key for the authenticated user"

Request example

curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/user/keys \ -d '{"title":"ssh-rsa AAAAB3NzaC1yc2EAAA","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}'

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 }

Get a public SSH key for the authenticated user

View extended details for a single public SSH key.

OAuth app tokens and personal access tokens (classic) need the read:public_key scope to use this endpoint.

Fine-grained access tokens for "Get a public SSH key for the authenticated user"

This endpoint works with the following fine-grained token types:

The fine-grained token must have the following permission set:

Parameters for "Get a public SSH key for the authenticated user"

Headers

Name, Type, Description
accept string Setting to application/vnd.github+json is recommended.

Path parameters

Name, Type, Description
key_id integer RequiredThe unique identifier of the key.

HTTP response status codes for "Get a public SSH key for the authenticated user"

Status code Description
200 OK
304 Not modified
401 Requires authentication
403 Forbidden
404 Resource not found

Code samples for "Get a public SSH key for the authenticated user"

Request example

curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/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 }

Delete a public SSH key for the authenticated user

Removes a public SSH key from the authenticated user's GitHub account.

OAuth app tokens and personal access tokens (classic) need the admin:public_key scope to use this endpoint.

Fine-grained access tokens for "Delete a public SSH key for the authenticated user"

This endpoint works with the following fine-grained token types:

The fine-grained token must have the following permission set:

Parameters for "Delete a public SSH key for the authenticated user"

Headers

Name, Type, Description
accept string Setting to application/vnd.github+json is recommended.

Path parameters

Name, Type, Description
key_id integer RequiredThe unique identifier of the key.

HTTP response status codes for "Delete a public SSH key for the authenticated user"

Status code Description
204 No Content
304 Not modified
401 Requires authentication
403 Forbidden
404 Resource not found

Code samples for "Delete a public SSH key for the authenticated user"

Request example

delete/user/keys/{key_id}

curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/user/keys/KEY_ID

Response

List public keys for a user

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

Fine-grained access tokens for "List public keys for a user"

This endpoint works with the following fine-grained token types:

The fine-grained token must have the following permission set:

This endpoint can be used without authentication or the aforementioned permissions if only public resources are requested.

Parameters for "List public keys for a user"

Headers

Name, Type, Description
accept string Setting to application/vnd.github+json is recommended.

Path parameters

Name, Type, Description
username string RequiredThe handle for the GitHub user account.

Query parameters

Name, Type, Description
per_page integer The number of results per page (max 100). For more information, see "Using pagination in the REST API."Default: 30
page integer The page number of the results to fetch. For more information, see "Using pagination in the REST API."Default: 1

HTTP response status codes for "List public keys for a user"

Status code Description
200 OK

Code samples for "List public keys for a user"

Request example

get/users/{username}/keys

curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/users/USERNAME/keys

Response

Status: 200

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