Skip to main content

JavaScript SDK API Reference

Complete API documentation for the JavaScript/TypeScript SDK.

new Replane<T>(options?)

Creates a new Replane client instance. The client is usable immediately if you provide defaults or a snapshot.

Constructor Options

OptionTypeRequiredDescription
defaultsRecord<string, unknown>NoDefault values to use before connecting
contextRecord<string, unknown>NoDefault context for all override evaluations
snapshotReplaneSnapshot<T>NoRestore from a previous getSnapshot() call
loggerLoggerNoCustom logger (default: console)

replane.connect(options)

Connects to the Replane server and starts receiving real-time updates via SSE. Returns a Promise that resolves when the connection is established.

Connect Options

OptionTypeRequiredDescription
sdkKeystringYesSDK key for authentication
baseUrlstringYesReplane server URL
fetchFntypeof fetchNoCustom fetch implementation
requestTimeoutMsnumberNoSSE request timeout (default: 2000)
connectTimeoutMsnumberNoConnection timeout (default: 5000)
retryDelayMsnumberNoDelay between retries (default: 200)
inactivityTimeoutMsnumberNoSSE inactivity timeout (default: 30000)
agentstringNoAgent identifier for User-Agent header

Example

const replane = new Replane<Configs>({
defaults: {
'feature-flag': false,
'rate-limit': 100
},
context: {
env: 'production',
region: 'us-east'
}
})

await replane.connect({
sdkKey: process.env.REPLANE_SDK_KEY,
baseUrl: 'https://replane.example.com',
requestTimeoutMs: 5000,
connectTimeoutMs: 10000
})

replane.get<K>(name, options?)

Gets a config value. Returns the current value synchronously.

Parameters

ParameterTypeRequiredDescription
namekeyof TYesConfig name
options.contextRecord<string, string | number | boolean | null | undefined>NoContext for override evaluation
options.defaultT[K]NoDefault value to return if config not found (prevents throwing)

Returns

The config value with type T[K], or the default value if provided and config is not found.

Example

// Simple get
const enabled = replane.get('feature-dark-mode') // boolean

// With context
const limit = replane.get('api-rate-limit', {
context: { plan: 'premium' }
}) // number

// With default value - won't throw if config doesn't exist
const timeout = replane.get('timeout-ms', { default: 5000 }) // number

replane.subscribe(name, callback)

Subscribes to a specific config's changes. Returns an unsubscribe function.

const unsubscribe = replane.subscribe('feature-flag', (config) => {
console.log('Feature flag changed:', config.value)
// config.value is typed based on your Configs interface
})

// Later: stop receiving updates
unsubscribe()

replane.disconnect()

Disconnects from the server and cleans up resources. Safe to call multiple times. Call this when shutting down your application.

process.on('SIGTERM', () => {
replane.disconnect()
process.exit(0)
})

replane.getSnapshot()

Returns a serializable snapshot of the current config state. Use for SSR hydration or persisting state.

const snapshot = replane.getSnapshot()

// Later, restore from snapshot
const newClient = new Replane({ snapshot })