adding-project-secret-api-key-auth

Gates PostHog API viewset actions with project secret API key authentication.

713|118|Updated Aug 11, 2020
One-click install
npx skills add https://github.com/PostHog/posthog-foss --skill adding-project-secret-api-key-auth
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: adding-project-secret-api-key-auth
Source: https://github.com/PostHog/posthog-foss/tree/main/.agents/skills/adding-project-secret-api-key-auth
Command: npx skills add https://github.com/PostHog/posthog-foss --skill adding-project-secret-api-key-auth

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Adding programmatic, service-style authentication to a PostHog API endpoint requires coordinating several pieces: scope allowlists, authenticator classes, action opt-ins, rate throttles, and synthetic user handling. This Skill walks through the complete checklist for wiring project secret API key (PSAK) auth to a viewset action without missing any required step.

Core Features & Use Cases

  • Scope and action allowlisting: Add the (scope_object, action) tuple to PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION and opt in actions via psak_allowed_actions, which is default-deny.
  • PSAK-aware throttling: Replace PersonalApiKeyRateThrottle (which silently bypasses PSAK requests) with PersonalOrProjectSecretApiKeyRateThrottle and ProjectSecretApiKeyTeamRateThrottle.
  • Synthetic user handling: Correctly work with ProjectSecretAPIKeyUser, which has no id, always fails has_perm(), and is dropped by report_user_action.
  • Use Case: You are adding a programmatic run action to the endpoints product and need it callable via a phs_ Bearer token with proper scoping, throttling, team binding, and tests.

Quick Start

Ask the agent to add project secret API key authentication to a specific PostHog viewset action, including the scope allowlist entry, throttles, and tests.

Frequently Asked Questions about adding-project-secret-api-key-auth

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add project secret API key auth to a PostHog endpoint?

Add your (scope_object, action) tuple to PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION in posthog/scopes.py, set authentication_classes to include ProjectSecretAPIKeyAuthentication, and list the action in psak_allowed_actions on the viewset. Also switch to PSAK-aware rate throttles and handle the synthetic user.

What is a project secret API key in PostHog?

A ProjectSecretAPIKey is a project-scoped, user-less service credential with a phs_ token prefix used in the Authorization Bearer header. It carries its own scopes, survives users leaving the project, and authenticates as a synthetic ProjectSecretAPIKeyUser rather than a real User row.

Why is my PSAK request not being rate limited?

PersonalApiKeyRateThrottle subclasses silently bypass PSAK requests because there is no personal key to throttle. Use PersonalOrProjectSecretApiKeyRateThrottle for per-key budgets and ProjectSecretApiKeyTeamRateThrottle for per-team aggregate caps instead.

Why does my PSAK request get a 403 on a viewset action?

psak_allowed_actions is default-deny, so APIScopePermission rejects any PSAK request whose action is not listed. Also verify the key's scopes cover the action, the scope is in the global allowlist, and the key belongs to the same team as the route's project.

Can I use request.user.id with project secret API key auth?

No. PSAK requests authenticate as ProjectSecretAPIKeyUser, a synthetic user whose id is None, so it must never be used as a foreign key. Use user.current_team_id instead, and note that has_perm() always returns False for synthetic users.

What is the difference between PSAK and TeamSecretTokenAuthentication?

TeamSecretTokenAuthentication validates the legacy per-team Team.secret_api_token and is only for feature-flag local evaluation and similar pre-PSAK surfaces. ProjectSecretAPIKeyAuthentication is the modern scoped service credential, and the legacy token is pegged for migration to PSAK.