GitHub

Reflag

The Reflag provider contains support for Reflag's feature management tools.

Reflag is agent-ready feature flags for TypeScript

The @flags-sdk/reflag provider package exports

Learn more about Adapters

Learn more about the Flags Explorer

Deploy the Reflag template


Setup

The Reflag provider is available in the @flags-sdk/reflag module. Install it with

npm install @flags-sdk/reflag

Provider Instance

Import the default adapter instance reflagAdapter from @flags-sdk/reflag:

import { reflagAdapter } from "@flags-sdk/reflag";

If you need a customized setup, you can import createReflagAdapter from @flags-sdk/reflag and create an adapter instance with your settings:

import { createReflagAdapter } from "@flags-sdk/reflag";
 
const reflagAdapter = createReflagAdapter({
  secretKey: process.env.REFLAG_SECRET_KEY,
});

See the Reflag NodeSDK documentation for the full list of options.


Identify Users and Companies

The Reflag provider uses the identify property to identify users and companies. The identify function is called for every request to determine the user and company context.

Reflag relies on a setting a user/company to evaluate flags for a given request.

Set the identify property to a function which returns a Reflag Context containing user/company properties:

import { dedupe, flag } from "flags/next";
import type { Identify } from "flags";
import { reflagAdapter, type Context } from "@flags-sdk/reflag";
 
const identify = dedupe((async ({ headers, cookies }) => {
  // Your own logic to identify the user
  // Identifying the user should rely on reading cookies and headers only, and
  // not make any network requests, as it's important to keep latency low here.
  const user = await getUser(headers, cookies);
 
  return {
    user: {
      id: user.id,
      name: user.name,
      email: user.email,
    },
    company: {
      id: user.companyId,
    },
  } satisfies Context;
}) satisfies Identify<Context>);
 
export const myFeature = flag<boolean, Context>({
  key: "my_feature",
  identify,
  adapter: reflagAdapter.isEnabled(),
});

Learn more about dedupe

Learn more about identify


Methods

Feature toggling

Through the featureIsEnabled method, the Reflag provider supports determining if features are enabled/disabled.

export const myFeature = flag<boolean, Context>({
  key: "my_feature",
  adapter: reflagAdapter.isEnabled(),
  identify,
});

Remote Config, Adoption tracking and automatic feedback surveys are currently not supported in the Reflag Flags SDK provider.


"Checks" events

Check events are used to log when a user is exposed to a feature. Because middleware and server components are evaluated when routes are prefetched, check events are not supported in the Reflag Provider.

See the Reflag React SDK documentation for more information on how to use check events in the client.


Flags Explorer

View and override your Reflag feature toggles using the Flags Explorer.

To make Flags Explorer aware of your Reflag features, you need to provide a route which Flags Explorer will load your experiment metadata from.

Use the getProviderData function in your Flags API endpoint to load and emit your Reflag data. getProviderData takes a ReflagClient in the options object:

app/.well-known/vercel/flags/route.ts
import { createFlagsDiscoveryEndpoint } from 'flags/next';
import { reflagAdapter, getProviderData } from "@flags-sdk/reflag";
import * as flags from '../../../../flags';
 
export const GET = createFlagsDiscoveryEndpoint(async () => {
  return getProviderData({
    reflagClient: await reflagAdapter.reflagClient(),
  });
});

Read More

Read more about Reflag, Flags SDK, and the Reflag adapter.