Node.js SDK
Node.js, JavaScript/TypeScript client for Bucket.co.
Bucket supports feature toggling, tracking feature usage, collecting feedback on features, and remotely configuring features.
Installation
Install using yarn
or npm
with:
yarn add -s @bucketco/node-sdk
ornpm install -s @bucketco/node-sdk
.
Other supported languages/frameworks are in the Supported languages documentation pages.
You can also use the HTTP API directly
Basic usage
To get started you need to obtain your secret key from the environment settings in Bucket.
Secret keys are meant for use in server side SDKs only. Secret keys offer the users the ability to obtain information that is often sensitive and thus should not be used in client-side applications.
Bucket will load settings through the various environment variables automatically (see Configuring below).
Find the Bucket secret key for your development environment under environment settings in Bucket.
Set
BUCKET_SECRET_KEY
in your.env
fileCreate a
bucket.ts
file containing the following:
Once the client is initialized, you can obtain features along with the isEnabled
status to indicate whether the feature is targeted for this user/company:
If user.id
or company.id
is not given, the whole user
or company
object is ignored.
You can also use the getFeatures()
method which returns a map of all features:
High performance feature targeting
The SDK contacts the Bucket servers when you call initialize()
and downloads the features with their targeting rules.
These rules are then matched against the user/company information you provide
to getFeatures()
(or through bindClient(..).getFeatures()
). That means thegetFeatures()
call does not need to contact the Bucket servers onceinitialize()
has completed. BucketClient
will continue to periodically
download the targeting rules from the Bucket servers in the background.
Batch Operations
The SDK automatically batches operations like user/company updates and feature tracking events to minimize API calls. The batch buffer is configurable through the client options:
You can manually flush the batch buffer at any time:
It's recommended to call flush()
before your application shuts down to ensure all events are sent.
Rate Limiting
The SDK includes automatic rate limiting for feature events to prevent overwhelming the API. Rate limiting is applied per unique combination of feature key and context. The rate limiter window size is configurable:
Caching
Feature definitions are automatically cached and refreshed in the background. The cache behavior is configurable:
Error Handling
The SDK is designed to fail gracefully and never throw exceptions to the caller. Instead, it logs errors and provides fallback behavior:
Feature Evaluation Failures:
Network Errors:
Missing Context:
Offline Mode:
The SDK logs all errors with appropriate severity levels. You can customize logging by providing your own logger:
Remote config (beta)
Remote config is a dynamic and flexible approach to configuring feature behavior outside of your app – without needing to re-deploy it.
Similar to isEnabled
, each feature has a config
property. This configuration is managed from within Bucket.
It is managed similar to the way access to features is managed, but instead of the binary isEnabled
you can have
multiple configuration values which are given to different user/companies.
key
is mandatory for a config, but if a feature has no config or no config value was matched against the context, the key
will be undefined
. Make sure to check against this case when trying to use the configuration in your application. payload
is an optional JSON value for arbitrary configuration needs.
Just as isEnabled
, accessing config
on the object returned by getFeatures
does not automatically
generate a check
event, contrary to the config
property on the object returned by getFeature
.
Configuring
The Bucket Node.js
SDK can be configured through environment variables,
a configuration file on disk or by passing options to the BucketClient
constructor. By default, the SDK searches for bucketConfig.json
in the
current working directory.
secretKey
string
The secret key used for authentication with Bucket's servers.
BUCKET_SECRET_KEY
logLevel
string
The log level for the SDK (e.g., "DEBUG"
, "INFO"
, "WARN"
, "ERROR"
). Default: INFO
BUCKET_LOG_LEVEL
offline
boolean
Operate in offline mode. Default: false
, except in tests it will default to true
based off of the TEST
env. var.
BUCKET_OFFLINE
apiBaseUrl
string
The base API URL for the Bucket servers.
BUCKET_API_BASE_URL
featureOverrides
Record<string, boolean>
An object specifying feature overrides for testing or local development. See example/app.test.ts for how to use featureOverrides
in tests.
BUCKET_FEATURES_ENABLED, BUCKET_FEATURES_DISABLED
configFile
string
Load this config file from disk. Default: bucketConfig.json
BUCKET_CONFIG_FILE
bucketConfig.json
example:
When using a bucketConfig.json
for local development, make sure you add it to your.gitignore
file. You can also set these options directly in the BucketClient
constructor. The precedence for configuration options is as follows, listed in the
order of importance:
Options passed along to the constructor directly,
Environment variable,
The config file.
Type safe feature flags
To get type checked feature flags, install the Bucket CLI:
then generate the types:
This will generate a bucket.d.ts
containing all your features.
Any feature look ups will now be checked against the features that exist in Bucket.
Here's an example of a failed type check:

