@flags-sdk/posthog

中文文档
1.1.0发布于 2026/09/26MITTypeScriptESM + CJS构建来源已验证近 30 天下载 23.9万

PostHog adapter for the Flags SDK

原始使用文档
文档目录7 节
  1. Setup
  2. Example Usage
  3. Environment variables
  4. Flags Explorer metadata
  5. Evaluation modes
  6. Runtimes
  7. Documentation

Flags SDK — PostHog Adapter

The PostHog adapter for Flags SDK supports dynamic server side feature flags powered by PostHog.

Setup

Install the adapter

pnpm i @flags-sdk/posthog

Example Usage

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

export const marketingGate = flag<boolean>({
  // The key in PostHog
  key: "my_posthog_flag_key_here",
  adapter: postHogAdapter,
});

Environment variables

Always required, read by postHogAdapter:

# Regional API host, determines where your data lives
POSTHOG_HOST=https://us.i.posthog.com # or https://eu.i.posthog.com
# Settings > Project > Project API Key
POSTHOG_PROJECT_API_KEY=phc_...

Optional, opts postHogAdapter into local evaluation:

# Settings > Project > Feature flags secret key
POSTHOG_SECRET_KEY=phs_...

For the Flags Explorer, read by getProviderData only:

# Settings > Project > Project secret API keys (feature_flag:read scope)
POSTHOG_PROJECT_SECRET_API_KEY=phs_...
# Alternatively: Settings > User > Personal API keys (feature_flag:read scope)
# POSTHOG_PERSONAL_API_KEY=phx_...
# Settings > Project > Project ID
POSTHOG_PROJECT_ID=521742

Flags Explorer metadata

getProviderData supports either a project secret API key or a personal API key. Existing personal-key calls continue to use the paginated management API:

import { getProviderData } from "@flags-sdk/posthog";

await getProviderData({
  personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY!,
  projectId: process.env.POSTHOG_PROJECT_ID!,
});

To use a project-scoped key with feature_flag:read, select the definitions API:

await getProviderData({
  projectSecretApiKey: process.env.POSTHOG_PROJECT_SECRET_API_KEY!,
  projectId: process.env.POSTHOG_PROJECT_ID!,
});

Both modes read POSTHOG_HOST by default. appHost overrides the dashboard host; project-key mode also accepts apiHost to override the ingestion host. The project ID supplies dashboard links in project-key mode; authentication needs only the key. Definitions include descriptions (name), boolean or multivariate options, and payload options. As before, nonempty payloads take precedence over flag values. Creation timestamps are available only from the personal-key management API.

Evaluation modes

  • Remote (default): with only POSTHOG_PROJECT_API_KEY and POSTHOG_HOST set, each evaluation calls PostHog. No background polling; request volume scales with traffic. Recommended for serverless.
  • Local: set POSTHOG_SECRET_KEY (phs_...) to opt in. posthog-node polls flag definitions (~30s) and evaluates in-process for lower latency. Polling runs per warm server process and counts against your PostHog feature flag request quota regardless of user traffic.

POSTHOG_PERSONAL_API_KEY and POSTHOG_PROJECT_SECRET_API_KEY are used only by the Flags Explorer (getProviderData) and do not enable local evaluation.

Runtimes

RuntimeSupported
Node✅
Edge Runtime❌

Note: posthog-node does not support the Edge Runtime.

To use with Routing Middleware and precompute, read more: Middleware now supports Node.js

Documentation

View more PostHog documentation at posthog.com.

版本与发布记录

@flags-sdk/posthog 共发布 11 个版本,首次发布于 2025/05/19;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/09/261 个月前
1.0.12026/08/053 个月前
1.0.02026/07/293 个月前
1.0.0-eb2e43c-20260721063140snapshot2026/07/213 个月前
0.2.22025/11/061 年前
0.2.2-fcec5ecd-202511061449522025/11/061 年前

关于 @flags-sdk/posthog 的常见问题

@flags-sdk/posthog 是什么?

PostHog adapter for the Flags SDK

如何安装 @flags-sdk/posthog?

在项目目录运行 npm install @flags-sdk/posthog,或使用 pnpm add / yarn add / bun add。

@flags-sdk/posthog 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flags-sdk/posthog 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flags-sdk/posthog 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.20.0 || >=22.22.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flags-sdk/posthog 可以商用吗?

@flags-sdk/posthog 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flags-sdk/posthog 的下载量有多少?

根据 npm 官方下载统计,@flags-sdk/posthog 在 2026/10/15 – 2026/11/13 这 30 天内被下载 238,997 次,日均约 7,967 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flags-sdk/posthog 还在维护吗?

最新版本 1.1.0 发布于 2026/09/26(1 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 4 个;首次发布于 2025/05/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。