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

推荐订阅源

博客园_首页
N
Netflix TechBlog - Medium
V
Visual Studio Blog
博客园 - Franky
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
V2EX
The Cloudflare Blog
月光博客
月光博客
Last Week in AI
Last Week in AI
雷峰网
雷峰网
WordPress大学
WordPress大学
博客园 - 【当耐特】
博客园 - 聂微东
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

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