@stashbase/node-sdk
v0.5.0
Published
Stashbase Node SDK
Readme
Stashbase Node SDK
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...
Table of Contents
Installation
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-sdkUsage
For 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.
Auto client
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, 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"Workspace client
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, hard capped at 10
hooks: {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
}, // optional
})List project
const { data, error } = await client.projects.list()Create environment
const { data, error } = await client.environments({ project: 'project-name' }).create({
name: 'api-dev',
isProduction: false,
})Bind workspace context
const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.list()Get workspace secret metadata
const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.getMetadata('HOST')List workspace secrets metadata
const ctx = client.withContext({
project: 'project-name',
environment: 'dev',
})
const { data, error } = await ctx.secrets.listMetadata()Load environment
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')Environment client
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, hard capped at 10
hooks: {
beforeRequest: ({ method, url }) => console.log('[request]', method, url),
}, // optional
})Timeouts and retries
- Default request timeout is
5000ms. - Maximum request timeout is
10000ms. - Default retry count is
3. - Maximum retry count is
10. timeoutMsis applied per request attempt, not as a total wall-clock budget across all retries.
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'Transport hooks
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.
Error handling
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)
}
}Get environment details
const { data, error } = await client.environment.get()Load environment
This method will load the environment and inject the secrets into the process.
const { error } = await client.environment.load()List secrets
const { data, error } = await client.secrets.list()Get environment secret metadata
const { data, error } = await client.secrets.getMetadata('HOST')List environment secrets metadata
const { data, error } = await client.secrets.listMetadata()Type imports
All public SDK types are exported from the package root.
import type { Secret, ListSecretsResponse, GenericApiErrorCode, SecretErrors } from '@stashbase/node-sdk'Contributing
Bug fixes, documentation improvements, and library improvements are always welcome.
See CONTRIBUTING.md for details.
License
Stashbase Node SDK is licensed under the MIT License. You can find the license in the LICENSE.txt file.
