Set up request identity
Use stateless visitor bucketing for flags
Request identity derives a bucketing subject from the client IP, user agent, and an experiment-specific salt. Use it when client-side visitor storage is not appropriate.
Configure the root salt
Call setSaltBucketingConfig() before evaluating any request identity. The
configuration is held in package module state, so configure every runtime
module graph that evaluates request identity—not merely once for the conceptual
application. Choose whether the salt is fixed or rotates over time.
Next.js can compile instrumentation.ts, proxy.ts or middleware.ts, and
server routes into isolated module graphs. Configuring the package from
instrumentation does not guarantee that the package instance in the proxy is
configured. Put the setup in a small application-owned bootstrap function and
call it from each relevant entrypoint or a module that entrypoint imports.
Set BUCKETING_ROOT_SALT to a long, randomly generated secret in your
deployment environment. A fixed root salt is supported indefinitely.
// request-identity-bootstrap.ts
import { setSaltBucketingConfig } from "@octopus-energy/flags/identity";
export function bootstrapRequestIdentity() {
setSaltBucketingConfig({
getRootSalt: () => process.env.BUCKETING_ROOT_SALT,
onSaltMissing: (error) => console.error(error),
});
}Invoke that bootstrap in each evaluating module graph before creating or using request identity. For example, Storyblok middleware must initialize the proxy's copy explicitly:
// proxy.ts
import { bootstrapRequestIdentity } from "./request-identity-bootstrap";
// The proxy is compiled separately, so instrumentation alone is insufficient.
bootstrapRequestIdentity();Add request identity to a flag
Give the experiment a stable, unique name—normally the flag key. The example uses Vercel Flags as Global Config flags do not support identity-based decisions.
// flags.ts
import { vercelAdapter } from "@flags-sdk/vercel";
import { createRequestIdentity } from "@octopus-energy/flags/identity";
import { flag } from "flags/next";
import { bootstrapRequestIdentity } from "./request-identity-bootstrap";
// This server entrypoint can be compiled into its own module graph.
bootstrapRequestIdentity();
export const heroFlag = flag({
key: "hero-flag",
adapter: vercelAdapter,
identify: createRequestIdentity({ experimentName: "hero-flag" }),
defaultValue: "control",
options: ["control", "variant"],
});