Skip to content

Commit dc5bcad

Browse files
committed
Make examples run without requiring personal api key
1 parent eba2bbd commit dc5bcad

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

example.py

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,54 +35,40 @@ def load_env_file():
3535
personal_api_key = os.getenv("POSTHOG_PERSONAL_API_KEY", "")
3636
host = os.getenv("POSTHOG_HOST", "http://localhost:8000")
3737

38-
# Check if credentials are provided
39-
if not project_key or not personal_api_key:
40-
print("❌ Missing PostHog credentials!")
41-
print(
42-
" Please set POSTHOG_PROJECT_API_KEY and POSTHOG_PERSONAL_API_KEY environment variables"
43-
)
38+
# Check if project key is provided (required)
39+
if not project_key:
40+
print("❌ Missing PostHog project API key!")
41+
print(" Please set POSTHOG_PROJECT_API_KEY environment variable")
4442
print(" or copy .env.example to .env and fill in your values")
4543
exit(1)
4644

47-
# Test authentication before proceeding
48-
print("🔑 Testing PostHog authentication...")
45+
# Configure PostHog with credentials
46+
posthog.debug = False
47+
posthog.api_key = project_key
48+
posthog.project_api_key = project_key
49+
posthog.host = host
50+
posthog.poll_interval = 10
4951

50-
try:
51-
# Configure PostHog with credentials
52-
posthog.debug = False # Keep quiet during auth test
53-
posthog.api_key = project_key
54-
posthog.project_api_key = project_key
52+
# Check if personal API key is available for local evaluation
53+
local_eval_available = bool(personal_api_key)
54+
if personal_api_key:
5555
posthog.personal_api_key = personal_api_key
56-
posthog.host = host
57-
posthog.poll_interval = 10
58-
59-
# Test by attempting to get feature flags (this validates both keys)
60-
# This will fail if credentials are invalid
61-
test_flags = posthog.get_all_flags("test_user", only_evaluate_locally=True)
62-
63-
# If we get here without exception, credentials work
64-
print("✅ Authentication successful!")
65-
print(f" Project API Key: {project_key[:9]}...")
66-
print(" Personal API Key: [REDACTED]")
67-
print(f" Host: {host}\n\n")
68-
69-
except Exception as e:
70-
print("❌ Authentication failed!")
71-
print(f" Error: {e}")
72-
print("\n Please check your credentials:")
73-
print(" - POSTHOG_PROJECT_API_KEY: Project API key from PostHog settings")
74-
print(
75-
" - POSTHOG_PERSONAL_API_KEY: Personal API key (required for local evaluation)"
76-
)
77-
print(" - POSTHOG_HOST: Your PostHog instance URL")
78-
exit(1)
56+
57+
print("🔑 PostHog Configuration:")
58+
print(f" Project API Key: {project_key[:9]}...")
59+
if local_eval_available:
60+
print(" Personal API Key: [SET]")
61+
else:
62+
print(" Personal API Key: [NOT SET] - Local evaluation examples will be skipped")
63+
print(f" Host: {host}\n")
7964

8065
# Display menu and get user choice
8166
print("🚀 PostHog Python SDK Demo - Choose an example to run:\n")
8267
print("1. Identify and capture examples")
83-
print("2. Feature flag local evaluation examples")
68+
local_eval_note = "" if local_eval_available else " [requires personal API key]"
69+
print(f"2. Feature flag local evaluation examples{local_eval_note}")
8470
print("3. Feature flag payload examples")
85-
print("4. Flag dependencies examples")
71+
print(f"4. Flag dependencies examples{local_eval_note}")
8672
print("5. Context management and tagging examples")
8773
print("6. Run all examples")
8874
print("7. Exit")
@@ -148,6 +134,12 @@ def load_env_file():
148134
)
149135

