The official SDK for Stashbase, a secrets management platform for developers.
Features:
- Manage projects, environments, and secrets from Node.js
- Inject secrets into your process environment
- Simple, promise-based API
- Zero non-development dependencies (security-focused runtime footprint)
- And more...
Install with npm or npm compatible package manager (bun, pnpm, etc.). We recommend using bun for the best experience.
Supported runtime: Node.js 18+.
npm install @stashbase/node-sdkFor full documentation, please visit Stashbase Node SDK.
Here are some common usage examples for the Stashbase Node SDK:
Secret values are validated by UTF-8 byte length. Maximum secret value size: 16 KB.
Use createClient to create a client with explicit scope selection.
import { createClient } from '@stashbase/node-sdk'
const client = createClient({
apiKey: process.env.STASHBASE_API_KEY,
scope: 'workspace', // or "environment"
timeoutMs: 5000, // optional, hard capped at 10000
retries: 3, // optional, at most 3 total attempts (hard capped at 10)
hooks: {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
afterResponse: ({ response }) => console.log('[response]', response.status),
onError: ({ error }) => console.error('[error]', error),
}, // optional
})
console.log(client.scope) // "workspace" or "environment"Use workspace client to manage resources in a workspace, you can use Service Account or Personal API key.
import { createWorkspaceClient } from '@stashbase/node-sdk'
const client = createWorkspaceClient(process.env.STASHBASE_API_KEY)
console.log(client.scope) // "workspace"const client = createWorkspaceClient(process.env.STASHBASE_API_KEY, {
timeoutMs: 5000, // optional, hard capped at 10000
retries: 3, // optional, at most 3 total attempts (hard capped at 10)
hooks: {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
}, // optional
})const { data, error } = await client.projects.list()const { data, error } = await client.environments({ project: 'project-name' }).create({
name: 'api-dev',
isProduction: false,
})const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.list()const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.getMetadata('HOST')const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.listMetadata()This method will load the environment and inject the secrets into the process.
// using workspace client
const { error } = await client.environments({ project: 'project-name' }).load('api-dev')Use environment client to manage resources in a specific environment, using Environment Account API key.
import { createEnvironmentClient } from '@stashbase/node-sdk'
const client = createEnvironmentClient(process.env.STASHBASE_ENV_API_KEY)
console.log(client.scope) // "environment"const client = createEnvironmentClient(process.env.STASHBASE_ENV_API_KEY, {
timeoutMs: 5000, // optional, hard capped at 10000
retries: 3, // optional, at most 3 total attempts (hard capped at 10)
hooks: {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
}, // optional
})- Default request timeout is
5000ms. - Maximum request timeout is
10000ms. - Default maximum attempt count is
3. - Maximum attempt count is
10. timeoutMsis applied per request attempt, not as a total wall-clock budget across all retries.- Retries are automatic for idempotent requests (
GET,PUT, andDELETE) and include rate-limited (429) responses. MutatingPOSTandPATCHrequests are not retried automatically to avoid duplicate side effects.
The transport defaults are also exported from the package root:
import {
DEFAULT_API_TIMEOUT_MS,
MAX_API_TIMEOUT_MS,
DEFAULT_API_RETRIES,
MAX_API_RETRIES,
} from '@stashbase/node-sdk'Hooks can be configured at creation time and updated later at runtime through client.options.hooks.
const client = createWorkspaceClient(process.env.STASHBASE_API_KEY)
client.options.hooks = {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
afterResponse: ({ response }) => console.log('[response]', response.status),
onError: ({ error }) => console.error('[error]', error),
}Hook behavior contract:
beforeRequest: runs before each request attempt.afterResponse: runs after receiving a response (including non-2xx).onError: runs when request processing throws.- If
beforeRequestorafterResponsethrows, request fails withHookExecutionErrorinresponse.error. - If
onErrorthrows, that error is ignored and the original request error is preserved.
Use withRequestOptions to apply a timeout or cancellation signal to one operation without changing the client defaults.
const controller = new AbortController()
const response = await client
.withRequestOptions({ signal: controller.signal, timeoutMs: 2000 })
.projects.list()Cancelled and timed-out requests return the stable SDK error codes request.aborted and request.timed_out, respectively.
Every SDK method returns an ApiResponse shape:
const response = await client.projects.get('project-name')
if (!response.ok) {
console.error(response.error.code, response.error.message, response.status)
return
}
console.log(response.data)You can also branch on stable error codes:
const response = await client.projects.get('project-name')
if (!response.ok) {
switch (response.error.code) {
case 'resource.project_not_found':
console.log('Project does not exist')
break
case 'auth.unauthorized':
console.log('API key is invalid or missing permissions')
break
default:
console.log(response.error.message)
}
}const { data, error } = await client.environment.get()This method will load the environment and inject the secrets into the process.
const { error } = await client.environment.load()const { data, error } = await client.secrets.list()const { data, error } = await client.secrets.getMetadata('HOST')const { data, error } = await client.secrets.listMetadata()All public SDK types are exported from the package root.
import type { Secret, ListSecretsResponse, GenericApiErrorCode, SecretErrors } from '@stashbase/node-sdk'Bug fixes, documentation improvements, and library improvements are always welcome.
See CONTRIBUTING.md for details.
Stashbase Node SDK is licensed under the MIT License. You can find the license in the LICENSE.txt file.