Naalya Handbook
Secrets and credentials

Adding and rotating

Declare a config secret in the schema, set the value in Railway, and name it with preserve() so it never enters git.

This page is the how-to: add a new config secret, put it on Railway, and rotate one. Never reproduce a real secret value here. Use placeholders such as <your-secret>.

The steps follow infra/README.md and .railway/variables.ts at a5dff0c208d5193c274d3d1f87787b55344d5d30. scripts/sync-railway-env.sh and infra/*/secrets.*.yaml are not in that tree. Do not create a secrets YAML to match an older copy of this page.

Step 1: Declare the key

Say a new integration needs an ACME_API_KEY. Declare it in the Zod schema so boot fails when it is missing. Make it required (z.string()) unless there is a real fallback.

libs/shared/src/config/env.config.ts
const envSchema = z.object({
  // ...existing keys
  ACME_API_KEY: z.string(),
});

Then expose it through the shaped config so services read it via ConfigService instead of process.env directly: group it with related keys.

libs/shared/src/config/env.config.ts
return {
  // ...
  acme: { apiKey: process.env.ACME_API_KEY },
};

Add the name to .env.example as an empty placeholder, and set a development value in .env.local. Do not commit .env.local.

Step 2: Put the value in Railway

Set the value in Railway before you add the name. The README's command, unexecuted here:

railway variable set ACME_API_KEY --stdin -s <service> -e <environment>

Then add 'ACME_API_KEY' to the secrets array for every service that boots with that schema. preserve() keeps the value Railway holds. A name you leave off the list is deleted the next time the Railway config is applied. The comment in variables.ts says that explicitly.

Apply through .github/workflows/railway-config.yml, as Where They Live describes. A missing required key stops that environment from booting. Confirm staging before you repeat the name and the value for production.

Step 3: Rotate a secret

  1. Generate a new value in the provider's dashboard. Keep the old one valid until the new one is in use.
  2. Set the new value in Railway for that environment (railway variable set, or the dashboard). Update .env.local for your own machine. Do not write the value into git.
  3. Let the service pick up the new value. This pass did not measure whether railway variable set redeploys by itself.
  4. Confirm the feature works, then revoke the old value at the provider.

For product secrets, rotation is different and simpler: an API key has a revokedAt column, so ApiKeyService.revoke(id) stamps it and verify immediately stops matching it, mint a fresh key, hand it over, revoke the old. No redeploy needed, because product secrets live in the database, not the environment. See Secrets at Rest for how those are stored.

Rotate, don't reuse, and never log a secret

When a value is compromised (or a laptop with .env.local walks off), rotate it; don't assume it's fine. And when you read a secret in code, route it through ConfigService and never console.log it, never put it in an error message, never put it on a log line or a trace attribute. The api-key mapper omitting keyHash is the pattern: secrets should have no path to an output.

Never-commit rules

Two mechanisms keep config secrets out of git.

.gitignore blocks every .env variant. Local secret files can't be committed even by accident, because every dotenv filename listed there is ignored. Confirm the list in the file before you rely on a name that is not shown below.

.gitignore
# dotenv environment variable files
.env
.env.local
.env.development
.env.production
.env.staging
.env.prod

Do not add a secrets YAML to bring the old flow back. Those files are gone. A private repository is not a reason to commit a live credential. Secret names belong in the secrets arrays. Values belong in Railway and in .env.local.

.env.example lists names, not live values. Task env copies it to .env.local. Keep real secrets out of the example file. envSchema is what boot enforces.

Gotchas

A few traps that bite people working with secrets here.

A settings value is still not a secret

.railway/variables.ts commits non-secret settings in full. That does not make it a place for API keys, database URLs, or webhook secrets. Those names go in secrets and resolve through preserve(). A real value in a fixture, a comment, a script, or a handbook page is a leak. Use <your-secret> everywhere else.

Build-time vs runtime: secrets are a runtime concern

Config secrets are read from process.env when the app runs, not when it builds. The Docker image / compiled bundle should contain no secrets: they're injected by Railway at deploy time (or loaded from .env.local at startup locally). If you find yourself wanting a secret available during pnpm build, stop and reconsider: baking a secret into a build artifact means it ships inside the image, which is exactly what we're avoiding.

A secret read but never validated

It's easy to add process.env.NEW_THING straight into a service and skip the schema. Don't. A key that isn't in env.config.ts is never validated, so a typo or a missing value won't fail at boot: it'll surface as undefined deep in a request, often as a confusing downstream error from the third-party SDK. Every secret goes through the Zod schema first; that's what turns "mysterious 3am failure" into "the app refused to start and told you which key was missing."

Where to go next

On this page