Analytics

PostHog API keys: find, store, rotate, and use them safely with AI agents

PostHog has a public project token for capture and flag endpoints, personal API keys (phx_) for private endpoints, and project secret API keys (phs_, beta) for server-to-server access. Personal API keys are created under Personal API keys in account settings with the scopes you choose, and PostHog automatically rolls any phx_ or phs_ key that GitHub secret scanning finds in a public repository.

PostHog key types

TypePrefixSecret?ScopeExpiry
Project token (project API key)
Found in project settings. Safe to ship in client code; it only identifies the project.
—NoPublic POST endpoints such as event capture and feature flag evaluation—
Personal API key
Sent as Authorization: Bearer or as personal_api_key in the body. Up to 10 per user.
phx_YesThe scopes chosen at creation, limited to what your user can access; can be scoped to an organization or project—
Project secret API key (beta)
For server-to-server use. Rolling out gradually, so it may not be available in every organization yet.
phs_YesOnly the scopes granted, for one project; not tied to a user—
OAuth access token
Paired with an OAuth refresh token (phr_).
pha_YesThe scopes the user granted to the OAuth app—

How to create a PostHog key

  1. Open the Personal API keys section of your account settings (us.posthog.com/settings/user-api-keys, or the EU equivalent).
  2. Click + Create a personal API Key and enter a label describing its purpose.
  3. Choose only the scopes the integration needs, such as insight:read.
  4. Copy the key immediately; PostHog never shows it again after you refresh the page.

Open PostHog's key settings

Limiting what the key can do

Each PostHog scope has a read and a write level, such as feature_flag:read or insight:write; request :read when the key only views data. Personal API keys can also be limited to one organization or project, and org admins can review each key's access under Settings → Organization → Security → Personal API key access. A call that needs a missing scope fails with a 403 naming that scope.

How to rotate a PostHog key

  1. Click the personal API key in the keys table to open its edit modal.
  2. Click Roll key and confirm in the dialog.
  3. Copy the new value and update every script or service that used the old key.

Rolling a PostHog key generates a new value and invalidates the old one immediately, so there is no overlap window. PostHog's docs describe Roll key for upgrading legacy keys created before February 2024.

If your PostHog key leaked

  1. Roll the exposed personal API key so the old value stops working, as PostHog recommends when exposure is suspected.
  2. Update every script, service and MCP client that used the key.
  3. Ask an org admin to review Personal API key access for keys that are broader than needed.
  4. PostHog is a GitHub secret scanning partner. A phx_ or phs_ key found in a public repository is rolled automatically and the owner or project admins are emailed; leaked OAuth tokens are revoked.

Things that catch people out

  • The project token is public by design; personal and project secret keys are not.
  • Most rate limits apply per personal API key, not per team.
  • Personal API keys are deleted when their user is deleted, which breaks automations tied to that person; project secret keys are not tied to a user.
  • For legacy feature flag secure keys, rotating leaves a backup key active until you delete it.

Common PostHog authentication errors

Project token invalid. You can find your project token in your PostHog project settings.

HTTP 401 on a public endpoint; the project token is wrong or belongs to another project.

Invalid Personal API key.

HTTP 401; the personal API key is wrong, was rolled, or its user was deleted.

Using your PostHog key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a PostHog key, these intents fit:

IntentWhat BotPassword does
run_withRuns a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
http_requestMakes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
rotatePartly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
store_secretSaves a value the agent already holds as a new vault item.

PostHog also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

What is the difference between a PostHog project token and a personal API key?

The project token identifies your project for public capture and flag endpoints and can be public. A personal API key (phx_) authenticates private endpoints with your user's access and must stay secret.

Where do I create a PostHog personal API key?

In account settings under Personal API keys, using + Create a personal API Key. Choose scopes and copy the key straight away.

Is the PostHog project token secret?

No. The project token is meant for client code and only allows public endpoints such as event capture.

How do I roll a PostHog personal API key?

Open the key in the keys table, click Roll key and confirm. The old value stops working, so update every client.

What happens if a PostHog key is pushed to GitHub?

GitHub secret scanning reports it to PostHog, which rolls phx_ and phs_ keys automatically and emails the owner.

Can an AI agent use my PostHog key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with run_with, http_request, store_secret, so the agent gets the result and never the value.

Stop pasting PostHog keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.