Skip to main content

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/secrets

Parameters

NameTypeInDescription
acceptstringheader

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

ownerstringpath
repostringpath
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/repos/OWNER/REPO/codespaces/secrets
JavaScript @octokit/core.js
await octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets', { owner: 'OWNER', repo: 'REPO' })
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /repos/OWNER/REPO/codespaces/secrets

Response

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 CodeDescription
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-key

Parameters

NameTypeInDescription
acceptstringheader

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

ownerstringpath
repostringpath

Code samples

Example

Shell
curl \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/OWNER/REPO/codespaces/secrets/public-key
JavaScript @octokit/core.js
await octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets/public-key', { owner: 'OWNER', repo: 'REPO' })
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /repos/OWNER/REPO/codespaces/secrets/public-key

Response

Status: 200
{ "key_id": "012345678912345678", "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234" }

Status codes

HTTP Status CodeDescription
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

NameTypeInDescription
acceptstringheader

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

ownerstringpath
repostringpath
secret_namestringpath

secret_name parameter

Code samples

Example

Shell
curl \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME
JavaScript @octokit/core.js
await octokit.request('GET /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', { owner: 'OWNER', repo: 'REPO', secret_name: 'SECRET_NAME' })
GitHub CLI gh api
gh api \ -H "Accept: application/vnd.github.v3+json" \ /repos/OWNER/REPO/codespaces/secrets/SECRET_NAME

Response

Status: 200
{ "name": "GH_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z" }

Status codes

HTTP Status CodeDescription
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

NameTypeInDescription
acceptstringheader

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

ownerstringpath
repostringpath
secret_namestringpath

secret_name parameter

encrypted_valuestringbody

Value for your secret, encrypted with LibSodium using the public key retrieved from the Get a repository public key endpoint.

key_idstringbody

ID of the key you used to encrypt the secret.

Code samples

Example

Shell
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"}'
JavaScript @octokit/core.js
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' })
GitHub CLI gh api
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: 201

Example

Shell
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"}'
JavaScript @octokit/core.js
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' })
GitHub CLI gh api
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: 204

Status codes

HTTP Status CodeDescription
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

NameTypeInDescription
acceptstringheader

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

ownerstringpath
repostringpath
secret_namestringpath

secret_name parameter

Code samples

Example

Shell
curl \ -X DELETE \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/OWNER/REPO/codespaces/secrets/SECRET_NAME
JavaScript @octokit/core.js
await octokit.request('DELETE /repos/{owner}/{repo}/codespaces/secrets/{secret_name}', { owner: 'OWNER', repo: 'REPO', secret_name: 'SECRET_NAME' })
GitHub CLI gh api
gh api \ --method DELETE \ -H "Accept: application/vnd.github.v3+json" \ /repos/OWNER/REPO/codespaces/secrets/SECRET_NAME

Response

Status: 204

Status codes

HTTP Status CodeDescription
204

No Content

Notes