Skip to main content

synth_ai.core.env

Environment resolution utilities. This module provides non-interactive environment variable resolution for use by SDK and CLI. It consolidates the various env resolution patterns into a clean API.

Functions

get_api_key

get_api_key(env_key: str = 'SYNTH_API_KEY', required: bool = True) -> str | None
Get API key from environment. Args:
  • env_key: Environment variable name to check
  • required: If True, raises AuthenticationError when not found
Returns:
  • API key string or None if not required and not found
Raises:
  • AuthenticationError: If required and not found

get_backend_url

get_backend_url(mode: Literal['prod', 'dev', 'local'] | None = None) -> str
Resolve backend URL. Priority order:
  1. SYNTH_BACKEND_URL env var (if set)
  2. Mode-specific URL based on SYNTH_BACKEND_MODE or explicit mode
  3. Default to production
Args:
  • mode: Force a specific mode (prod/dev/local), or detect from env
Returns:
  • Backend URL (without trailing /api)

resolve_env_file

resolve_env_file(explicit_path: str | Path | None = None, search_cwd: bool = True) -> Path | None
Find and return path to .env file. Args:
  • explicit_path: If provided, use this path directly
  • search_cwd: If True, search current directory for .env
Returns:
  • Path to .env file, or None if not found

load_env_file

load_env_file(path: Path) -> dict[str, str]
Parse a .env file into a dictionary. Args:
  • path: Path to .env file
Returns:
  • Dict mapping env var names to values

mask_value

mask_value(value: str, visible_chars: int = 4) -> str
Mask a sensitive value for display. Args:
  • value: The value to mask
  • visible_chars: Number of characters to show at start and end
Returns:
  • Masked string like “abc…xyz”

get_backend_from_env

get_backend_from_env() -> tuple[str, str]
Resolve (base_url, api_key) using LOCAL/DEV/PROD override scheme. Env vars consulted:
  • BACKEND_OVERRIDE = full URL (with or without /api)
  • SYNTH_BACKEND_URL_OVERRIDE = local|dev|prod (case-insensitive)
  • LOCAL_BACKEND_URL, TESTING_LOCAL_SYNTH_API_KEY
  • DEV_BACKEND_URL, DEV_SYNTH_API_KEY
  • PROD_BACKEND_URL, TESTING_PROD_SYNTH_API_KEY (fallback to SYNTH_API_KEY)
Base URL is normalized (no trailing /api). Defaults: prod base URL → https://api.usesynth.ai Returns:
  • Tuple of (base_url, api_key)