Getting flag updates

By default, SDKs check for updates to your flag logic in the background. When you save and activate a new commit from the Hypertune UI, SDKs will fetch your new flag logic and use it on the next flag evaluation.

You can change the frequency at which the SDK checks for updates by setting initDataRefreshIntervalMs in your createSource options:

src/lib/getHypertune.ts
import { createSource } from "../generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  initDataRefreshIntervalMs: 5_000,
});

export default async function getHypertune() {
  // Get flag updates in serverless environments
  // await hypertuneSource.initIfNeeded();

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === "development"
            ? "development"
            : "production",
        user: { id: "1", name: "Test", email: "hi@test.com" },
      },
    },
  });
}

You can disable checking for updates by setting the shouldRefreshInitData option to false:

src/lib/getHypertune.ts
import { createSource } from "../generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  shouldRefreshInitData: false,
});

export default async function getHypertune() {
  // Get flag updates in serverless environments
  // await hypertuneSource.initIfNeeded();

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === "development"
            ? "development"
            : "production",
        user: { id: "1", name: "Test", email: "hi@test.com" },
      },
    },
  });
}

Manually check for flag updates

You can manually check for flag updates with the initIfNeeded method:

import { createSource } from "../generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  initDataRefreshIntervalMs: 5_000,
});

export default async function getHypertune() {
  await hypertuneSource.initIfNeeded(); // Check for flag updates

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === "development"
            ? "development"
            : "production",
        user: { id: "1", name: "Test", email: "hi@test.com" },
      },
    },
  });
}

When using initIfNeeded, the initDataRefreshIntervalMs option specifies the minimum time between initialization requests.

For example, if you set this to 5_000, initIfNeeded will only trigger a new initialization request if the last one was over 5 seconds ago.

So you can await initIfNeeded on every backend request to ensure flag values are fresh while minimizing network latency and bandwidth.

This is particularly useful in serverless and edge environments like Vercel deployments, Cloudflare Workers, AWS Lambdas, etc, where background SDK tasks like fetching updates aren't guaranteed to execute.

Subscribing to flag updates

You can add and remove listeners to be notified of updates with:

hypertune.addUpdateListener(listener: (newStateHash: string) => void)
hypertune.removeUpdateListener(listener: (newStateHash: string) => void)

You can also get the current state hash with:

hypertune.getStateHash(): string | null

Last updated