| name | posthog-security-basics |
| description | Secure PostHog integration: API key management, project key vs personal key
separation, secret rotation, scoped keys, and git-leak prevention.
Trigger: "posthog security", "posthog secrets", "secure posthog",
"posthog API key security", "posthog key rotation".
|
| allowed-tools | Read, Write, Grep |
| version | 1.12.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","posthog","api","security","audit"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
PostHog Security Basics
Overview
Secure PostHog API key management, least-privilege access, and secret rotation. PostHog has two key types with very different security profiles: the Project API Key (phc_...) is intentionally public and safe to include in frontend bundles, while the Personal API Key (phx_...) grants admin access and must never be exposed.
Prerequisites
- PostHog account with admin access
- Understanding of environment variable management
.gitignore configured
Instructions
Step 1: Understand Key Security Profiles
| Key Type | Prefix | Exposure Risk | Capabilities |
|---|
| Project API Key | phc_ | Low (designed to be public) | Capture events, evaluate flags, identify users |
| Personal API Key | phx_ | Critical (full admin access) | CRUD flags, read persons, query insights, delete data |
NEXT_PUBLIC_POSTHOG_KEY=phc_abc123
POSTHOG_PERSONAL_API_KEY=phx_xyz789
POSTHOG_PROJECT_ID=12345
.env
.env.local
.env.*.local
Step 2: Create Scoped Personal API Keys
set -euo pipefail
curl -X POST "https://app.posthog.com/api/personal_api_keys/" \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"label": "bi-dashboard-readonly",
"scopes": ["insight:read", "dashboard:read", "query:read"]
}'
curl -X POST "https://app.posthog.com/api/personal_api_keys/" \
-H \
-H \
-d