Node.js SDK
Loads the vault into process.env when your app starts. Everything is verified and decrypted in your process.
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:
npm install @brunosps00/coffreboxFor now, from the repository (needs Rust with the wasm32-unknown-unknown target and wasm-pack):
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.tgzThere’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.
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
const { Coffrebox } = require("@brunosps00/coffrebox")
async function main() {
await Coffrebox.load()
require("./server").start()
}
main()Options
const vault = await Coffrebox.load({
override: false,
watch: false,
cacheDir: "/var/cache/my-api",
offline: "use-cache",
warn: (msg) => logger.warn(msg),
})| Option | Default | What it does |
|---|---|---|
override | false | If the variable already exists in process.env: false keeps the existing one, true replaces it with the vault’s value. |
watch | off | true or { intervalMs }: checks the vault periodically and keeps process.env up to date. Minimum 30 s. |
url and token | COFFREBOX_URL and COFFREBOX_TOKEN | The server address and the application credential. |
cacheDir | COFFREBOX_CACHE_DIR | Folder for the on-disk cache, which stores only ciphertext. Without the variable, ~/.cache/coffrebox. |
ttlMs | 5 min | After this time, get() checks the server again. |
maxStaleMs | 72 h | With the server down, a cache older than this is rejected. |
offline | "use-cache" | "fail" makes a server outage an error, even with a cache. |
warn | console.warn | Where 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:
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
304with 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.envvariables it set itself. If a secret is removed from the vault, it’s removed fromprocess.env(or the value it had before comes back). - Code that already read
process.env.Xkeeps the old value: useonChangeto 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:
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 valuesErrors
Every error inherits from CoffreboxError and has a stable code. Messages never include values or the token.
| Class | code | When |
|---|---|---|
NotFoundError | COFFREBOX_NOT_FOUND | get() for a secret that doesn’t exist. |
RevokedError | COFFREBOX_REVOKED | The access was revoked. The on-disk cache is deleted. |
OfflineError | COFFREBOX_OFFLINE | The server is down and there’s no usable cache (or offline: "fail"). |
TamperedError | COFFREBOX_TAMPERED | A signature, a hash, or the revision didn’t match. Nothing from the response is used. |
StaleCacheError | COFFREBOX_STALE_CACHE | The server is down and the cache is older than maxStaleMs. |
ConfigError | COFFREBOX_INVALID_CONFIG | A URL, token, or option is missing or invalid, or the client was already closed. |
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:
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_TOKENcarries 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 with0600permissions. - 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 ishttp://outside localhost.
More details in Security.