Skip to main content

Shared server client

Create one shared server client module:
import { BarekeyClient } from "@barekey/sdk/server";

export const barekey = new BarekeyClient();
This works especially well when:
  • barekey.json is committed
  • developers use barekey auth login
  • production sets BAREKEY_ACCESS_TOKEN

Production auth pattern

Use a real deployment secret:
BAREKEY_ACCESS_TOKEN=bk_at_...
Then:
const client = new BarekeyClient({
  organization: "acme",
  project: "api",
  environment: "production",
});
This avoids depending on a local CLI session in production.

Local developer pattern

Commit barekey.json and have every developer run:
barekey auth login
barekey typegen
That gives you:
  • CLI commands without repeating flags
  • SDK reads without manually wiring tokens
  • generated types in the local editor

Pull-file pattern

If a tool only understands dotenv files:
barekey env pull --out .env.local
Re-run it whenever upstream values change.

Batch-read pattern

Read a group of startup values together:
const [databaseUrl, redisUrl, stripeKey] = await barekey.get([
  "DATABASE_URL",
  "REDIS_URL",
  "STRIPE_SECRET_KEY",
] as const);

Experiment pattern

Use deterministic ab_roll reads:
const variant = await barekey.get("CHECKOUT_FLOW", {
  seed: user.id,
  key: "checkout-v2",
});

Public-variable pattern

Use the public client for browser-safe reads:
import { PublicBarekeyClient } from "@barekey/sdk/public";

const publicBarekey = new PublicBarekeyClient({
  organization: "acme",
  project: "web",
  environment: "production",
});