JavaScript SDK reference
This SDK is not currently supported on Harness Self-Managed Enterprise Edition (on premises).
This topic describes how to use the Harness Feature Flags Javascript SDK for your JavaScript application.
For getting started quickly, you can use our sample code from the JavaScript SDK README. You can also clone and run a sample application from the JavaScript SDK GitHub Repository.
Before You Begin
Make sure you read and understand:
- Feature Flags Overview
- Getting Started with Feature Flags
- Client-Side and Server-Side SDKs
- Communication Strategy Between SDKs and Harness Feature Flags
Version
Latest SDK version can be found on GitHub Release Page
Requirements
To use this SDK, make sure you:
- Create a JavaScript application to use with the SDK, or clone our sample application to use.
- Download the SDK from our GitHub Repository.
- Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode
. - Create a Client SDK key and make a copy of it.
Install the SDK
You can install the SDK using npm, Yarn, or by importing it directly from unpkg.
Install using npm
Run the following command:
npm i @harnessio/ff-javascript-client-sdk
Then, import the SDK using the following command:
import { initialize, Event } from '@harnessio/ff-javascript-client-sdk'
Install using Yarn
Run the following command:
yarn add @harnessio/ff-javascript-client-sdk
Then, import the SDK using the following command:
import { initialize, Event } from '@harnessio/ff-javascript-client-sdk'
Install directly from unpkg
Run the following script:
<script type="module">
import { initialize, Event } from 'https://unpkg.com/@harnessio/ff-javascript-client-sdk/dist/sdk.client.js'
</script>
Make sure you install the latest version of the SDK, which you can view in GitHub and in Version.If you need to support browsers that no longer support ES modules, run the following script instead:
<script src="https://unpkg.com/@harnessio/ff-javascript-client-sdk/dist/sdk.client-iife.js"></script>
<script>
var initialize = HarnessFFSDK.initialize
var Event = HarnessFFSDK.Event
</script>
Initialize the SDK
To initialize the JavaScript SDK, you need to:
- Add your Client SDK key to connect to your Harness Environment.
- Add a target that you want to evaluate against a Feature Flag.
- (Optional) Configure the SDK.
- Complete the initialization with the SDK using the client SDK Key, target, and Configuration parameters you set.
Add your Client SDK Key
To connect to the correct Environment that you set up on the Harness Platform, you need to add the Client SDK Key from that Environment. Input the Client SDK Key into the FeatureFlagSDKKey
parameter.