.env · generator · Python (FastAPI / Flask)

Python (FastAPI / Flask) .env Generator

Modern Python services (FastAPI, Flask, litestar) all consume .env via python-dotenv or Pydantic's BaseSettings. This generator produces a .env with the idiomatic keys — SECRET_KEY, DATABASE_URL, REDIS_URL, ALLOWED_ORIGINS.

.env for Python (FastAPI / Flask) · 7 keys
Customize →
ENVIRONMENT=development
LOG_LEVEL=INFO
DATABASE_URL=postgresql://user:pass@localhost:5432/app
REDIS_URL=redis://localhost:6379/0
SECRET_KEY=zAxhk2F1QDDFYya347bRRnKIAG3TGFNv6VOnYyXW3mQlzgQM
ALLOWED_ORIGINS=http://localhost:3000
SENTRY_DSN=

Secrets regenerate on every page load. Want to combine stacks (e.g., Next.js + Stripe + Supabase)? Use the full generator.

How to use this .env in Python (FastAPI / Flask)

  1. 1.Save the output as .env in the project root.
  2. 2.Install python-dotenv: pip install python-dotenv (or pydantic-settings for Pydantic).
  3. 3.Load at startup: from dotenv import load_dotenv; load_dotenv() — before importing settings.
  4. 4.In FastAPI, prefer Pydantic's BaseSettings for automatic validation + type coercion.

Gotchas

  • Pydantic BaseSettings reads env at class-init time, not at runtime — changes need a restart.
  • Don't use os.getenv() inside request handlers — read once at startup, pass via dependency injection.
  • ALLOWED_ORIGINS from .env is a string; split on ',' in settings.py, not at the read site.

Common keys explained

ENVIRONMENT

development | staging | production. Controls logging verbosity.

LOG_LEVEL

DEBUG | INFO | WARNING | ERROR. Standard Python logging levels.

DATABASE_URL

Postgres URL parsed by SQLAlchemy or asyncpg.

REDIS_URL

Redis for cache + Celery broker.

SECRET_KEY

Server signing key. Generate — never type manually.

ALLOWED_ORIGINS

Comma-separated CORS origins.

Related tools

Other stacks