Skip to content

Commit 077eb70

Browse files
committed
feat: add environment variable support to example.py
- Add .env.example with sample configuration values - Update example.py to read API keys and host from environment variables - Include fallback to hardcoded values for backward compatibility - Add simple .env file loader without external dependencies - Document usage in example.py header comments This makes it easier for users to configure the example script without modifying the source code directly.
1 parent 20b8825 commit 077eb70

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# PostHog API Configuration
2+
# Copy this file to .env and update with your actual values
3+
4+
# Your project API key (found on the /setup page in PostHog)
5+
POSTHOG_PROJECT_API_KEY=phc_your_project_api_key_here
6+
7+
# Your personal API key (for local evaluation and other advanced features)
8+
POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key_here
9+
10+
# PostHog host URL (remove this line if using posthog.com)
11+
POSTHOG_HOST=http://localhost:8000

example.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
# PostHog Python library example
2+
#
3+
# Configuration:
4+
# 1. Copy .env.example to .env
5+
# 2. Update .env with your actual PostHog API keys and host
6+
# 3. Run this script - it will automatically load values from .env
7+
#
8+
# The script will fall back to default values if .env is not found.
9+
210
import argparse
11+
import os
312

413
import posthog
514

15+
16+
def load_env_file():
17+
"""Load environment variables from .env file if it exists."""
18+
env_path = os.path.join(os.path.dirname(__file__), '.env')
19+
if os.path.exists(env_path):
20+
with open(env_path, 'r') as f:
21+
for line in f:
22+
line = line.strip()
23+
if line and not line.startswith('#') and '=' in line:
24+
key, value = line.split('=', 1)
25+
os.environ.setdefault(key.strip(), value.strip())
26+
27+
28+
# Load .env file if it exists
29+
load_env_file()
30+
631
# Add argument parsing
732
parser = argparse.ArgumentParser(description="PostHog Python library example")
833
parser.add_argument(
@@ -14,13 +39,14 @@
1439

1540
posthog.debug = True
1641

17-
# You can find this key on the /setup page in PostHog
18-
posthog.project_api_key = "phc_gtWmTq3Pgl06u4sZY3TRcoQfp42yfuXHKoe8ZVSR6Kh"
19-
posthog.personal_api_key = "phx_fiRCOQkTA3o2ePSdLrFDAILLHjMu2Mv52vUi8MNruIm"
42+
# Load configuration from environment variables with fallbacks
43+
# You can find your project API key on the /setup page in PostHog
44+
posthog.project_api_key = os.getenv("POSTHOG_PROJECT_API_KEY", "phc_gtWmTq3Pgl06u4sZY3TRcoQfp42yfuXHKoe8ZVSR6Kh")
45+
posthog.personal_api_key = os.getenv("POSTHOG_PERSONAL_API_KEY", "phx_fiRCOQkTA3o2ePSdLrFDAILLHjMu2Mv52vUi8MNruIm")
2046

2147
# Where you host PostHog, with no trailing /.
2248
# You can remove this line if you're using posthog.com
23-
posthog.host = "http://localhost:8000"
49+
posthog.host = os.getenv("POSTHOG_HOST", "http://localhost:8000")
2450
posthog.poll_interval = 10
2551

2652
print(

0 commit comments

Comments
 (0)