.env file.
For most server-side code, import from @barekey/sdk/server.
Install
- npm
- pnpm
- yarn
- bun
Package entrypoints
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
config keys
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 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:
- kind
- visibility
- rollout state
minimal
This is the standalone default.
It only generates the resolved value type, such as string, boolean, or an inferred object shape 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 any Standard Schema v1 validator before reads proceed. Pass the schema directly. You do not need to wrap~standard yourself.
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:
React .tsx
Use @barekey/react when you want public values to feel like normal React data reads.
Install it alongside the SDK:
useBarekey() when you want to bootstrap values before rendering a provider tree:
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:
Example:
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.