150136
elif choice == "2":
137+
if not local_eval_available:
138+
print("\n❌ This example requires a personal API key for local evaluation.")
139+
print(" Set POSTHOG_PERSONAL_API_KEY environment variable to run this example.")
140+
posthog.shutdown()
141+
exit(1)
142+
151143
print("\n" + "=" * 60)
152144
print("FEATURE FLAG LOCAL EVALUATION EXAMPLES")
153145
print("=" * 60)
@@ -215,6 +207,12 @@ def load_env_file():
215207
print(f"Value (variant or enabled): {result.get_value()}")
216208

217209
elif choice == "4":
210+
if not local_eval_available:
211+
print("\n❌ This example requires a personal API key for local evaluation.")
212+
print(" Set POSTHOG_PERSONAL_API_KEY environment variable to run this example.")
213+
posthog.shutdown()
214+
exit(1)
215+
218216
print("\n" + "=" * 60)
219217
print("FLAG DEPENDENCIES EXAMPLES")
220218
print("=" * 60)
@@ -429,6 +427,8 @@ def process_payment(payment_id):
429427

430428
elif choice == "6":
431429
print("\n🔄 Running all examples...")
430+
if not local_eval_available:
431+
print(" (Skipping local evaluation examples - no personal API key set)\n")
432432

433433
# Run example 1
434434
print(f"\n{'🔸' * 20} IDENTIFY AND CAPTURE {'🔸' * 20}")
@@ -447,35 +447,37 @@ def process_payment(payment_id):
447447
distinct_id="new_distinct_id", properties={"email": "[email protected]"}
448448
)
449449

450-
# Run example 2
451-
print(f"\n{'🔸' * 20} FEATURE FLAGS {'🔸' * 20}")
452-
print("🏁 Testing basic feature flags...")
453-
print(f"beta-feature: {posthog.feature_enabled('beta-feature', 'distinct_id')}")
454-
print(
455-
f"Sydney user: {posthog.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
456-
)
450+
# Run example 2 (requires local evaluation)
451+
if local_eval_available:
452+
print(f"\n{'🔸' * 20} FEATURE FLAGS {'🔸' * 20}")
453+
print("🏁 Testing basic feature flags...")
454+
print(f"beta-feature: {posthog.feature_enabled('beta-feature', 'distinct_id')}")
455+
print(
456+
f"Sydney user: {posthog.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
457+
)
457458

458459
# Run example 3
459460
print(f"\n{'🔸' * 20} PAYLOADS {'🔸' * 20}")
460461
print("📦 Testing payloads...")
461462
print(f"Payload: {posthog.get_feature_flag_payload('beta-feature', 'distinct_id')}")
462463

463-
# Run example 4
464-
print(f"\n{'🔸' * 20} FLAG DEPENDENCIES {'🔸' * 20}")
465-
print("🔗 Testing flag dependencies...")
466-
result1 = posthog.feature_enabled(
467-
"test-flag-dependency",
468-
"demo_user",
469-
person_properties={"email": "[email protected]"},
470-
only_evaluate_locally=True,
471-
)
472-
result2 = posthog.feature_enabled(
473-
"test-flag-dependency",
474-
"demo_user2",
475-
person_properties={"email": "[email protected]"},
476-
only_evaluate_locally=True,
477-
)
478-
print(f"✅ @example.com user: {result1}, regular user: {result2}")
464+
# Run example 4 (requires local evaluation)
465+
if local_eval_available:
466+
print(f"\n{'🔸' * 20} FLAG DEPENDENCIES {'🔸' * 20}")
467+
print("🔗 Testing flag dependencies...")
468+
result1 = posthog.feature_enabled(
469+
"test-flag-dependency",
470+
"demo_user",
471+
person_properties={"email": "[email protected]"},
472+
only_evaluate_locally=True,
473+
)
474+
result2 = posthog.feature_enabled(
475+
"test-flag-dependency",
476+
"demo_user2",
477+
person_properties={"email": "[email protected]"},
478+
only_evaluate_locally=True,
479+
)
480+
print(f"✅ @example.com user: {result1}, regular user: {result2}")
479481

480482
# Run example 5
481483
print(f"\n{'🔸' * 20} CONTEXT MANAGEMENT {'🔸' * 20}")

0 commit comments

Comments
 (0)