Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:
Environment variables are used to manage configuration and secrets for the Adfluens application, particularly for the backend API. They allow developers to set up API keys, server ports, and other operational parameters without hardcoding them into the source code. This is crucial for security and for maintaining different configurations between development and production environments.
Variables are loaded from a .env file located in the root directory of the monorepo. The backend services consume these variables through a shared module, likely @repo/env, which is a dependency for packages like @repo/db and @repo/auth.
Sources: README.md:31-37, apps/api/src/index.ts:9, packages/db/package.json:28, packages/auth/package.json:25
Setup
To run the application, you must create a .env file in the root of the project. This file should contain the necessary API keys for external services.
Sources: README.md:31-32
Example .env file
Copy the following structure into a new file named .env in the project's root directory and replace the placeholder values with your actual keys.
YOUTUBE_API_KEY=your_youtube_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here
Sources: README.md:34-36
Core Variables
The following table lists the key environment variables used by the backend API.
| Variable | Description | Required | Used In - |
|---|---|---|---|
YOUTUBE_API_KEY | API key for the YouTube Data API v3. Used to fetch channel and video information. | Yes | README.md:34 - |
GEMINI_API_KEY| API key for Google Gemini. Used for all AI-powered analysis and chat features. | Yes |README.md:35,apps/api/src/features/ai/ai.service.ts:15- |PORT| The port on which the backend Express server will run. | No |apps/api/src/index.ts:13- |NODE_ENV| The runtime environment. Set toproductionordevelopment. Affects CORS policy and static file serving. | No |apps/api/src/index.ts:14- |
Architecture and Data Flow
The application uses a centralized module, @repo/env, to load and provide environment variables to different parts of the backend. This ensures consistent access and management of configuration.
The diagram below illustrates how environment variables are loaded from the .env file and consumed by various API services.
This diagram shows the flow of configuration from the local .env file into the application's runtime.
Sources: apps/api/src/index.ts:9, apps/api/src/features/ai/ai.service.ts:9, README.md:31-37
Consumption in Services
API Server (apps/api/src/index.ts)
The main API entry point uses NODE_ENV to determine the environment (development vs. production) and adjust its behavior accordingly, such as setting the CORS policy. It also uses PORT to define which port the Express server listens on.
Sources: apps/api/src/index.ts:13-18
AI Service (apps/api/src/features/ai/ai.service.ts)
The AI service requires the GEMINI_API_KEY to instantiate the GoogleGenAI client, which is necessary for making requests to the Gemini API for video analysis and chat functionalities.
// apps/api/src/features/ai/ai.service.ts:15
const ai = new GoogleGenAI({ apiKey: env.GEMINI_API_KEY });
Sources: apps/api/src/features/ai/ai.service.ts:15
Summary
Environment variables are a fundamental part of the Adfluens application's configuration, providing a secure and flexible way to manage external API keys and server settings. All backend services that depend on external configuration or runtime-specific parameters rely on the centralized @repo/env module, which sources its values from the root .env file. Proper setup of this file is the first and most critical step for local development.