New to Claude Skills? Learn how to install them →

posthog on GitHub

Project Secret API Key Auth

Free

Securely authenticate PostHog API endpoints with PSAK.

by posthog37.6k stars on posthog/posthog
1 views
Updated Aug 11, 2026
Get this skill

Free · Opens the source repo

What Project Secret API Key Auth does

The Project Secret API Key (PSAK) is a user-less service credential designed for PostHog API authentication. It allows developers to secure API endpoints using a project-scoped key that persists even when users leave the project. This key, prefixed with 'phs_', is used in the Bearer authorization header and supports specific scopes that are project-wide, simplifying access management while ignoring object-level controls. This makes it particularly useful for programmatic access to endpoints without tying authentication to individual user accounts.

To implement PSAK authentication, developers must follow a checklist that includes whitelisting the scope/action pairs, adding the appropriate authenticator, and setting up PSAK-aware rate throttles. The integration process involves modifying your viewset to recognize the PSAK and defining which actions can be accessed using this key. By doing so, you ensure that only designated actions are allowed, enhancing security and control over API access.

Additionally, PSAK authentication provides unique features like query tagging for analytics and activity logging for key management actions, which are automatically handled by the system. This reduces the overhead for developers, allowing them to focus on building features rather than managing authentication intricacies. The skill is particularly beneficial for teams that require robust API access management without the complexity of user-level permissions.

This skill is ideal for developers working with PostHog who need to implement secure API access for automated processes or integrations. It streamlines the authentication process and enhances security by leveraging project-specific credentials, making it a valuable addition to any PostHog deployment.

When to use it

Use this skill when you need to authenticate API requests programmatically without tying access to individual user accounts.

When not to use it

This skill is not suitable for scenarios requiring user-specific permissions or where individual user tracking is essential.

What you can build with it

Automating Data Ingestion

Use PSAK to securely authenticate automated scripts that ingest data into PostHog without requiring user credentials.

Integrating Third-Party Services

Leverage PSAK to allow third-party services to interact with PostHog APIs securely, ensuring that access is limited to defined scopes.

Managing API Access for CI/CD Pipelines

Implement PSAK in your CI/CD pipelines to authenticate API calls during deployment processes without exposing user credentials.

How to install Project Secret API Key Auth

View source

1. Install with the skills CLI

npx skills add posthog/posthog/adding-project-secret-api-key-auth --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by posthog

Adding project secret API key (PSAK) auth to an endpoint

What a PSAK is

A ProjectSecretAPIKey is a project-scoped, user-less service credential (posthog/models/project_secret_api_key.py). It behaves like a personal API key but survives users leaving the project, carries its own scopes, and authenticates as a synthetic user — not a real User row.

  • Token format: phs_... (Bearer header only — no body fallback, unlike the legacy token).
  • Scopes are project-wide within their resource type and deliberately ignore object-level access controls (per-resource RBAC).
  • Do not confuse with TeamSecretTokenAuthentication — that validates the legacy per-team Team.secret_api_token (also phs_-prefixed) and is only for feature-flag local evaluation and similar pre-PSAK surfaces. It is pegged for migrating to PSAK at some point.

Keys are managed at POST /api/environments/:id/project_secret_api_keys (label + scopes; plaintext value returned once; roll action to rotate; max 50 per project; wildcard * scope not allowed).

Wiring a viewset action — the checklist

The machinery is shipped but nothing is wired to it yet — the first planned consumer is the endpoints (the product) run action. Four things, all required:

1. Whitelist the scope/action pair

PSAK-assignable scopes are a global allowlist in posthog/scopes.py:

PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION: list[tuple[APIScopeObject, APIScopeActions]] = [("endpoint", "read")]

If your product isn't listed, key creation rejects the scope before auth is ever attempted. Add your (scope_object, action) tuple here first.

2. Add the authenticator and opt in actions

class MyViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
    scope_object = "endpoint"
    authentication_classes = [ProjectSecretAPIKeyAuthentication]  # extends, TeamAndOrgViewSetMixin keeps session/PAK auth
    psak_allowed_actions = ["run"]

psak_allowed_actions is default-deny: APIScopePermission rejects any PSAK request whose action isn't listed ("This action does not support project secret API key access"). List only the programmatic actions — never CRUD that should stay human-driven.

APIScopePermission also enforces team binding automatically: a PSAK only works against view.team == key.team, so PSAK auth only makes sense on project-scoped (/api/environments/:id/...) routes.

3. Use PSAK-aware throttles

PersonalApiKeyRateThrottle subclasses silently bypass PSAK requests (no personal key → no throttling). Use the PSAK-aware pair from posthog/rate_limit.py:

  • PersonalOrProjectSecretApiKeyRateThrottle — per-key budget (keyed psak:{key_id}), also still throttles personal keys.
  • ProjectSecretApiKeyTeamRateThrottle — per-team aggregate (keyed psak-team:{team_id}), caps total PSAK load regardless of how many keys a project mints. Stack it alongside the per-key throttle.

Subclass them to set product-specific scope/rate; remember each throttle keeps its own cache bucket per scope string.

4. Handle the synthetic user

request.user is a ProjectSecretAPIKeyUser (a SyntheticUser, posthog/synthetic_user.py), not a User:

  • user.id is None — never use it as a foreign key. Use user.current_team_id.

  • has_perm() always returns False — Django permission checks silently deny.

  • Skip per-object access-control checks for it (PSAK scopes are project-wide by design):

    if is_authenticated_via_project_secret_api_key(request):
        return  # PSAK bypasses object-level RBAC deliberately
    

    Use isinstance(user, ProjectSecretAPIKeyUser) only where no request is in scope.

  • report_user_action drops synthetic users — if you need analytics for PSAK-authenticated calls, capture explicitly with posthoganalytics.capture(distinct_id=user.distinct_id, ...) and include an auth_method property so both paths emit the same event shape.

  • HogQL system tables: Database.create_for hides RBAC-scoped system tables the key's scopes don't cover (via readable_system_table_access_scopes()).

Helpers in posthog/permissions.py when you need to branch: is_authenticated_via_project_secret_api_key(request) and is_service_auth(request) (covers PSAK + legacy team token).

What you get for free

  • Query tagging: the authenticator calls tag_authentication(access_method=AccessMethod.PROJECT_SECRET_API_KEY, api_key_mask=..., api_key_label=...), so ClickHouse query_log attribution works with no per-endpoint code. If you add a new authenticator, tag through tag_authentication (the single funnel in posthog/clickhouse/query_tagging.py) — not with ad-hoc tag_queries calls.
  • last_used_at tracking: updated at most hourly via .update() (bypasses ModelActivityMixin so routine auth doesn't spam the activity log).
  • Activity logging on key create/update/roll/delete.

Calling a PSAK-gated endpoint

curl -s https://us.posthog.com/api/environments/<project_id>/<your_action_path>/ \
  -H "Authorization: Bearer phs_<key>" \
  -H "Content-Type: application/json" \
  -d '{...}'

Testing

Mirror the PSAK sections of posthog/api/test/test_authentication.py, posthog/test/test_permissions.py, and posthog/test/test_rate_limit.py. Cover at minimum:

  • allowed action with correct scope → 200
  • action not in psak_allowed_actions → 403
  • missing/wrong scope → 403
  • key from another team's project → 403
  • non-PSAK auth on the same action still works

Frequently asked questions about Project Secret API Key Auth

Similar skills