A .env file stores environment-variable assignments outside application source code. Backend code reads credentials such as OPENAI_API_KEY or GEMINI_API_KEY from process.env, avoiding hardcoded secrets and supporting provider-specific configuration. section 2.5.5
Use a .env file as part of the local application setup:
- Create the file in the project root before starting the application. section 2.5.5
- Add provider credentials as assignments such as OPENAI_API_KEY=<OPENAI_SECRET_KEY> or GEMINI_API_KEY=<GEMINI_SECRET_KEY>. section 2.8.3
- Load variables in an Express backend with dotenv/config, then retrieve them through process.env. section 2.5.5
- Add the file to .gitignore so credentials are not committed to version control. section 2.5.5
Next.js can load .env.local or .env automatically, so the dotenv package is unnecessary there. Server-side code can read process.env.OPENAI_API_KEY while keeping the key away from the browser. Only variables prefixed with NEXT_PUBLIC_ become part of the client bundle; reserve that prefix for nonsensitive values such as analytics IDs, public endpoints, or feature flags. section 2.8.3
Provider-backed chat configuration
Create a root .env file with OPENAI_API_KEY=<OPENAI_SECRET_KEY>. Load it with import "dotenv/config"; retrieve the value using const apiKey = process.env.OPENAI_API_KEY; and reject startup when it is missing. The backend can then pass the key to the OpenAI client while the frontend receives only the generated response. section 2.5.5
OPENAI_API_KEY=<OPENAI_SECRET_KEY> GEMINI_API_KEY= <GEMINI_SECRET_KEY>
Browse the environment-variable listing for OpenAI and Gemini credential assignment syntax. section 2.8.3