This is an example of a failed config payload check:

Feature Overrides
Feature overrides allow you to override feature flags and their configurations locally. This is particularly useful for development and testing. You can specify overrides in three ways:
Through environment variables:
Through
bucketConfig.json
:
Programmatically through the client options:
Remote Feature Evaluation
In addition to local feature evaluation, Bucket supports remote evaluation using stored context. This is useful when you want to evaluate features using user/company attributes that were previously sent to Bucket:
Remote evaluation is particularly useful when:
You want to use the most up-to-date user/company attributes stored in Bucket
You don't want to pass all context attributes with every evaluation
You need to ensure consistent feature evaluation across different services
Using with Express
A popular way to integrate the Bucket Node.js SDK is through an express middleware.
See example/app.ts for a full example.
Remote flag evaluation with stored context
If you don't want to provide context each time when evaluating feature flags but
rather you would like to utilize the attributes you sent to Bucket previously
(by calling updateCompany
and updateUser
) you can do so by calling getFeaturesRemote
(or getFeatureRemote
for a specific feature) with providing just userId
and companyId
.
These methods will call Bucket's servers and feature flags will be evaluated remotely
using the stored attributes.
User and company attribute updates are processed asynchronously, so there might be a small delay between when attributes are updated and when they are available for evaluation.
Opting out of tracking
There are use cases in which you not want to be sending user
, company
andtrack
events to Bucket.co. These are usually cases where you could be impersonating
another user in the system and do not want to interfere with the data being
collected by Bucket.
To disable tracking, bind the client using bindClient()
as follows:
Another way way to disable tracking without employing a bound client is to call getFeature()
or getFeatures()
by supplying enableTracking: false
in the arguments passed to
these functions.
Note, however, that calling track()
, updateCompany()
or updateUser()
in the BucketClient
will still send tracking data. As such, it is always recommended to use bindClient()
when using this SDK.
Flushing
It is highly recommended that users of this SDK manually call flush()
method on process shutdown. The SDK employs a batching technique to minimize
the number of calls that are sent to Bucket's servers. During process shutdown,
some messages could be waiting to be sent, and thus, would be discarded if the
buffer is not flushed.
By default, the SDK automatically subscribes to process exit signals and attempts to flush
any pending events. This behavior is controlled by the flushOnExit
option in the client configuration:
If you are creating multiple client instances in your application, it's recommended to disable flushOnExit
to avoid potential conflicts during process shutdown. In such cases, you should implement your own flush handling.
When you bind a client to a user/company, this data is matched against the targeting rules. To get accurate targeting, you must ensure that the user/company information provided is sufficient to match against the targeting rules you've created. The user/company data is automatically transferred to Bucket. This ensures that you'll have up-to-date information about companies and users and accurate targeting information available in Bucket at all time.
Tracking custom events and setting custom attributes
Tracking allows events and updating user/company attributes in Bucket. For example, if a customer changes their plan, you'll want Bucket to know about it, in order to continue to provide up-do-date targeting information in the Bucket interface.
The following example shows how to register a new user, associate it with a company and finally update the plan they are on.
It's also possible to achieve the same through a bound client in the following manner:
Some attributes are used by Bucket to improve the UI, and are recommended to provide for easier navigation:
name
-- display name foruser
/company
,email
-- the email of the user,avatar
-- the URL foruser
/company
avatar image.
Attributes cannot be nested (multiple levels) and must be either strings, integers or booleans.
Managing Last seen
Last seen
By default updateUser
/updateCompany
calls automatically update the given
user/company Last seen
property on Bucket servers.
You can control if Last seen
should be updated when the events are sent by settingmeta.active = false
. This is often useful if you
have a background job that goes through a set of companies just to update their
attributes but not their activity.
Example:
bindClient()
updates attributes on the Bucket servers but does not automatically
update Last seen
.
Zero PII
The Bucket SDK doesn't collect any metadata and HTTP IP addresses are not being stored. For tracking individual users, we recommend using something like database ID as userId, as it's unique and doesn't include any PII (personal identifiable information). If, however, you're using e.g. email address as userId, but prefer not to send any PII to Bucket, you can hash the sensitive data before sending it to Bucket:
Typescript
Types are bundled together with the library and exposed automatically when importing through a package manager.
License
MIT License Copyright (c) 2025 Bucket ApS
Last updated
Was this helpful?