twc/secrets
Read a tenant's secrets from a script with getSecret from the twc/secrets module - key rules, versions, caching, missing and expired secrets.
Scripts regularly need a credential: the password of a CRM account, a client secret, a signing key. Writing such a value in a script or in a YAML file exposes it to everyone who can read the configuration and keeps it in the configuration history. The twc/secrets module solves this: a secret is stored once for the tenant, and a script reads it at run time with getSecret. Scripts that call external systems should use it for every credential, and should never contain one.
Import and signature
import { getSecret } from "twc/secrets";
const value = await getSecret("CRM-API-KEY");getSecret(key)
key: string
A promise of the secret value as a string, or null when no usable value exists.
The import is written at the top of the script file, before the export default function. The old name neo/secrets still works and is described on the page Obsolete neostore names. The prefix neostore is the platform's former name and is kept in technical identifiers.
Keys
A key is a string of 2 to 32 characters, made of letters, digits and hyphens only. Keys are case-sensitive: CRM-API-KEY and crm-api-key are two different secrets. A key that does not follow these rules makes getSecret throw an error that explains which rule is broken, so a typo in the name of the secret fails visibly while a well-formed name that does not exist returns null.
Secrets belong to one tenant. A script can read only the secrets of the tenant it runs for, and the same key can hold different values in different tenants. Secrets are created and updated in the admin console by a user whose role can manage secrets, or with the Secret API; see API keys and secrets for the procedure. A secret value is never returned by the admin console after it is saved, which is the reason a script is the right place to use it.
Versions, activation and expiry
A secret can have several versions, for example when a credential is rotated. getSecret returns the most recently updated version that is currently usable. A version is usable when it is enabled, has not expired and its activation date, when one was set, has passed. A version with an empty value is skipped. A rotation can therefore be prepared in advance: a new version with a future activation date takes over by itself at that moment, and scripts need no change.
When no version is usable, getSecret returns null. This covers a key that was never created, a secret whose versions are all disabled or expired, and a secret whose new version is not active yet. The script decides what a missing credential means; ignoring null produces a request without credentials that the remote system rejects.
Caching
The result of a lookup, null included, is kept for up to 10 minutes. The cache is shorter when a version is about to become active or to expire within that time, so that a scheduled rotation takes effect at the planned moment. Calling getSecret in each handler is therefore inexpensive. The delay also means that a secret saved in the admin console may take a few minutes to be seen by scripts, and that a key created after a first failed lookup can keep answering null for a few minutes.
Example
The handler below reads the key when it runs, fails softly when the secret is absent, and calls the remote system with the value.
Checking the result
A script is tested by running the flow that calls it and by checking the outcome on the remote system. When the remote system answers 401 or 403, the first suspects are a missing secret (the script receives null), a key typed with a different case, and a version that is not active yet. Logging whether the value is present, never the value itself, is enough to tell them apart.
FAQ
Last updated

