# Environment Variables for AI Image Generation This document lists all environment variables needed for the AI image generation system. ## Required Variables Add these to your `.env.local` file: ```bash # ============================================================================= # AI IMAGE GENERATION CONFIGURATION # ============================================================================= # n8n Webhook Configuration # The base URL where your n8n instance is running N8N_WEBHOOK_URL=http://localhost:5678/webhook # Secret token for authenticating webhook requests # Generate a secure random token: openssl rand -hex 32 N8N_SECRET_TOKEN=your-secure-random-token-here # Stable Diffusion API Configuration # The URL where your Stable Diffusion WebUI is running SD_API_URL=http://localhost:7860 # Optional: API key if your SD instance requires authentication # SD_API_KEY=your-sd-api-key-here # ============================================================================= # IMAGE GENERATION SETTINGS # ============================================================================= # Automatically generate images when new projects are created # Set to 'true' to enable, 'false' to disable AUTO_GENERATE_IMAGES=true # Directory where generated images will be saved # Should be inside your public directory for web access GENERATED_IMAGES_DIR=/app/public/generated-images # Maximum time to wait for image generation (in milliseconds) # Default: 180000 (3 minutes) IMAGE_GENERATION_TIMEOUT=180000 # ============================================================================= # STABLE DIFFUSION SETTINGS (Optional - Overrides n8n workflow defaults) # ============================================================================= # Default image dimensions SD_DEFAULT_WIDTH=1024 SD_DEFAULT_HEIGHT=768 # Generation quality settings SD_DEFAULT_STEPS=30 SD_DEFAULT_CFG_SCALE=7 # Sampler algorithm # Options: "Euler a", "DPM++ 2M Karras", "DDIM", etc. SD_DEFAULT_SAMPLER=DPM++ 2M Karras # Default model checkpoint # SD_DEFAULT_MODEL=sd_xl_base_1.0.safetensors # ============================================================================= # FEATURE FLAGS (Optional) # ============================================================================= # Enable/disable specific features ENABLE_IMAGE_REGENERATION=true ENABLE_BATCH_GENERATION=false ENABLE_IMAGE_OPTIMIZATION=true # ============================================================================= # LOGGING & MONITORING (Optional) # ============================================================================= # Log all image generation requests LOG_IMAGE_GENERATION=true # Send notifications on generation success/failure # DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/... # SLACK_WEBHOOK_URL=https://hooks.slack.com/services/... # ============================================================================= # ADVANCED SETTINGS (Optional) # ============================================================================= # Custom prompt prefix for all generations # SD_CUSTOM_PROMPT_PREFIX=professional tech illustration, modern design, # Custom negative prompt suffix for all generations # SD_CUSTOM_NEGATIVE_SUFFIX=low quality, blurry, pixelated, text, watermark # Image file naming pattern # Available variables: {projectId}, {timestamp}, {title} IMAGE_FILENAME_PATTERN=project-{projectId}-{timestamp}.png # Maximum concurrent image generation requests MAX_CONCURRENT_GENERATIONS=2 # Retry failed generations AUTO_RETRY_ON_FAILURE=true MAX_RETRY_ATTEMPTS=3 ``` ## Production Environment For production deployments, adjust these settings: ```bash # Production n8n (if using cloud/dedicated instance) N8N_WEBHOOK_URL=https://n8n.yourdomain.com/webhook # Production Stable Diffusion (if using dedicated GPU server) SD_API_URL=https://sd-api.yourdomain.com # Production image storage (use absolute path) GENERATED_IMAGES_DIR=/var/www/portfolio/public/generated-images # Disable auto-generation in production (manual only) AUTO_GENERATE_IMAGES=false # Enable logging LOG_IMAGE_GENERATION=true # Set timeouts appropriately IMAGE_GENERATION_TIMEOUT=300000 # Limit concurrent generations MAX_CONCURRENT_GENERATIONS=1 ``` ## Docker Environment If running in Docker, use these paths: ```bash # Docker-specific paths N8N_WEBHOOK_URL=http://n8n:5678/webhook SD_API_URL=http://stable-diffusion:7860 GENERATED_IMAGES_DIR=/app/public/generated-images ``` Add to `docker-compose.yml`: ```yaml services: portfolio: environment: - N8N_WEBHOOK_URL=http://n8n:5678/webhook - N8N_SECRET_TOKEN=${N8N_SECRET_TOKEN} - SD_API_URL=http://stable-diffusion:7860 - AUTO_GENERATE_IMAGES=true - GENERATED_IMAGES_DIR=/app/public/generated-images volumes: - ./public/generated-images:/app/public/generated-images n8n: image: n8nio/n8n ports: - "5678:5678" environment: - N8N_BASIC_AUTH_ACTIVE=true - N8N_BASIC_AUTH_USER=admin - N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD} stable-diffusion: image: your-sd-webui-image ports: - "7860:7860" deploy: resources: reservations: devices: - driver: nvidia count: 1 capabilities: [gpu] ``` ## Cloud GPU Configuration If using cloud GPU services (RunPod, vast.ai, etc.): ```bash # Remote GPU URL with authentication SD_API_URL=https://your-runpod-instance.com:7860 SD_API_KEY=your-api-key-here # Longer timeout for network latency IMAGE_GENERATION_TIMEOUT=300000 ``` ## Security Best Practices 1. **Never commit `.env.local` to version control** ```bash # Add to .gitignore echo ".env.local" >> .gitignore ``` 2. **Generate secure tokens** ```bash # Generate N8N_SECRET_TOKEN openssl rand -hex 32 # Or using Node.js node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" ``` 3. **Restrict API access** - Use firewall rules to limit SD API access - Enable authentication on n8n webhooks - Use HTTPS in production 4. **Environment-specific files** - `.env.local` - local development - `.env.production` - production (server-side only) - `.env.test` - testing environment ## Verifying Configuration Test your environment variables: ```bash # Check if variables are loaded npm run dev # In another terminal node -e " const envFile = require('fs').readFileSync('.env.local', 'utf8'); console.log('✓ .env.local exists'); console.log('✓ Variables found:', envFile.split('\\n').filter(l => l && !l.startsWith('#')).length); " # Test n8n connection curl -f $N8N_WEBHOOK_URL/health || echo "n8n not reachable" # Test SD API connection curl -f $SD_API_URL/sdapi/v1/sd-models || echo "SD API not reachable" ``` ## Troubleshooting ### Variables not loading ```bash # Ensure .env.local is in the project root ls -la .env.local # Restart Next.js dev server npm run dev ``` ### Wrong paths in Docker ```bash # Check volume mounts docker-compose exec portfolio ls -la /app/public/generated-images # Fix permissions docker-compose exec portfolio chmod 755 /app/public/generated-images ``` ### n8n webhook unreachable ```bash # Check n8n is running docker ps | grep n8n # Check network connectivity docker-compose exec portfolio ping n8n # Verify webhook URL in n8n UI ``` ## Example Complete Configuration ```bash # .env.local - Complete working example # Database (required for project data) DATABASE_URL=postgresql://user:password@localhost:5432/portfolio # NextAuth (if using authentication) NEXTAUTH_URL=http://localhost:3000 NEXTAUTH_SECRET=your-nextauth-secret # AI Image Generation N8N_WEBHOOK_URL=http://localhost:5678/webhook N8N_SECRET_TOKEN=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6 SD_API_URL=http://localhost:7860 AUTO_GENERATE_IMAGES=true GENERATED_IMAGES_DIR=/Users/dennis/code/gitea/portfolio/public/generated-images # Image settings SD_DEFAULT_WIDTH=1024 SD_DEFAULT_HEIGHT=768 SD_DEFAULT_STEPS=30 SD_DEFAULT_CFG_SCALE=7 SD_DEFAULT_SAMPLER=DPM++ 2M Karras # Optional features ENABLE_IMAGE_REGENERATION=true LOG_IMAGE_GENERATION=true IMAGE_GENERATION_TIMEOUT=180000 MAX_CONCURRENT_GENERATIONS=2 ``` --- **Note**: Always keep your `.env.local` file secure and never share tokens publicly!