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.
Creation rules
Section titled “Creation rules”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 valueThis means:
- Secrets are generated exactly once, on first install.
- Re-running
./install.sh installon an existing installation never rotates or overwrites secrets. - Passing environment variables such as
AIG_HTTP_PORTorAIG_DATABASE_URLon the command line has no effect if.envalready exists. To change those values after installation, edit.envdirectly and restart the affected services.
Generated values
Section titled “Generated values”The following values are generated with openssl rand when .env is created:
| Key | How generated | Purpose |
|---|---|---|
POSTGRES_PASSWORD | openssl rand -hex 24 | Password for the bundled PostgreSQL service. |
REDIS_PASSWORD | openssl rand -hex 24 | Password for the bundled Redis service. |
DB_FIELD_ENCRYPTION_KEY | openssl rand -base64 32 | AES key used to encrypt sensitive fields in the database (IdP client secrets, provider API keys, etc.). |
AIG_BOOTSTRAP_TOKEN | openssl rand -hex 32 | One-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.
Operator-supplied values
Section titled “Operator-supplied values”The installer sets the following values from flags and environment variables at creation time:
| Key | Source |
|---|---|
VERSION | Read from the VERSION file in the package directory, or --version <version>. |
AIG_HTTP_PORT | AIG_HTTP_PORT environment variable (default 80). |
APP_ENV | Set to local for --hostname localhost; otherwise prod. |
MANAGEMENT_BASE_URL | Derived from --hostname and AIG_PUBLIC_PORT. To change it later, see Change the hostname. |
MANAGEMENT_FRONTEND_URL | Same as MANAGEMENT_BASE_URL. |
MCP_OAUTH_CALLBACK_URL | <base>/api/mcp/oauth/callback. |
DATABASE_URL | AIG_DATABASE_URL environment variable (empty means use the bundled service). |
REDIS_URL | AIG_REDIS_URL environment variable (empty means use the bundled service). |
COMPOSE_PROFILES | Set 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
Section titled “CODEX_ENABLED”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.
The DB_FIELD_ENCRYPTION_KEY warning
Section titled “The DB_FIELD_ENCRYPTION_KEY warning”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
.envthat 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 keyBacking up .env
Section titled “Backing up .env”Back up .env immediately after installation and after every upgrade:
cp -p aig/current/.env /secure/backup/location/aig-<version>.envStore 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).
File permissions
Section titled “File permissions”The installer creates .env with mode 600 (owner read/write only). Do not
widen the permissions:
ls -l aig/current/.env# -rw------- 1 root root ... aig/current/.envSafe handling rules
Section titled “Safe handling rules”- Never commit
.envto a version control system. It contains plain-text secrets. - Never regenerate
.envon a running installation. Doing so creates a newDB_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_BOOTSTRAPin.envmanually. This variable is set only by./install.sh recoverat runtime. Leaving it in.envkeeps the gateway in recovery mode indefinitely. - Do not share
.envin support tickets or logs. Redact the secrets before sharing diagnostic information.
Changing a value after installation
Section titled “Changing a value after installation”.env changes do not take effect with docker compose restart. After editing
.env, recreate the affected container:
cd aig/currentdocker compose up -dFor the VERSION key, use ./install.sh upgrade <version> instead of editing
.env directly.