
Nakama reads two kinds of secret from its environment:

- `NAKAMA_SECRETS_KEY`, the master key that encrypts saved Settings keys, Telegram and Discord bot tokens, and [scheduled backups](/backup-restore#scheduled-backups). It is read **only** as an environment variable.
- Key overrides such as `OPENAI_API_KEY` or `ANTHROPIC_API_KEY`. Each one can also come from a file: set `OPENAI_API_KEY_FILE=/run/secrets/openai` and Nakama reads that file instead. A direct variable wins over its `_FILE` form.

Whatever the manager, the rules are the same:

- Pin the master key to one secret **version**, never "latest". A new version picked up on restart is a different key, and Nakama stops at startup because it cannot decrypt what it saved.
- Keep the key out of the data root and out of `NAKAMA_BACKUP_DIR`.
- Give every Nakama process that shares the database the same value.

## HashiCorp Vault

Render an env file with Vault Agent and pass it to the container:

```hcl
template {
  destination = "/run/nakama/secrets.env"
  perms       = "0600"
  contents    = <<EOT
NAKAMA_SECRETS_KEY={{ with secret "kv/data/nakama" }}{{ .Data.data.secrets_key }}{{ end }}
EOT
}
```

```bash
docker run --env-file /run/nakama/secrets.env -v nakama-data:/nakama/data ghcr.io/ahmadrosid/nakama:latest
```

Render each override to its own file and point the `_FILE` variable at it. A KV v2 secret keeps its versions, so pin `?version=` in the path when you [rotate](#rotating-the-master-key).

## AWS Secrets Manager

On ECS, map the secret into the task definition. The ARN suffix pins the version:

```json
"secrets": [
  {
    "name": "NAKAMA_SECRETS_KEY",
    "valueFrom": "arn:aws:secretsmanager:region:account:secret:nakama-secrets-key::AWSCURRENT:"
  }
]
```

Replace `AWSCURRENT` with a version stage or ID you control before a [rotation](#rotating-the-master-key). On a VM, fetch it at boot with `aws secretsmanager get-secret-value --secret-id nakama-secrets-key --version-id <id> --query SecretString --output text` into an env file under `/run`.

## GCP Secret Manager

On a VM, fetch a pinned version at boot:

```bash
printf 'NAKAMA_SECRETS_KEY=%s\n' \
  "$(gcloud secrets versions access 3 --secret=nakama-secrets-key)" \
  > /run/nakama/secrets.env
```

On GKE, sync it into a Kubernetes Secret with the External Secrets Operator, pinned to a version, then expose it as an environment variable named `NAKAMA_SECRETS_KEY`. The same operator also works for Vault and AWS. See [rotation](#rotating-the-master-key) before changing the version.

## Rotating the master key

1. Stop every Nakama process and take a backup.
2. Add the new key as a new secret version. Leave the old version in place.
3. Start Nakama once with `NAKAMA_SECRETS_KEY` set to the old version and `NAKAMA_SECRETS_KEY_NEXT` set to the new one. It re-encrypts the saved values and exits.
4. Point `NAKAMA_SECRETS_KEY` at the new version and start Nakama normally.

Keep the old version while any backup made with it might still be restored.
