{"meta":{"title":"Encrypting secrets for the REST API","intro":"In order to create or update a secret with the REST API, you must encrypt the value of the secret.","product":"REST API","breadcrumbs":[{"href":"/en/rest","title":"REST API"},{"href":"/en/rest/guides","title":"Guides"},{"href":"/en/rest/guides/encrypting-secrets-for-the-rest-api","title":"Encrypt secrets"}],"documentType":"article"},"body":"# Encrypting secrets for the REST API\n\nIn order to create or update a secret with the REST API, you must encrypt the value of the secret.\n\n## About encrypting secrets\n\nSeveral REST API endpoints let you create secrets on GitHub. To use these endpoints, you must encrypt the secret value using libsodium. For more information, see the [libsodium documentation](https://libsodium.gitbook.io/doc/bindings_for_other_languages).\n\nIn order to encrypt a secret, you need a Base64 encoded public key. You can get a public key from the REST API. To determine which endpoint to use to get the public key, look at the documentation for the `encrypted_value` parameter in the endpoint that you will use to create a secret .\n\n## Example encrypting a secret using Node.js\n\nIf you are using Node.js, you can encrypt your secret using the libsodium-wrappers library. For more information, see [libsodium-wrappers](https://www.npmjs.com/package/libsodium-wrappers).\n\nIn the following example, replace `YOUR_SECRET` with the plain text value that you want to encrypt. Replace `YOUR_BASE64_KEY` with your Base64 encoded public key. The documentation for the endpoint that you will use to create a secret will tell you which endpoint you can use to get the public key. `ORIGINAL` is not a placeholder; it is a parameter for the libsodium-wrappers library.\n\n```javascript copy\nconst sodium = require('libsodium-wrappers')\n\nconst secret = 'YOUR_SECRET'\nconst key = 'YOUR_BASE64_KEY'\n\n//Check if libsodium is ready and then proceed.\nsodium.ready.then(() => {\n  // Convert the secret and key to a Uint8Array.\n  let binkey = sodium.from_base64(key, sodium.base64_variants.ORIGINAL)\n  let binsec = sodium.from_string(secret)\n\n  // Encrypt the secret using libsodium\n  let encBytes = sodium.crypto_box_seal(binsec, binkey)\n\n  // Convert the encrypted Uint8Array to Base64\n  let output = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL)\n\n  // Print the output\n  console.log(output)\n});\n```\n\n## Example encrypting a secret using Python\n\nIf you are using Python 3, you can encrypt your secret using the PyNaCl library. For more information, see [PyNaCl](https://pynacl.readthedocs.io/en/latest/public/#nacl-public-sealedbox).\n\nIn the following example, replace `YOUR_SECRET` with the plain text value that you want to encrypt. Replace `YOUR_BASE64_KEY` with your Base64 encoded public key. The documentation for the endpoint that you will use to create a secret will tell you which endpoint you can use to get the public key.\n\n```python copy\nfrom base64 import b64encode\nfrom nacl import encoding, public\n\ndef encrypt(public_key: str, secret_value: str) -> str:\n  \"\"\"Encrypt a Unicode string using the public key.\"\"\"\n  public_key = public.PublicKey(public_key.encode(\"utf-8\"), encoding.Base64Encoder())\n  sealed_box = public.SealedBox(public_key)\n  encrypted = sealed_box.encrypt(secret_value.encode(\"utf-8\"))\n  return b64encode(encrypted).decode(\"utf-8\")\n\nencrypt(\"YOUR_BASE64_KEY\", \"YOUR_SECRET\")\n```\n\n## Example encrypting a secret using C#\n\nIf you are using C#, you can encrypt your secret using the Sodium.Core package. For more information, see [Sodium.Core](https://www.nuget.org/packages/Sodium.Core/).\n\nIn the following example, replace `YOUR_SECRET` with the plain text value that you want to encrypt. Replace `YOUR_BASE64_KEY` with your Base64 encoded public key. The documentation for the endpoint that you will use to create a secret will tell you which endpoint you can use to get the public key.\n\n```csharp copy\nvar secretValue = System.Text.Encoding.UTF8.GetBytes(\"YOUR_SECRET\");\nvar publicKey = Convert.FromBase64String(\"YOUR_BASE64_KEY\");\n\nvar sealedPublicKeyBox = Sodium.SealedPublicKeyBox.Create(secretValue, publicKey);\n\nConsole.WriteLine(Convert.ToBase64String(sealedPublicKeyBox));\n```\n\n## Example encrypting a secret using Ruby\n\nIf you are using Ruby, you can encrypt your secret using the RbNaCl gem. For more information, see [RbNaCl](https://github.com/RubyCrypto/rbnacl).\n\nIn the following example, replace `YOUR_SECRET` with the plain text value that you want to encrypt. Replace `YOUR_BASE64_KEY` with your Base64 encoded public key. The documentation for the endpoint that you will use to create a secret will tell you which endpoint you can use to get the public key.\n\n```ruby copy\nrequire \"rbnacl\"\nrequire \"base64\"\n\nkey = Base64.decode64(\"YOUR_BASE64_KEY\")\npublic_key = RbNaCl::PublicKey.new(key)\n\nbox = RbNaCl::Boxes::Sealed.from_public_key(public_key)\nencrypted_secret = box.encrypt(\"YOUR_SECRET\")\n\n# Print the base64 encoded secret\nputs Base64.strict_encode64(encrypted_secret)\n```"}