Skip to main content

Published

PostHog

In brief: Give TestChimp a PostHog personal API key with only the feature_flag:read scope, plus your project ID. Sync reads feature flags, multivariate variants and rollout percentages for the project.

The PostHog integration reads the feature flags of one PostHog project, so feature flag coverage can list every variant and show the ones no test ran.

What you need​

  • A PostHog personal API key. Create it in PostHog under your account settings, in Personal API keys. Give it only the feature_flag:read scope, and limit it to the project TestChimp will read.
  • The project ID: the number in your PostHog project URL and project settings.

TestChimp never writes to PostHog, so the key needs no write scopes. Do not use the project API key that your app sends events with; that key cannot read the flags API.

Configure TestChimp​

An org admin opens Project settings → Integrations → Feature flags:

  1. Pick PostHog.
  2. Enter the Personal API key (it starts with phx_).
  3. Enter the Project ID.
  4. If your project is on PostHog EU Cloud, set the base URL to https://eu.posthog.com. The default is https://us.posthog.com.
  5. Choose Test connection, then save. The first sync runs right away.

The base URL must use HTTPS on a posthog.com host. Self-hosted PostHog on your own domain is not supported yet.

How the environment is mapped​

The PostHog flags API has no environments, so a PostHog connection has one environment, shown as Project. If you use separate PostHog projects for test and production, connect the project your tests run against.

  • Multivariate flags: each variant key is an arm, with its rollout percentage.
  • Boolean flags: the arms are true and false. A rollout is shown only when the flag has exactly one release condition with no property filters; then true gets that condition's percentage and false the rest.

What sync reads​

  • Feature flags in the project: key, name, tags, archived state, and the release conditions and multivariate variants.
  • Deleted flags are skipped. Flags no longer in PostHog are marked archived in TestChimp, not deleted.

Sync runs when you save the connection, once a day, and when you choose Sync now.

Capture in your app​

The connection supplies the catalog. To record which variants your tests run, add @testchimp/flags to your app:

  • Browser: call instrumentPostHog(posthog) once after posthog.init. Lookups with send_event: false are not recorded.
  • Node backend: instrumentPostHogNode(posthogClient) covers getFeatureFlag, isFeatureEnabled and evaluateFlags. On a multivariate flag, isFeatureEnabled answers true without naming the variant, so it counts no arm: use getFeatureFlag or evaluateFlags there. Add the collection middleware and set TESTCHIMP_FLAG_ECHO=1 in test environments.

Tests that mock PostHog's /decide or /flags endpoints with route.fulfill are recorded as forced. See Setup.

Troubleshooting​

  • 401 or 403: check that the key has the feature_flag:read scope and access to this project.
  • 404 on test: check the project ID and the base URL (US or EU Cloud).
  • No rollout on a boolean flag: the flag has more than one release condition, or property filters. Coverage still lists both arms.

Rotation​

Create a new personal API key with the same scope, edit the connection, paste it in, test and save. Leaving the key field blank keeps the stored one. Then delete the old key in PostHog.

FAQ

Which PostHog key should I use?

A personal API key with only the feature_flag:read scope, limited to the project TestChimp reads. Not the project API key your app sends events with.

Is PostHog EU Cloud supported?

Yes. Set the base URL to https://eu.posthog.com. Self-hosted PostHog on your own domain is not supported yet.