.env file.
For most server-side code, import from @barekey/sdk/server.
Install
- npm
- pnpm
- yarn
- bun
Package entrypoints
| Import | Use for |
|---|---|
@barekey/sdk/server | server-side reads, CLI-session auth, API-backed mode, standalone .env mode, and typegen |
@barekey/sdk/public | public variables only, including browser-facing reads |
@barekey/sdk | combined root export, but the explicit subpaths are clearer in app code |
The simplest server client
barekey.jsoncan be found in the current directory or a parent directory- the runtime can authenticate with either
BAREKEY_ACCESS_TOKENor a stored CLI login
How the server SDK resolves configuration
BarekeyClient supports three configuration styles.
Option 1: rely on barekey.json
Option 2: pass the scope explicitly
Option 3: pass a json object
- all of
organization,project, andenvironment - or
json - or nothing, in which case Barekey tries to load
barekey.json
json.
barekey.json reference
This file is used by both the SDK and CLI.
Top-level keys
| Key | Type | Required | Notes |
|---|---|---|---|
organization | string | Yes in centralized mode | Canonical organization key |
project | string | Yes in centralized mode | Project slug |
environment | string | Yes in centralized mode | Stage name |
org | string | No | Alias for organization |
stage | string | No | Alias for environment |
config | object | No | Extra SDK and runtime behavior |
config keys
| Key | Allowed values | Default | What it does |
|---|---|---|---|
config.mode | "centralized" or "standalone" | "centralized" | Chooses API-backed Barekey mode or local .env file mode |
config.typegen | "semantic" or "minimal" | "semantic" in centralized mode, "minimal" in standalone mode | Controls the style of generated SDK types |
Search behavior
The SDK and CLI search forbarekey.json starting from the current working directory and walking upward through parent directories.
That means one repo-level file can cover a whole monorepo.
How auth is resolved
In centralized mode,BarekeyClient resolves credentials in this order:
BAREKEY_ACCESS_TOKEN- a stored CLI session created by
barekey auth login
BAREKEY_ACCESS_TOKEN
If BAREKEY_ACCESS_TOKEN is set, the SDK uses it directly.
Optional:
BAREKEY_API_URLoverrides the default API base URL
CLI session fallback
IfBAREKEY_ACCESS_TOKEN is not set, the SDK tries to reuse the local CLI login. That is convenient for local development because you can log in once with:
Reading values
get() returns a promise-like handle.
If the variable has a known generated type, await returns the parsed value directly.
Use inspect() when you need metadata
- debugging
- logging resolution metadata
- checking
ab_rolldecisions
Dynamic reads and caching
get() accepts BarekeyGetOptions:
dynamic
Use dynamic when a value should be refreshed instead of coming from the static definition cache.
ttl means:
- numbers are milliseconds
Dateuses its timestamp- objects with
epochMillisecondsare also accepted
seed and key
Use these for deterministic ab_roll evaluation:
seed and key pair gives the same result for the same variable.
Typegen
Barekey can write generated types into your installed@barekey/sdk package.
Run:
Typegen modes
semantic
This is the centralized default.
It preserves Barekey metadata in the generated types, including:
- mode
- visibility
minimal
This is the standalone default.
It generates simpler value types, which is useful when types are inferred from local .env files.
SDK-side typegen refresh
BarekeyClient also accepts:
- automatic typegen refresh only runs in
development - it only applies when filesystem access is available
- set
typegen: falseto disable it
Requirements validation
You can validate the resolved configuration against a Standard Schema v1 validator before reads proceed.Standalone mode
Standalone mode makes the server SDK read local.env* files instead of calling the Barekey API.
Set:
- filesystem access is required
organization,project, andenvironmentmay be omittedtypegenbecomesminimal- values are inferred from local
.envcontent
Public client
UsePublicBarekeyClient for public variables:
Important public-client rules
- it only reads public variables
- it does not use CLI auth
- it supports
baseUrl - it does not support standalone mode
config.mode is "standalone", PublicBarekeyClient throws.
Errors you should expect
The SDK throwsBarekeyError subclasses. Common ones:
| Error code | Meaning |
|---|---|
NO_CONFIGURATION_PROVIDED | Barekey could not find explicit config or a barekey.json file |
INVALID_CONFIGURATION_PROVIDED | Config shape is incomplete or invalid |
NO_CREDENTIALS_PROVIDED | No access token and no CLI session were available |
INVALID_CREDENTIALS_PROVIDED | The stored or provided credentials are invalid |
VARIABLE_NOT_FOUND | The named variable does not exist |
UNAUTHORIZED | The API rejected the current token |
REQUIREMENTS_VALIDATION_FAILED | Your requirements schema rejected the resolved config |
Practical patterns
- Create one shared client module and import it where needed.
- Use CLI-session auth locally and
BAREKEY_ACCESS_TOKENin production. - Run
barekey typegenafter config changes. - Use
inspect()when you need metadata, not just the value. - Use standalone mode only on the server side.

