Load secrets in a Node server
A Node server can read its secrets from Mocco when it starts, with no CLI and no .env file on the machine. @mocco/node reads one folder of one environment with a secret API key and gives you the values as a map or as environment variables.
Create a key
Create a secret API key with secrets:read, bound to the environment the server runs in and the folders it needs (for example production and /api/**). See API keys. Store the key where the server gets its other configuration, as MOCCO_SECRET_KEY for example. It is the only value the server needs to know in advance.
Install
npm install @mocco/node --@mocco:registry=https://packages.mocco.club/api/packages/fi-workers/npm/Mocco's packages are on Mocco's package registry, not npmjs.com. The flag points the @mocco scope there.
Load into the environment at boot
import { createMoccoServer } from '@mocco/node';
const mocco = createMoccoServer({ secretKey: process.env.MOCCO_SECRET_KEY! });
await mocco.secrets.loadIntoEnv({ path: '/api' });
// process.env.DATABASE_URL and the rest are set from here on.Load before the code that reads the variables runs. With ES modules, put this in the file you start and import the rest of the app after it.
loadIntoEnv keeps a variable that is already set, so a value you set by hand on one machine still wins. Pass override: true to replace them. It answers which keys it set and which it kept, by name only.
path is the folder, / when you leave it out. environment may name the key's environment and must then be that one. Imports come first, then the folder's own secrets, with references resolved: the same values mocco secrets run gives a command.
Use the values without the environment
const secrets = await mocco.secrets.load({ path: '/api' });
const db = connect(secrets.DATABASE_URL);load answers a plain object and changes nothing else.
Keep a long-running server current
const live = await mocco.secrets.live({
path: '/api',
refreshEveryMs: 60_000,
onChange: (secrets) => pool.reconnect(secrets.DATABASE_URL),
});
live.get().DATABASE_URL; // the newest valueslive reads once, then asks every refreshEveryMs whether anything changed. A check that finds nothing new is a short answer with no values in it. When a check fails, the server keeps the last values and onError is called; without it, a warning with no value in it goes to the console. live.refresh() checks at once, and live.close() stops checking. The timer doesn't keep the process running.
The shortest interval is 10 seconds. A key reads at most 60 times a minute, shared by every process that uses it, so give each server a longer interval when many of them share one key.
When a read fails
If a reference or an import couldn't be resolved,
load,loadIntoEnvandlivethrow aSecretsUnresolvedErrorthat lists the keys and why, andloadIntoEnvsets nothing. PassallowUnresolved: trueto use the values anyway; those secrets keep the text as written. See References and imports.A refused read throws a
MoccoErrorwithstatusandcode:insufficient_scopewhen the key lackssecrets:read,secret_scopewhen the environment or folder is outside the key's binding,404while the workspace has secrets off.
Errors and warnings name keys and folders, never a value. Each read is on the workspace's Audit page under the key.