Skip to content
In this guideNode.js SDK

Node.js SDK

Loads the vault into process.env when your app starts. Everything is verified and decrypted in your process.

  • @brunosps00/coffrebox 0.1.0 (npm coming soon)
  • Node.js 20+
  • ESM and CommonJS
  • TypeScript types

Install

The @brunosps00/coffrebox package isn’t on npm yet. Until it is, run your app with the coffre CLI (coffre run -- node server.js), which hands the secrets over as environment variables, or build the SDK from the repository as shown below.

Once the package is out:

Terminal
npm install @brunosps00/coffrebox

For now, from the repository (needs Rust with the wasm32-unknown-unknown target and wasm-pack):

Terminal
git clone --branch v0.1.0 https://github.com/brunosps/coffrebox-sdk
cd coffrebox-sdk/packages/sdk-ts
npm ci && npm run build:wasm && npm run build && npm pack
# in your project:
npm install /path/to/coffrebox-sdk/packages/sdk-ts/brunosps00-coffrebox-0.1.0.tgz

There’s no native dependency: the cryptography ships compiled to WebAssembly inside the package.

Load at startup

Call load() at the start of your entry point, before the rest of your app. It reads COFFREBOX_URL and COFFREBOX_TOKEN from the environment.

server.js
import { Coffrebox } from "@brunosps00/coffrebox"

await Coffrebox.load()

// From here on, process.env.DATABASE_URL holds the value from the vault
startServer()

load() fails closed. If it can’t load the secrets (server down with no valid cache, access revoked, tampered response), it throws an error and your app doesn’t start half-configured.

Nothing happens on import: the SDK only talks to the server when you call load() or connect().

CommonJS

server.cjs
const { Coffrebox } = require("@brunosps00/coffrebox")

async function main() {
  await Coffrebox.load()
  require("./server").start()
}
main()

Options

server.js
const vault = await Coffrebox.load({
  override: false,
  watch: false,
  cacheDir: "/var/cache/my-api",
  offline: "use-cache",
  warn: (msg) => logger.warn(msg),
})
OptionDefaultWhat it does
overridefalseIf the variable already exists in process.env: false keeps the existing one, true replaces it with the vault’s value.
watchofftrue or { intervalMs }: checks the vault periodically and keeps process.env up to date. Minimum 30 s.
url and tokenCOFFREBOX_URL and COFFREBOX_TOKENThe server address and the application credential.
cacheDirCOFFREBOX_CACHE_DIRFolder for the on-disk cache, which stores only ciphertext. Without the variable, ~/.cache/coffrebox.
ttlMs5 minAfter this time, get() checks the server again.
maxStaleMs72 hWith the server down, a cache older than this is rejected.
offline"use-cache""fail" makes a server outage an error, even with a cache.
warnconsole.warnWhere warnings go. They never include values.

Live updates

With watch off, there are no background requests: the SDK reads the vault at startup and only goes back to the server when a get() finds the values older than ttlMs (5 minutes). process.env doesn’t change: to get new values into it, restart your app.

With it on, the SDK asks the server every 5 minutes whether the vault changed:

server.js
const vault = await Coffrebox.load({ watch: true }) // or { watch: { intervalMs: 60_000 } }

vault.onChange((names) => {
  // names = ["DATABASE_URL"]: reconnect the database pool, for example
})
  • No change: the server responds 304 with no body. Nothing is downloaded or decrypted.
  • A change: the whole encrypted vault comes down, a few KB for dozens of secrets. The signature covers the whole set, and that’s what stops the server from hiding or swapping a single item. The SDK compares the values in your process and reports the exact names: the server doesn’t know which name changed, because the names are encrypted too.
  • The SDK only updates the process.env variables it set itself. If a secret is removed from the vault, it’s removed from process.env (or the value it had before comes back).
  • Code that already read process.env.X keeps the old value: use onChange to react.

Cost: with watch on, about 20 small requests per hour per process (12 checks, plus renewing the access token every 15 minutes).

Read values

connect() does the same as load(), without touching process.env:

server.js
const vault = await Coffrebox.connect()

await vault.get("DATABASE_URL")          // throws NotFoundError if missing
await vault.getOptional("SENTRY_DSN")    // undefined if missing
await vault.all()                        // { NAME: value, ... }
await vault.loadIntoEnv({ override: false })
await vault.refresh()                    // checks the server now
const unsubscribe = vault.onChange((names) => {})
await vault.close()                      // stops the checks and forgets the values

Errors

Every error inherits from CoffreboxError and has a stable code. Messages never include values or the token.

ClasscodeWhen
NotFoundErrorCOFFREBOX_NOT_FOUNDget() for a secret that doesn’t exist.
RevokedErrorCOFFREBOX_REVOKEDThe access was revoked. The on-disk cache is deleted.
OfflineErrorCOFFREBOX_OFFLINEThe server is down and there’s no usable cache (or offline: "fail").
TamperedErrorCOFFREBOX_TAMPEREDA signature, a hash, or the revision didn’t match. Nothing from the response is used.
StaleCacheErrorCOFFREBOX_STALE_CACHEThe server is down and the cache is older than maxStaleMs.
ConfigErrorCOFFREBOX_INVALID_CONFIGA URL, token, or option is missing or invalid, or the client was already closed.
server.js
import { Coffrebox, CoffreboxError } from "@brunosps00/coffrebox"

try {
  await Coffrebox.load()
} catch (err) {
  if (err instanceof CoffreboxError) console.error(err.code, err.message)
  process.exit(1)
}

Two vaults in the same process

Each token reads a single vault. To read two, create two clients with explicit options; the cache keeps them apart:

server.js
const own = await Coffrebox.connect({ url, token: process.env.COFFREBOX_TOKEN })
const shared = await Coffrebox.connect({ url, token: process.env.SHARED_COFFREBOX_TOKEN })

const db = await own.get("DATABASE_URL")
const payments = await shared.get("PAYMENTS_API_KEY")

Security

  • The token is the key. COFFREBOX_TOKEN carries the access’s private keys: anyone who has the token can read that vault, only that one, and only for reading. Treat it like a password, and revoke the access if a machine is compromised.
  • The on-disk cache is only ciphertext: the snapshot exactly as the server sent it, at <cacheDir>/<app_id>/<vault_id>.snapshot, written in one step with 0600 permissions.
  • Rollback protection: a snapshot with a revision lower than one already seen (in memory or on disk) is rejected, so the server can’t bring back an old secret.
  • Revocation: when the server says the access was revoked, the SDK deletes the cache and throws RevokedError.
  • Use https:// in production. The SDK warns you when the URL is http:// outside localhost.

More details in Security.