Configure PostHog across development, staging, and production environments. Use when setting up multi-environment deployments, configuring per-environment secrets, or implementing environment-specific PostHog configurations. Trigger with phrases like "posthog environments", "posthog staging", "posthog dev prod", "posthog environment setup", "posthog config by env".
Configure PostHog across development, staging, and production environments.
| Environment | Purpose | API Keys | Data |
|---|---|---|---|
| Development | Local dev | Test keys | Sandbox |
| Staging | Pre-prod validation | Staging keys | Test data |
| Production | Live traffic | Production keys | Real data |
config/
├── posthog/
│ ├── base.json # Shared config
│ ├── development.json # Dev overrides
│ ├── staging.json # Staging overrides
│ └── production.json # Prod overrides
{
"timeout": 30000,
"retries": 3,
"cache": {
"enabled": true,
"ttlSeconds": 60
}
}
{
"apiKey": "${POSTHOG_API_KEY}",
"baseUrl": "https://api-sandbox.posthog.com",
"debug": true,
"cache": {
"enabled": false
}
}
{
"apiKey": "${POSTHOG_API_KEY_STAGING}",
"baseUrl": "https://api-staging.posthog.com",
"debug": false
}
{
"apiKey": "${POSTHOG_API_KEY_PROD}",
"baseUrl": "https://api.posthog.com",
"debug": false,
"retries": 5
}
// src/posthog/config.ts
import baseConfig from '../../config/posthog/base.json';
type Environment = 'development' | 'staging' | 'production';
function detectEnvironment(): Environment {
const env = process.env.NODE_ENV || 'development';
const validEnvs: Environment[] = ['development', 'staging', 'production'];
return validEnvs.includes(env as Environment)
? (env as Environment)
: 'development';
}
export function getPostHogConfig() {
const env = detectEnvironment();
const envConfig = require(`../../config/posthog/${env}.json`);
return {
...baseConfig,
...envConfig,
environment: env,
};
}
# .env.local (git-ignored)
POSTHOG_API_KEY=sk_test_dev_***