Repository Secrets
The Repository Secrets API allows a user to create, list, and delete secrets (such as access tokens for cloud services) for repositories that the user has access to.
Repository Secrets
The Repository Secrets API allows a user to create, list, and delete secrets (such as access tokens for cloud services) for repositories that the user has access to. These secrets are made available to the codespace at runtime. For more information, see "Managing encrypted secrets for your codespaces."
List repository secrets
Lists all secrets available in a repository without revealing their encrypted values. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have the codespaces_secrets repository permission to use this endpoint.
get /repos/{owner}/{repo}/codespaces/secretsParameters
| Name | Type | In | Description |
|---|---|---|---|
accept | string | header | Setting to |
owner | string | path | |
repo | string | path | |
per_page | integer | query | Results per page (max 100) Default: |
page | integer | query | Page number of the results to fetch. Default: |
Code samples
Example
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secretsawait octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets', {
owner: 'OWNER',
repo: 'REPO'
})gh api \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secretsResponse
Status: 200{
"total_count": 2,
"secrets": [
{
"name": "GH_TOKEN",
"created_at": "2019-08-10T14:59:22Z",
"updated_at": "2020-01-10T14:59:22Z"
},
{
"name": "GIST_ID",
"created_at": "2020-01-10T10:59:22Z",
"updated_at": "2020-01-11T11:59:22Z"
}
]
}Status codes
| HTTP Status Code | Description |
|---|---|
200 | OK |
Notes
Get a repository public key
Gets your public key, which you need to encrypt secrets. You need to encrypt a secret before you can create or update secrets. Anyone with read access to the repository can use this endpoint. If the repository is private you must use an access token with the repo scope. GitHub Apps must have the codespaces_secrets repository permission to use this endpoint.
get /repos/{owner}/{repo}/codespaces/secrets/public-keyParameters
| Name | Type | In | Description |
|---|---|---|---|
accept | string | header | Setting to |
owner | string | path | |
repo | string | path |
Code samples
Example
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secrets/public-keyawait octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets/public-key', {
owner: 'OWNER',
repo: 'REPO'
})gh api \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secrets/public-keyResponse
Status: 200{
"key_id": "012345678912345678",
"key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"
}Status codes
| HTTP Status Code | Description |
|---|---|
200 | OK |
Get a repository secret
Gets a single repository secret without revealing its encrypted value. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have the codespaces_secrets repository permission to use this endpoint.
get /repos/{owner}/{repo}/codespaces/secrets/{secret_name}Parameters
| Name | Type | In | Description |
|---|---|---|---|
accept | string | header | Setting to |
owner | string | path | |
repo | string | path | |
secret_name | string | path | secret_name parameter |
Code samples
Example
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAMEawait octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', {
owner: 'OWNER',
repo: 'REPO',
secret_name: 'SECRET_NAME'
})gh api \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secrets/SECRET_NAMEResponse
Status: 200{
"name": "GH_TOKEN",
"created_at": "2019-08-10T14:59:22Z",
"updated_at": "2020-01-10T14:59:22Z"
}Status codes
| HTTP Status Code | Description |
|---|---|
200 | OK |
Notes
Create or update a repository secret
Creates or updates a repository secret with an encrypted value. Encrypt your secret using
LibSodium. You must authenticate using an access
token with the repo scope to use this endpoint. GitHub Apps must have the codespaces_secrets repository
permission to use this endpoint.
Example of encrypting a secret using Node.js
Encrypt your secret using the tweetsodium library.
const sodium = require('tweetsodium');
const key = "base64-encoded-public-key";
const value = "plain-text-secret";
// Convert the message and key to Uint8Array's (Buffer implements that interface)
const messageBytes = Buffer.from(value);
const keyBytes = Buffer.from(key, 'base64');
// Encrypt using LibSodium.
const encryptedBytes = sodium.seal(messageBytes, keyBytes);
// Base64 the encrypted secret
const encrypted = Buffer.from(encryptedBytes).toString('base64');
console.log(encrypted);
Example of encrypting a secret using Python
Encrypt your secret using pynacl with Python 3.
from base64 import b64encode
from nacl import encoding, public
def encrypt(public_key: str, secret_value: str) -> str:
"""Encrypt a Unicode string using the public key."""
public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
sealed_box = public.SealedBox(public_key)
encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
return b64encode(encrypted).decode("utf-8")
Example of encrypting a secret using C#
Encrypt your secret using the Sodium.Core package.
var secretValue = System.Text.Encoding.UTF8.GetBytes("mySecret");
var publicKey = Convert.FromBase64String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU=");
var sealedPublicKeyBox = Sodium.SealedPublicKeyBox.Create(secretValue, publicKey);
Console.WriteLine(Convert.ToBase64String(sealedPublicKeyBox));
Example of encrypting a secret using Ruby
Encrypt your secret using the rbnacl gem.
require "rbnacl"
require "base64"
key = Base64.decode64("+ZYvJDZMHUfBkJdyq5Zm9SKqeuBQ4sj+6sfjlH4CgG0=")
public_key = RbNaCl::PublicKey.new(key)
box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
encrypted_secret = box.encrypt("my_secret")
# Print the base64 encoded secret
puts Base64.strict_encode64(encrypted_secret)
put /repos/{owner}/{repo}/codespaces/secrets/{secret_name}Parameters
| Name | Type | In | Description |
|---|---|---|---|
accept | string | header | Setting to |
owner | string | path | |
repo | string | path | |
secret_name | string | path | secret_name parameter |
encrypted_value | string | body | Value for your secret, encrypted with LibSodium using the public key retrieved from the Get a repository public key endpoint. |
key_id | string | body | ID of the key you used to encrypt the secret. |
Code samples
Example
curl \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME \
-d '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}'await octokit.request('PUT /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', {
owner: 'OWNER',
repo: 'REPO',
secret_name: 'SECRET_NAME',
encrypted_value: 'c2VjcmV0',
key_id: '012345678912345678'
})gh api \
--method PUT \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME \
-f encrypted_value='c2VjcmV0' -f key_id='012345678912345678'Response when creating a secret
Status: 201Example
curl \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME \
-d '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}'await octokit.request('PUT /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', {
owner: 'OWNER',
repo: 'REPO',
secret_name: 'SECRET_NAME',
encrypted_value: 'c2VjcmV0',
key_id: '012345678912345678'
})gh api \
--method PUT \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME \
-f encrypted_value='c2VjcmV0' -f key_id='012345678912345678'Response when updating a secret
Status: 204Status codes
| HTTP Status Code | Description |
|---|---|
201 | Response when creating a secret |
204 | Response when updating a secret |
Notes
Delete a repository secret
Deletes a secret in a repository using the secret name. You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have the codespaces_secrets repository permission to use this endpoint.
delete /repos/{owner}/{repo}/codespaces/secrets/{secret_name}Parameters
| Name | Type | In | Description |
|---|---|---|---|
accept | string | header | Setting to |
owner | string | path | |
repo | string | path | |
secret_name | string | path | secret_name parameter |
Code samples
Example
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAMEawait octokit.request('DELETE /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', {
owner: 'OWNER',
repo: 'REPO',
secret_name: 'SECRET_NAME'
})gh api \
--method DELETE \
-H "Accept: application/vnd.github.v3+json" \
/repos/OWNER/REPO/codespaces/secrets/SECRET_NAMEResponse
Status: 204Status codes
| HTTP Status Code | Description |
|---|---|
204 | No Content |