Keel by ChimpLabs
Jump to page or section Esc
Primitives

Secret

Boot-verified redacted secrets backed by environment variables, mounted files, Effect Config, or chained stores.

Secret declares a named credential required by a service or ExternalSystem. Keel wraps secret values in Effect’s Redacted type so logs and JSON serialization do not expose them. verifySecrets checks that declared secrets exist at boot, and deployment scopes each secret only to the service unit that declared it.


Declaring a Secret

Plain TypeScript SDK (@chimp-labs/keel/sdk)

import { k } from "@chimp-labs/keel/sdk"

export const StripeApiKey = k.secret("STRIPE_API_KEY")
export const StripeSigningSecret = k.secret("STRIPE_SIGNING_SECRET")

Effect v4 (@chimp-labs/keel/secret)

import { Secret } from "@chimp-labs/keel/secret"

export class StripeApiKey extends Secret("STRIPE_API_KEY") {}

Attaching to a Service

Declaring a secret in Service.resources or ExternalSystem.secrets has two effects:

  1. verifySecrets checks that the secret exists at startup and fails with MissingSecretsError, which reports every missing key in one error.
  2. @chimp-labs/keel-alchemy injects the secret binding only into the Worker or Docker container that declared it.
export const BillingService = k.service("billing", {
  resources: [InvoicesTable, StripeApiKey],
  operations: { /* ... */ },
})

Operations reference

Scoped usage and redacted reads

// Recommended: unwrap raw value only inside a tight callback scope
const customer = await keel.stripeKey.use(async (apiKey) => {
  const res = await fetch("https://api.stripe.com/v1/customers", {
    headers: { Authorization: `Bearer ${apiKey}` },
  })
  return res.json()
})

// Or read as Redacted<string> (safe against accidental console.log)
const redacted = await keel.stripeKey.getRedacted()

// Or read raw string directly
const raw = await keel.stripeKey.get()

Boot preflight verification

Pass verifySecrets: true to createKeel(...) or service.live(handlers, { verifySecrets: true }) so missing credentials fail at startup instead of during a production request:

const keel = await createKeel({
  backend: "postgres",
  secrets: "env", // or { STRIPE_API_KEY: "..." } or { source: "dir", path: "/run/secrets" }
  resources: { billing: BillingService, stripeKey: StripeApiKey },
  verifySecrets: true,
})

Built-in secret store layers

Layer or source Description Best for
SecretStoreMemory(map) In-memory key map Unit and integration tests
SecretStoreEnv Reads process.env[name] Local dev, Cloudflare Workers, and PaaS hosts
SecretStoreConfig / SecretStoreConfigFrom Reads Effect’s active ConfigProvider Composable Effect config
SecretStoreDir(dirPath) Reads mounted files <dir>/<NAME> with path-traversal protection Docker Compose secrets: and Kubernetes /run/secrets
SecretStoreChain(...layers) Falls through on SecretNotFoundError only Layered local, mounted dir, and env precedence

Complete SDK and Effect API mapping

SDK method (keel.<secret>.*) Effect function (@chimp-labs/keel/secret) Description
use(fn) useSecret(secret, fn) Unwrap the raw string inside a scoped callback.
getRedacted() getSecret(secret) Read Redacted.Redacted<string>.
get() getSecret(secret) with Redacted.value Read the unwrapped secret string.
verifySecrets: true verifySecrets(descriptors) Run boot preflight and collect all missing secret names.

Errors and failure modes

Error tag Category HTTP status Retryable When it occurs
SecretNotFoundError not_found 404 No Requested secret is missing from all configured stores.
MissingSecretsError internal 500 No verifySecrets finds one or more missing secrets at boot.
SecretStoreError internal 500 Yes Underlying file or environment store read fails.

Backends and infrastructure

  • Every SecretStore layer (memory, env, config, dir, and chain) works with PostgreSQL 17 (KeelPostgresLive or backend: "postgres"), SurrealDB 4, SQLite, and Memory. KeelPostgresConfigLive reads DATABASE_URL and POSTGRES_* credentials as Redacted values through Effect’s ConfigProvider.
  • On Cloudflare Workers (@chimp-labs/keel-alchemy/cloudflare/stack), keelCloudflareStack and provisionKeelCloudflareTopology bind declared secrets strictly to the Worker units that reference them. On Docker and Kubernetes, mount secrets into /run/secrets and wire SecretStoreChain(SecretStoreDir("/run/secrets"), SecretStoreConfig).

Testing in memory

await using keel = await createKeel({
  backend: "memory",
  secrets: { STRIPE_API_KEY: "sk_test_123" },
  resources: { stripeKey: StripeApiKey },
  verifySecrets: true,
})

const prefix = await keel.stripeKey.use((raw) => raw.slice(0, 7))
// prefix === "sk_test"