Skip to content
AI GatewayAI Gateway Manual

Secrets and .env

How the .env file is created, which values are generated versus operator-supplied, how to back up the field encryption key, and safe handling rules.

AI Gateway keeps all runtime configuration, including generated secrets, in a single .env file in the version directory (for example aig/2026.11.01/.env). Understanding how this file is created and managed is essential for safe operation and disaster recovery.

The installer creates .env from env.template only when the file does not already exist. If .env is present, the installer logs a message and leaves every value unchanged:

[aig] .env exists; keeping every value

This means:

  • Secrets are generated exactly once, on first install.
  • Re-running ./install.sh install on an existing installation never rotates or overwrites secrets.
  • Passing environment variables such as AIG_HTTP_PORT or AIG_DATABASE_URL on the command line has no effect if .env already exists. To change those values after installation, edit .env directly and restart the affected services.

The following values are generated with openssl rand when .env is created:

KeyHow generatedPurpose
POSTGRES_PASSWORDopenssl rand -hex 24Password for the bundled PostgreSQL service.
REDIS_PASSWORDopenssl rand -hex 24Password for the bundled Redis service.
DB_FIELD_ENCRYPTION_KEYopenssl rand -base64 32AES key used to encrypt sensitive fields in the database (IdP client secrets, provider API keys, etc.).
AIG_BOOTSTRAP_TOKENopenssl rand -hex 32One-time token used to authenticate the initial setup session.

POSTGRES_PASSWORD and REDIS_PASSWORD are always generated, even when an external database or Redis is configured, because Docker Compose interpolates those variables from .env regardless of which profiles are active.

The installer sets the following values from flags and environment variables at creation time:

KeySource
VERSIONRead from the VERSION file in the package directory, or --version <version>.
AIG_HTTP_PORTAIG_HTTP_PORT environment variable (default 80).
APP_ENVSet to local for --hostname localhost; otherwise prod.
MANAGEMENT_BASE_URLDerived from --hostname and AIG_PUBLIC_PORT. To change it later, see Change the hostname.
MANAGEMENT_FRONTEND_URLSame as MANAGEMENT_BASE_URL.
MCP_OAUTH_CALLBACK_URL<base>/api/mcp/oauth/callback.
DATABASE_URLAIG_DATABASE_URL environment variable (empty means use the bundled service).
REDIS_URLAIG_REDIS_URL environment variable (empty means use the bundled service).
COMPOSE_PROFILESSet to postgres,redis unless a URL was provided for each service.

After creation, install.sh upgrade copies .env into the new version directory, updates VERSION, and appends any keys that the new env.template adds but the existing .env does not yet contain.

CODEX_ENABLED is an optional value the installer never writes. While it is unset or false, the console hides the OpenAI Codex management UI. To show it, add CODEX_ENABLED=true to .env and recreate the web container with docker compose up -d web.

Only the literals true and false are accepted; any other value keeps the web container from starting.

DB_FIELD_ENCRYPTION_KEY is the most critical value in .env. It is an AES key used to encrypt sensitive columns in the database. If this key is lost:

  • All encrypted settings become unreadable: IdP client secrets, LLM provider API keys, and any other secret field stored in the database.
  • Restoring from a database dump alone is not sufficient. The dump must be restored together with the .env that was current when the dump was taken.
  • There is no recovery path without the key. Contact support only if you have the original key value.

The installer logs a reminder immediately after creating .env:

[aig] created /path/to/aig/2026.11.01/.env; back it up now, it holds the field encryption key

Back up .env immediately after installation and after every upgrade:

Terminal window
cp -p aig/current/.env /secure/backup/location/aig-<version>.env

Store the backup in a location that is:

  • Accessible only to authorized operators (the file contains plain-text secrets).
  • Independent of the host being backed up (so it survives a host failure).
  • Retained alongside the corresponding database dump (a dump is unreadable without its matching key).

The installer creates .env with mode 600 (owner read/write only). Do not widen the permissions:

Terminal window
ls -l aig/current/.env
# -rw------- 1 root root ... aig/current/.env
  • Never commit .env to a version control system. It contains plain-text secrets.
  • Never regenerate .env on a running installation. Doing so creates a new DB_FIELD_ENCRYPTION_KEY, which makes all previously encrypted database values unreadable. If you accidentally delete .env, restore from backup before attempting any other recovery step.
  • Do not set AIG_FORCE_ENABLE_BOOTSTRAP in .env manually. This variable is set only by ./install.sh recover at runtime. Leaving it in .env keeps the gateway in recovery mode indefinitely.
  • Do not share .env in support tickets or logs. Redact the secrets before sharing diagnostic information.

.env changes do not take effect with docker compose restart. After editing .env, recreate the affected container:

Terminal window
cd aig/current
docker compose up -d

For the VERSION key, use ./install.sh upgrade <version> instead of editing .env directly.