Package Exports
- @togglely/sdk-core
- @togglely/sdk-core/dist/index.esm.js
- @togglely/sdk-core/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@togglely/sdk-core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@togglely/sdk-core
Core SDK for Togglely - Framework agnostic feature toggles.
No automatic polling - fetch once on init (configurable) and refresh manually or use WebSockets.
Installation
npm install @togglely/sdk-coreUsage
import { TogglelyClient } from '@togglely/sdk-core';
const client = new TogglelyClient({
apiKey: 'your-api-key',
environment: 'production',
baseUrl: 'https://your-togglely-instance.com'
});
// Check if feature is enabled
const isEnabled = await client.isEnabled('new-feature', false);
// Get string value
const message = await client.getString('welcome-message', 'Hello');
// Get number value
const limit = await client.getNumber('max-items', 10);
// Get JSON value
const config = await client.getJSON('app-config', {});
// Set context (useful for multi-tenant and targeting)
client.setContext({
userId: 'user-123',
tenantId: 'customer-abc',
plan: 'premium'
});
// Flags will now be evaluated against this context
const isPremiumFeature = await client.isEnabled('premium-feature', false);
// Listen to events
client.on('ready', () => console.log('Toggles loaded!'));
client.on('offline', () => console.log('Using offline toggles'));Offline Mode
The SDK automatically falls back to environment variables when the service is unavailable.
Environment Variables
TOGGLELY_NEW_FEATURE=true
TOGGLELY_MAX_ITEMS=100Browser Global
<script>
window.__TOGGLELY_TOGGLES = {
'new-feature': true,
'max-items': 100
};
</script>API
TogglelyClient(config)
Creates a new Togglely client.
Config options:
apiKey(string, required): Your API keyenvironment(string, required): Environment namebaseUrl(string, required): Togglely instance URLtimeout(number, default: 5000): Request timeout in msofflineFallback(boolean, default: true): Enable offline modeenvPrefix(string, default: 'TOGGLELY_'): Environment variable prefixautoFetch(boolean, default: true): Fetch toggles on init
Methods
isEnabled(key, defaultValue): Check if a boolean toggle is enabledgetString(key, defaultValue): Get a string toggle valuegetNumber(key, defaultValue): Get a number toggle valuegetJSON(key, defaultValue): Get a JSON toggle valuegetAllToggles(): Get all togglessetContext(context): Set evaluation context (userId, email, etc.)refresh(): Manually refresh togglesdestroy(): Cleanup resourceson(event, handler): Listen to events ('ready', 'update', 'error', 'offline', 'online')