Programmatic Usage
Use the SecretStash Node module as a project dependency for programmatic access to the SecretStash API.
When installed as a project dependency, the SecretStash Node module exposes a full TypeScript API for managing variables, device keys, envelopes, and applications programmatically. This is ideal for custom build scripts, server-side integrations, and automation workflows.
Installation
Client Setup
All API interactions go through the SecretStashClient class. By default, it reads credentials from environment variables or your .env file:
You can also pass configuration directly:
If constructor arguments are omitted, the client resolves configuration from system environment variables, then the .env file, then defaults. See Configuration for details.
Managing Variables
The VariablesManager handles listing, pulling, and pushing environment variables.
Listing Variables
Retrieve a masked list of variables for an environment:
Returns ListVariablesResult:
| Property | Type | Description |
|---|---|---|
variables | Array | List of variables with id, name, maskedValue, and created_at. |
total | number | Total number of variables. |
Pulling Variables
Decrypt and merge remote variables into a local .env file:
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
client | SecretStashClient | — | The API client. |
applicationId | string | — | Your application ID. |
environmentSlug | string | — | The environment slug (e.g., production). |
filePath | string | ".env" | Path to the .env file to update. |
Returns PullVariablesResult:
| Property | Type | Description |
|---|---|---|
filePath | string | The path to the updated .env file. |
variableCount | number | Number of variables written. |
Pushing Variables
Encrypt and upload local .env variables to SecretStash:
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
client | SecretStashClient | — | The API client. |
applicationId | string | — | Your application ID. |
environmentSlug | string | — | The environment slug. |
filePath | string | ".env" | Path to the .env file to read. |
Returns PushVariablesResult:
| Property | Type | Description |
|---|---|---|
created | number | Number of variables successfully pushed. |
failed | number | Number of variables that failed to push. |
Push is blocked for environments with the testing type. This restriction is enforced at both the client and API level.
Managing Device Keys
The KeyManager handles local RSA key pair generation, registration, and recovery.
You can optionally pass a custom key directory:
Initializing a Device Key
Generate and register a new RSA-4096 key pair:
Options (KeyInitOptions):
| Property | Type | Default | Description |
|---|---|---|---|
label | string | hostname | A human-readable label for this device. |
force | boolean | false | Force regeneration of existing keys. |
temporary | boolean | false | Create a short-lived key for CI/CD. |
ttlMinutes | number | 15 | TTL in minutes for temporary keys. |
Returns KeyInitResult:
| Property | Type | Description |
|---|---|---|
deviceKeyId | number | The server-assigned device key ID. |
label | string | The device label. |
fingerprint | string | SHA-256 fingerprint of the public key. |
keyDirectory | string | Path to the key storage directory. |
isTemporary | boolean | Whether this is a temporary key. |
expiresAt | string | null | Expiration timestamp for temporary keys. |
Checking Key Status
Syncing Device Metadata
Generating a Recovery Key
Managing Environments
The EnvironmentsManager handles listing and creating environments.
Listing Environments
Creating an Environment
Managing Envelopes
The EnvelopeManager handles envelope rewrapping, repair, and reset operations.
Rewrapping an Envelope
Migrate access from an old device key to your current device key:
Resetting Envelopes
Generate a new DEK and create envelopes for all registered devices:
Repairing an Envelope
Attempt a rewrap; if that fails, fall back to a full reset:
Managing Applications
The ApplicationsManager provides read access to your available applications.
Error Handling
The module throws typed errors that you can catch and handle:
| Error Class | Description |
|---|---|
InvalidApiToken | The API token is invalid or expired. |
MissingApiToken | No API token was configured. |
InvalidEnvironmentConfiguration | Required configuration is missing or invalid. |
NoEnvironmentsFound | The specified environment does not exist. |
DeviceKeyNotRegistered | No local device key found or key not registered on server. |
PrivateKeyNotFound | The local private key file is missing. |
PrivateKeyFailedToSave | Failed to save the private key to disk. |
MetaKeyFailedToSave | Failed to save the device metadata file. |
Exported Types
The module also exports the following TypeScript types for use in your application:
| Type | Description |
|---|---|
KeyInitOptions | Options for KeyManager.init(). |
KeyStatusResult | Return type of KeyManager.status(). |
KeyInitResult | Return type of KeyManager.init(). |
RecoveryKeyResult | Return type of KeyManager.generateRecoveryKey(). |
ListVariablesResult | Return type of VariablesManager.list(). |
PullVariablesResult | Return type of VariablesManager.pull(). |
PushVariablesResult | Return type of VariablesManager.push(). |
ListEnvironmentsResult | Return type of EnvironmentsManager.list(). |
CreateEnvironmentResult | Return type of EnvironmentsManager.create(). |
ListApplicationsResult | Return type of ApplicationsManager.list(). |
RewrapOptions | Options for EnvelopeManager.rewrap() and repair(). |
ResetResult | Return type of EnvelopeManager.reset(). |
DeviceMetadata | Device key metadata structure. |
EnvelopePayload | Encrypted envelope structure. |
ApplicationData | Application record from the API. |
EnvironmentData | Environment record from the API. |
EnvironmentType | Enum of environment types. |
Node module source code: https://github.com/dniccum/secret-stash-node