TypeScript - Node.js
The Lekko Node.js SDK lets you add dynamic configuration to your Node.js apps.
This SDK is intended to be used with Lekko’s code transformation tools. Make sure your project is set up with Lekko by following our Getting Started guide.
Prerequisites
- Node.js version 20 or greater
- TypeScript version 5 or greater
- Set up Lekko in your project using the Get started guide.
Install
In your terminal, run the following:
npm install @lekko/js-sdk @lekko/ts-transformer
Set up code transformation
Lekko uses TypeScript Compiler API (opens in a new tab) to make lekkos dynamic. To integrate into the TypeScript build process Lekko uses ts-patch
(opens in a new tab), which is a wrapper around the TypeScript compiler (tsc
) that allows running source code transformations during the build.
Add the following to your tsconfig.json
to install Lekko source transformer:
{
"compilerOptions": {
...
"plugins": [
{
"transform": "@lekko/ts-transformer"
}
]
}
}
If you use ts-node
you also need to add this to your tsconfig.json
:
{
...
"ts-node": { "compiler": "ts-patch/compiler" }
}
Then use tspc
instead of tsc
in your build script, for example:
{
...
"scripts": {
"build": "tspc"
...
},
}
Initialize the Lekko client
The Lekko client connects to Lekko's services to periodically refresh lekkos and send evaluation events. In your application entry file:
import { initLekko } from "@lekko/js-sdk";
// Before starting your application, create the global Lekko client
// that manages updates and evaluation events.
await initLekko({ updateIntervalMs: 5000 });
Usage
The following is an example of a lekko file under the lekko directory specified in your project’s .lekko
.
/** Whether to enable beta features */
export function getEnableBeta({ segment }: { segment: string }): boolean {
if (segment.startsWith("enterprise")) {
return true;
}
return false;
}
/** Return text based on environment */
export function getText({ env }: { env: string }): string {
if (env === "production") {
return "hello from prod";
}
return "hello from dev";
}
To use your lekkos, simply call them as normal functions:
import { getText } from "./lekko/example.js";
const server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end(getText({ env: process.env.NODE_ENV ?? "" }));
});
Environment variables
For the client initialization to successfully connect to Lekko, you must provide the the following environment variables when running the project.
LEKKO_API_KEY=<YOUR KEY>
LEKKO_REPOSITORY_OWNER=<GITHUB ORG>
LEKKO_REPOSITORY_NAME=lekko-configs
node --env-file .env.local <app entry>
Debug mode
You can set the LEKKO_DEBUG
environment to true
to enable debug logging of evaluations in your project.
Example:
LEKKO_DEBUG=true
This allows you to see whenever lekkos are evaluated in your code and what contexts they are evaluated with.
[lekko] Debug mode is enabled, unset `LEKKO_DEBUG` environment variable to disable it.
[lekko] Loaded remote lekkos from: lekkodev/lekko-configs commit hash: 9fdc4319340a1ea55aa3a611cbcae98da9672db6.
[lekko] Connected to lekkodev/lekko-configs using API key "lekko_703adc..."
[lekko] Evaluated configs/text using the following context: {some_key: "foo"} to get: "best app ever"