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

Source: https://botpassword.com/keys/posthog
Last checked: 2026-11-08

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Project token (project API key) | — | No | Public POST endpoints such as event capture and feature flag evaluation | — |
| Personal API key | `phx_` | Yes | The scopes chosen at creation, limited to what your user can access; can be scoped to an organization or project | — |
| Project secret API key (beta) | `phs_` | Yes | Only the scopes granted, for one project; not tied to a user | — |
| OAuth access token | `pha_` | Yes | The 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.

## 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.

## If the 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.

## Gotchas

- 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.

## Using this key with AI agents (BotPassword)

- `run_with`: Runs 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_request`: Makes 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.
- `rotate`: Partly 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_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### 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.

## Sources

- https://posthog.com/docs/api
- https://posthog.com/docs/api/overview
- https://posthog.com/docs/api/personal-api-keys
- https://posthog.com/docs/api/scopes
- https://posthog.com/docs/model-context-protocol
- https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns
