payload-posthog

Feature Flags

Server-side flag evaluation helpers and an admin dashboard widget.

Import the exported helpers directly wherever you need a flag check — access control, hooks, or a custom endpoint:

import { getFeatureFlags, isFeatureEnabled } from 'payload-posthog'

export const showBetaField: FieldHook = async ({ req }) => {
  return isFeatureEnabled('beta-fields', { req })
}

// Or evaluate everything at once (a single /flags request):
const flags = await getFeatureFlags({ req })
if (flags?.isEnabled('new-dashboard')) {
  /* ... */
}

getFeatureFlags wraps posthog-node's evaluateFlags() — the current recommendation over the deprecated single-flag lookups, since it consolidates evaluation into one request per incoming request.

Admin dashboard widget

payloadPosthog({
  apiKey: process.env.POSTHOG_API_KEY!,
  personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY, // enables local evaluation
  featureFlags: {
    adminWidget: true, // default
    endpointPath: '/posthog/flags', // default
  },
})

The widget lists every flag active for the logged-in admin user. It's server-rendered directly from posthog-node, no client-side fetch involved. Set personalApiKey to enable local flag evaluation for it; without one, it falls back to remote evaluation.

On this page