Hyphen SDK for Browser / Javascript
npm install @hyphen/browser-sdk




The Hyphen Browser SDK is used as a base library to get evaluations and works for browsers and the base for javascript libraries such as react, vuejs, svelte, etc . For the full featured nodejs sdk use @hyphen/sdk
get() MethodInstall the package using npm, yarn, or pnpm:
``bash`
npm install @hyphen/browser-sdk
For browser usage via CDN:
`html`
`javascript
import { Toggle } from '@hyphen/browser-sdk';
// Initialize the Toggle client
const toggle = new Toggle({
publicApiKey: 'public_your-api-key-here',
applicationId: 'your-app-id',
environment: 'production', // or 'development'
defaultContext: {
targetingKey: 'user-123',
user: {
id: 'user-123',
email: 'user@example.com',
name: 'John Doe'
}
}
});
`
The SDK provides type-safe helper methods for common data types:
`javascript`
const isFeatureEnabled = await toggle.getBoolean('feature-flag', false);
if (isFeatureEnabled) {
console.log('Feature is enabled!');
}
`javascript`
const welcomeMessage = await toggle.getString('welcome-message', 'Hello World');
document.getElementById('welcome').textContent = welcomeMessage;
`javascript`
const maxRetries = await toggle.getNumber('max-retries', 3);
const timeout = await toggle.getNumber('api-timeout', 5000);
`javascript`
const config = await toggle.getObject('app-config', { theme: 'light' });
console.log('App configuration:', config);
Method`javascript`
// Generic method with type parameter
const feature = await toggle.get
const settings = await toggle.get
You can override the default context for specific evaluations:
`javascript
const customContext = {
targetingKey: 'special-user',
user: { id: 'special-user', plan: 'premium' },
customAttributes: { region: 'us-west' }
};
const result = await toggle.getBoolean('premium-feature', false, {
context: customContext
});
`
`html
`
`html
`
- publicApiKey (string): Your Hyphen public API keyapplicationId
- (string): Your application identifierenvironment
- (string, optional): Environment name (default: 'development')defaultContext
- (ToggleContext, optional): Default evaluation contexthorizonUrls
- (string[], optional): Custom Horizon endpoint URLsdefaultTargetKey
- (string, optional): Default targeting key
- getBoolean(toggleKey, defaultValue, options?) - Get boolean togglegetString(toggleKey, defaultValue, options?)
- - Get string toggle getNumber(toggleKey, defaultValue, options?)
- - Get number togglegetObject
- - Get object toggleget
-
We welcome contributions to the Hyphen Node.js SDK! If you have an idea for a new feature, bug fix, or improvement, please follow the Contribution guidelines and our Code of Conduct.