Configuration & Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
- packages/twenty-server/src/engine/core-modules/twenty-config/twenty-config.service.ts
- packages/twenty-front/src/modules/client-config/services/client-config.service.ts
- packages/twenty-utils/dangerfile.ts
- packages/twenty-docker/k8s/terraform/README.md
- packages/twenty-front/src/pages/settings/ai/components/SettingsAIMCP.tsx
- packages/create-twenty-app/README.md
The Twenty application suite utilizes a robust and multi-layered configuration system to manage settings across its backend, frontend, and deployment environments. The primary mechanism for configuration is environment variables, which are processed and exposed differently in the twenty-server and twenty-front packages. This ensures a secure separation of concerns, where sensitive keys are kept on the server while public, client-safe values are exposed to the frontend.
The system is designed for flexibility, supporting local development via .env files and production deployments through container environments like Docker and Kubernetes. A central metadata file, config-variables.ts, acts as the source of truth for documenting and defining all backend environment variables, which is enforced through CI checks.
Backend Configuration (twenty-server)
The backend configuration is managed by the TwentyConfigService, which is a wrapper around the standard NestJS ConfigService. It provides strongly-typed access to environment variables defined for the server.
Sources: packages/twenty-server/src/engine/core-modules/twenty-config/twenty-config.service.ts
Configuration Loading Flow
The backend server loads configuration variables from environment variables at startup. The TwentyConfigService then provides a typed interface to access these values throughout the application.
This diagram illustrates the flow of configuration data from the environment to the application modules.
Sources: packages/twenty-server/src/engine/core-modules/twenty-config/twenty-config.service.ts:10-14
TwentyConfigService
This service is the central point for accessing all configuration variables on the backend. It uses generics to provide type safety based on the definitions in config-variables.ts.
Key methods:
get<T extends keyof ConfigVariables>(key: T): ConfigVariables[T]['value']: Retrieves a configuration value by its key, returning it with the correct type.isCloud(): boolean: A helper method to check if the application is running in the Twenty cloud environment.
Sources: packages/twenty-server/src/engine/core-modules/twenty-config/twenty-config.service.ts:12-20
config-variables.ts
This file is the single source of truth for all backend environment variables. It defines the metadata for each variable, including its type, a description, and whether it is required. This metadata is crucial for documentation and for CI checks that ensure all new variables are properly recorded.
An example variable definition:
// packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts:13-18
export const CONFIG_VARIABLES = {
NODE_ENV: {
type: 'string',
required: true,
description: 'The environment the application is running in.',
value: process.env.NODE_ENV,
},
// ... more variables
}
Sources: packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
Key Backend Environment Variables
The following table lists some of the core environment variables used by twenty-server.
| Variable | Type | Required | Description |
|---|---|---|---|
NODE_ENV | string | Yes | The environment the application is running in. |
IS_CLOUD | boolean | No | Set to true if running in Twenty's cloud environment. |
DATABASE_URL | string | Yes | The connection string for the PostgreSQL database. |
REDIS_URL | string | Yes | The connection string for the Redis instance. |
SERVER_BASE_URL | string | Yes | The base URL of the server. |
ACCESS_TOKEN_SECRET | string | Yes | Secret for signing access tokens. |
LOGIN_TOKEN_SECRET | string | Yes | Secret for signing login tokens. |
REFRESH_TOKEN_SECRET | string | Yes | Secret for signing refresh tokens. |
Sources: packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts:13-109
Frontend Configuration (twenty-front)
The frontend application (twenty-front) does not have direct access to environment variables for security reasons. Instead, it fetches a curated set of client-safe configuration values from a dedicated backend endpoint upon initialization.
ClientConfigService
This service is responsible for fetching and providing configuration to the frontend application. It makes a single API call to /client-config and stores the result for other parts of the application to use.
The service exposes a getConfig() method that returns a promise resolving to the ClientConfig object.
Sources: packages/twenty-front/src/modules/client-config/services/client-config.service.ts
Frontend Configuration Flow
The following diagram shows how the frontend retrieves its configuration from the backend.
This sequence shows the client-side application fetching its configuration from the server API on startup.
Sources: packages/twenty-front/src/modules/client-config/services/client-config.service.ts:18-24
Client-Side Variables
The frontend receives a ClientConfig object containing only non-sensitive information.
| Property | Type | Description |
|---|---|---|
isCloud | boolean | Whether the instance is a Twenty cloud deployment. |
isTelemetryEnabled | boolean | Whether telemetry is enabled for the instance. |
releaseVersion | string | The current version of the Twenty application. |
Sources: packages/twenty-front/src/modules/client-config/services/client-config.service.ts:5-11
Additionally, some build-time environment variables are directly accessible in the frontend code, such as REACT_APP_SERVER_BASE_URL, which is used to construct API URLs.
// packages/twenty-front/src/pages/settings/ai/components/SettingsAIMCP.tsx:50
const generateMcpContent = (pathSuffix: string, serverName: string) => {
return JSON.stringify(
{
mcpServers: {
[serverName]: {
type: 'streamable-http',
url: `${REACT_APP_SERVER_BASE_URL}${pathSuffix}`,
// ...
},
},
},
null,
2,
);
};
Sources: packages/twenty-front/src/pages/settings/ai/components/SettingsAIMCP.tsx:48-64
Deployment Configuration (Kubernetes & Terraform)
For production-like deployments using Kubernetes, Terraform is used to manage the infrastructure, including the configuration passed to the application containers. The Terraform variables defined in the k8s module directly map to environment variables or secrets used by the deployments.
Sources: packages/twenty-docker/k8s/terraform/README.md
Terraform Input Variables
The following table summarizes the key configuration inputs for a Terraform-managed deployment.
| Terraform Input | Description | Maps to... |
|---|---|---|
twentycrm_app_hostname | The public-facing hostname of the CRM. | SERVER_BASE_URL |
twentycrm_pgdb_admin_password | Password for the PostgreSQL database. | Kubernetes Secret / DATABASE_URL |
twentycrm_server_image | The Docker image tag for the server and worker. | Container image version |
twentycrm_db_pv_capacity | Storage capacity for the database persistent volume. | PVC configuration |
Sources: packages/twenty-docker/k8s/terraform/README.md
Governance and Contribution Workflow
To maintain consistency and ensure all configuration options are documented, the project uses a dangerfile.ts script in its CI/CD pipeline. This script checks for changes related to environment variables.
Adding a New Environment Variable
When a developer adds a new environment variable, they must perform two actions:
- Add the new variable to
.env.example. - Add the corresponding metadata for the variable to
config-variables.ts.
The Danger CI check will fail if .env.example is modified without a corresponding change to config-variables.ts, enforcing documentation.
This flowchart shows the enforced workflow for adding new environment variables.
Sources: packages/twenty-utils/dangerfile.ts:19-27
Summary
Twenty's configuration system is thoughtfully designed to be secure, scalable, and developer-friendly. It clearly separates backend and frontend concerns, with the server acting as the single source of truth for all settings. The frontend fetches a limited, safe subset of this configuration, while deployment tools like Terraform provide a structured way to manage environment-specific variables for production. Finally, CI checks enforce a strict documentation and contribution process, ensuring that the configuration landscape remains manageable and well-understood as the project evolves.