Skip to main content
Use the Barekey SDK when your code should read variables directly instead of shelling out to the CLI or depending on a pulled .env file. For most server-side code, import from @barekey/sdk/server.

Install

Package entrypoints

The simplest server client

This works when:
  • barekey.json can be found in the current directory or a parent directory
  • the runtime can authenticate with either BAREKEY_ACCESS_TOKEN or a stored CLI login

How the server SDK resolves configuration

BarekeyClient supports three configuration styles.

Option 1: rely on barekey.json

This is usually the best local-development setup.

Option 2: pass the scope explicitly

Option 3: pass a json object

You must provide either:
  • all of organization, project, and environment
  • or json
  • or nothing, in which case Barekey tries to load barekey.json
Do not mix explicit fields with 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 for barekey.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:
  1. BAREKEY_ACCESS_TOKEN
  2. 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_URL overrides the default API base URL
Example:
This is the best fit for production deploys and CI.

CLI session fallback

If BAREKEY_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:
Then your app can usually run without extra token wiring.

Reading values

get() returns a promise-like handle. If the variable has a known generated type, await returns the parsed value directly.
Batch reads preserve input order:

Use inspect() when you need metadata

This is especially useful for:
  • debugging
  • logging resolution metadata
  • checking ab_roll decisions

Dynamic reads and caching

get() accepts BarekeyGetOptions:

dynamic

Use dynamic when a value should be refreshed instead of coming from the static definition cache.
With a TTL:
ttl means:
  • numbers are milliseconds
  • Date uses its timestamp
  • objects with epochMilliseconds are also accepted

seed and key

Use these for deterministic ab_roll evaluation:
The same 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:
Then known keys become typed in the SDK.

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:
Or:
Notes:
  • automatic typegen refresh only runs in development
  • it only applies when filesystem access is available
  • set typegen: false to 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.
This works with any library that exposes Standard Schema v1 metadata, including Zod and ArkType-compatible schemas. Use this when you want startup-time guarantees that your resolved Barekey values form a valid config object.

Standalone mode

Standalone mode makes the server SDK read local .env* files instead of calling the Barekey API. Set:
In this mode:
  • filesystem access is required
  • organization, project, and environment may be omitted
  • typegen becomes minimal
  • values are inferred from local .env content
This is useful when you want one SDK API for local and centralized setups. Read more in Local development.

Public client

Use PublicBarekeyClient 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:
You can also pass a public client directly to 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
If config.mode is "standalone", PublicBarekeyClient throws.

Errors you should expect

The SDK throws BarekeyError subclasses. Common ones: Example:

Practical patterns

  • Create one shared client module and import it where needed.
  • Use CLI-session auth locally and BAREKEY_ACCESS_TOKEN in production.
  • Run barekey typegen after config changes.
  • Use inspect() when you need metadata, not just the value.
  • Use standalone mode only on the server side.
For CLI-driven local workflows, read CLI and Local development.