Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat: seed deterministic project token for demo data (#61016)

Joshua Snyder28c87fb801e6
diff --git a/posthog/demo/matrix/manager.py b/posthog/demo/matrix/manager.py
index b450e0914081..1e2f6db1e7a5 100644
--- a/posthog/demo/matrix/manager.py
+++ b/posthog/demo/matrix/manager.py
@@ -22,7 +22,7 @@ from posthog.models import (
Team,
User,
)
-from posthog.models.utils import UUIDT
+from posthog.models.utils import UUIDT, generate_random_token_project
from .matrix import Matrix
from .models import SimEvent, SimPerson
@@ -55,6 +55,7 @@ class MatrixManager:
password: str | None = None,
is_staff: bool = False,
email_collision_handling: Literal["log_in", "disambiguate"] = "log_in",
+ api_token: str | None = None,
) -> tuple[Organization, Team, User]:
"""If there's an email collision in signup in the demo environment, we treat it as a login."""
existing_user: User | None = User.objects.filter(email=email).first()
@@ -90,7 +91,11 @@ class MatrixManager:
theme_mode="system",
role_at_organization="engineering",
)
- team = self.create_team(organization)
+ if api_token:
+ # api_token is unique, so release it from any team that claimed it on a previous run
+ # before the new team claims it. Kept in the same transaction so a failure can't orphan it.
+ Team.objects.filter(api_token=api_token).update(api_token=generate_random_token_project())
+ team = self.create_team(organization, **({"api_token": api_token} if api_token else {}))
self.run_on_team(team, new_user)
return (organization, team, new_user)
elif existing_user.is_staff:
diff --git a/posthog/management/commands/generate_demo_data.py b/posthog/management/commands/generate_demo_data.py
index e3461ed4c738..1ad509e34983 100644
--- a/posthog/management/commands/generate_demo_data.py
+++ b/posthog/management/commands/generate_demo_data.py
@@ -8,6 +8,7 @@ from time import monotonic
from typing import Optional
from urllib.parse import quote
+from django.conf import settings
from django.core import exceptions
from django.core.management.base import BaseCommand
@@ -29,6 +30,10 @@ from ee.clickhouse.materialized_columns.analyze import materialize_properties_ta
logging.getLogger("kafka").setLevel(logging.ERROR) # Hide kafka-python's logspam
+# Deterministic project token for the locally seeded demo team, so SDKs/tools can target it without
+# looking up the random token. Only used for the freshly created demo account (not existing projects).
+DEMO_PROJECT_API_TOKEN = "phc_localposthogprojecttoken"
+
class Command(BaseCommand):
help = "Generate demo data using the Matrix"
@@ -170,6 +175,10 @@ class Command(BaseCommand):
raise ValueError(f"Project {existing_team_id} has no organization members")
matrix_manager.run_on_team(team, user)
else:
+ # Hand the demo team a deterministic token in local dev only. In tests (e.g. Playwright
+ # setup, which seeds many workspaces) and shared/cloud environments, keep random tokens
+ # so runs don't fight over the unique token or mutate unrelated projects.
+ demo_api_token = DEMO_PROJECT_API_TOKEN if (settings.DEBUG and not settings.TEST) else None
_organization, team, user = matrix_manager.ensure_account_and_save(
email,
"Employee 427",
@@ -177,6 +186,7 @@ class Command(BaseCommand):
is_staff=bool(options.get("staff")),
password=password,
email_collision_handling="disambiguate",
+ api_token=demo_api_token,
)
# Optionally generate demo issues for issue tracker if extension is available
@@ -233,6 +243,7 @@ class Command(BaseCommand):
"Pre-fill the login form with this link:\n"
f"http://localhost:8010/login?email={quote(user.email if user is not None else '')}\n"
f"The password is:\n{password}\n\n"
+ f"The project API token is:\n{team.api_token if team is not None else 'unknown'}\n\n"
"If running demo mode (DEMO=1), log in instantly with this link:\n"
f"http://localhost:8010/signup?email={quote(user.email if user is not None else '')}\n"
)