Comment on page
Node.js quickstart
Once you have a Node.js application ready, install Hypertune's JavaScript SDK:
npm
yarn
pnpm
npm install hypertune
yarn add hypertune
pnpm add hypertune
Define the following environment variables in your
.env
file:HYPERTUNE_TOKEN=token
HYPERTUNE_OUTPUT_FILE_PATH=src/generated/generated.ts
Replace
token
with your project token which you can find in the Settings tab of your project.Generate a type-safe client to access your flags by running:
npm
yarn
pnpm
npx hypertune
yarn hypertune
pnpm hypertune
Add a new file called
hypertune.ts
that creates and exports a hypertune
singleton:import { initializeHypertune } from "./generated/generated";
const hypertune = initializeHypertune({}, {
token: process.env.HYPERTUNE_TOKEN
});
export default hypertune;
Then import and use this
hypertune
singleton to access your flags with full type-safety:import express from "express";
import hypertune from "./hypertune";
export default function getApp() {
const app = express();
app.get("/exampleFlag", async (req, res, next) => {
await hypertune.initFromServerIfNeeded();
const rootNode = hypertune.root({
context: {
user: { id: "test_id", name: "Test", email: "[email protected]" },
},
});
const exampleFlag = rootNode.exampleFlag().get(/* fallback */ false);
res.status(200).json({ exampleFlag });
});
return app;
}
If you try accessing your flag just as your backend instance starts up, you'll get your hardcoded fallback value if the SDK hasn't had a chance to initialize from Hypertune Edge yet. To avoid this, you can include a snapshot of your flag logic in the generated client as a build-time fallback.
Add the following environment variable to your
.env
file:HYPERTUNE_INCLUDE_FALLBACK=true
Then regenerate the client.
The SDK will now instantly initialize from the snapshot first before fetching the latest flag logic from Hypertune Edge. And it will always successfully initialize, even if Hypertune Edge is unreachable. You can keep the snapshot fresh by setting up a webhook to regenerate the client on every Hypertune commit.
If you don't want to include the snapshot but still want to avoid using fallback values, you can explicitly wait for initialization from Hypertune Edge with
await hypertune.initFromServerIfNeeded()
or check for initialization from Hypertune Edge with hypertune.hasInitializedFromServer()
.Now you can update the logic for
exampleFlag
from the Hypertune UI without updating your code or waiting for a new build, deployment or service restart.To add a new flag, create it in the Hypertune UI then regenerate the client.