惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
D
Docker
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 叶小钗
I
InfoQ
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
B
Blog
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium

Flags SDK Documentation

Global Config Bulk Evaluation Evaluation Context Precompute GrowthBook Hypertune Split Evaluation Context Quickstart Precompute Evaluation Context Quickstart Precompute GrowthBook Hypertune PostHog Split Proxy Dashboard Pages Marketing Pages Dashboard Pages Marketing Pages Data Locality Providers LaunchDarkly Optimizely Reflag Statsig Server-side vs Client-side Flags as Code
PostHog
Vercel · 2026-05-28 · via Flags SDK Documentation

The PostHog package provides a managed PostHog adapter for the Flags SDK.

Learn more about Adapters

import { flag } from "flags/next";
import { postHogAdapter } from '@flags-sdk/posthog'
import identify from "@/lib/identify";

export const myFlag = flag({
  key: "posthog-is-feature-enabled",
  adapter: postHogAdapter.isFeatureEnabled(),
  identify,
});

export const myFlagVariant = flag({
  key: "posthog-feature-flag-value",
  adapter: postHogAdapter.featureFlagValue(),
  identify,
});

export const myFlagPayload = flag({
  key: "posthog-feature-flag-payload",
  adapter: postHogAdapter.featureFlagPayload((v) => v),
  defaultValue: {},
  identify,
});

Install the required dependencies:

pnpm i @flags-sdk/posthog

Set up required environment variables:

# https://posthog.com/docs/libraries/next-js
# Or Settings > Project > Project API Key
NEXT_PUBLIC_POSTHOG_KEY=key
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com

Import the PostHog adapter for Flags SDK, which uses the enviroment variables above as defaults:

import { postHogAdapter } from '@flags-sdk/posthog'

If needed, you can instead initialize the adapter with your own options by importing createPostHogAdapter

import { createPostHogAdapter } from '@flags-sdk/posthog'

const postHogAdapter = createPostHogAdapter({
  postHogKey: process.env.NEXT_PUBLIC_POSTHOG_KEY,
  postHogOptions: {
    host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
    // ...
  },
})

You can define flags using the PostHog adapter's methods, which are:

  • isFeatureEnabled: Returns a boolean indicating whether a feature flag is enabled
  • featureFlagValue: Returns a boolean, or a string for multivariate flags
  • featureFlagPayload: Maps a PostHog flag's payload to a structured flag value
import { flag } from 'flags/next'
import { postHogAdapter } from '@flags-sdk/posthog'
import { identify } from '@/lib/identify'

export const exampleFlag = flag({
  key: 'example-flag',
  defaultValue: false,
  adapter: postHogAdapter.isFeatureEnabled(),
  identify,
})

Then use it in your framework:

import { exampleFlag } from "@/flags";

export default async function Page() {
  const exampleValue = await exampleFlag();

  return <div>Example Flag: {String(exampleValue)}</div>;
}

How to inform the Flags Explorer about flags

You can view and override these flags using the Flags Explorer

For getProviderData, you will also need a personal API key and your project ID.

# Settings > User > Personal API keys
POSTHOG_PERSONAL_API_KEY=key
# Settings > Project > Project ID
POSTHOG_PROJECT_ID=projectId
import { createFlagsDiscoveryEndpoint } from 'flags/next'
import { getProviderData as getPostHogProviderData } from '@flags-sdk/posthog'
import * as flags from '../../../../flags'

export const GET = createFlagsDiscoveryEndpoint(() => getPostHogProviderData({
  personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY,
  projectId: process.env.NEXT_PUBLIC_POSTHOG_PROJECT_ID,
}))

Learn more about the Flags Explorer