When base64 in environment variables makes sense
Environment variables are single-line strings. Base64 turns anything into a safe single line of A-Z a-z 0-9 + / =, which is why it shows up in three places:
| Use | Why base64 | Decode where |
|---|---|---|
Kubernetes Secret data: | the API requires it (or use stringData: for plain text) | Kubernetes, before injecting into the pod |
| PEM keys, certificates | newlines get lost in many CI and hosting UIs | your app at startup |
| JSON service accounts (Google Cloud, Firebase) | quotes and newlines break .env parsing | your app at startup |
For everything else, store the plain value. Encoding without a reason only makes reviews harder.
Decoding a base64 variable in your app
// Node.js
const privateKey = Buffer.from(process.env.PRIVATE_KEY_B64!, "base64").toString("utf8");
// Python
// import base64, os
// private_key = base64.b64decode(os.environ["PRIVATE_KEY_B64"]).decode()
// Go
// key, _ := base64.StdEncoding.DecodeString(os.Getenv("PRIVATE_KEY_B64"))Name such variables with a _B64 suffix so nobody pastes the decoded value by mistake.
Command-line equivalents
# encode one value (-n: no trailing newline)
echo -n 'my-secret-key' | base64
# encode a file, one line, no wrapping (GNU coreutils; macOS needs no -w)
base64 -w 0 service-account.json
# decode a value from a Kubernetes Secret
kubectl get secret app-env -o jsonpath='{.data.API_KEY}' | base64 --decodeTo turn a whole .env into a ready-made Secret manifest, use the ENV to Docker converter (Kubernetes output) or kubectl create secret generic --from-env-file=.env.