Get started with an SDK
This guide shows you how to create a Feature Flag in Harness and use the Feature Flags Java SDK in your application.
You'll learn how to:
- Create a project in Harness
- Create an environment in Harness
- Create a Feature Flag in Harness
- Add an SDK Key for your environment
- Use Harness Feature Flags SDKs with Java Applications. Your flag is automatically available for use in all environments of your project. However, you need to install the Harness SDK to all environments you wish to initiate your flag in.
Create a project
As a first step, you need to create a project in Harness. To do this:
- On the Harness Platform, click Feature Flags and click Create Project.
- In Projects, click Project.
- In About the Project, in Name, enter the name for your project. You will create your Feature Flag for this project.
- (Optional) Select color for your project.
- Select the Organization from the list. If you need to create a new Organization, go to Create a Harness Organization and Projects and Organizations.
- Click Save and Continue.
- (Optional) In Invite Collaborators, in Invite People to Collaborate, add members to collaborate with you on the project.
- (Optional) Assign a role to the collaborators and click Add. You can select Project Admin, Project Member, or Project Viewer. For more information on permissions, go to Permissions Reference.
- Once you're done adding contributors and adding a role, click Save and Continue.
- Click GO TO FEATURE FLAGS.
Create an environment
Once you've created a project, you need to create an environment. An environment is the representation of your production or non-production infrastructure. You can configure the Target and Rules of your flag in the environment.
In Feature Flags, select the project that you created. For example, FFDOcs.
In your project, in Environments, click Create an Environment.
Enter a name for your environment.
Select the environment type and click Create. You can select Production or Non-Production.
Your environment is created. You can find it listed in Environments.
Create an SDK Key
Now you have an environment ready, you need to create an SDK Key for that environment. You need an SDK key to authorize the connection between your application and one of the Feature Flag SDKs.
Copy the SDK Key when you create it as it is available only after creation, once you leave the page, the key is redacted.
You must create an SDK Key that matches the type of SDK you want to use. That is, you must create a Client SDK Key if you're using a Client SDK or you must create a Server SDK Key if you're using a Server SDK. For more information about the types of SDKs available, go to Choose a Client-side or Server-side SDK.
To create an SDK Key:
Click on the Environment that you created in the previous step.
In Environments, in Settings, click Add Key.
In Create SDK Key, in Name, enter a name for your SDK key.
In Key Type, select the Key Type. For this quickstart we'll use the Server Key type.note
You can add multiple keys for the same environment if your organization needs to rotate them.
The SDK Key is created. Copy it and store it securely.
Create a boolean Flag
Harness Feature Flags support boolean and multivariate flag types. This quickstart uses a boolean Flag, which has only two variations,
To create a boolean Flag:
In Feature Flags, click Flag.
Select the Boolean type.
In About the Flag, in Name, enter a name for your boolean flag and click Next.
When you name a Feature Flag, Harness automatically generates its unique identifier based on the name you give it. You can edit the identifier when you are creating the Flag, but not after it is saved. If you rename the feature flag, the identifier remains the same. For more information, go to Entity Identifier Reference.
If you need to make the flag a permanent Flag, select the checkbox **This is a permanent flag**.
In Variation settings, complete the following:
- Select the flag type: In Flag Type, select Boolean.
- Define the variation: In boolean, you can have only two variations
- Enter a name for your variations. For example,
- Enter a name for your variations. For example,
- Define rules for the flag: Defines which variation you will go to by default when the flag is ON or OFF. Once you save the variations, a flag is created for the environment with these variations set as ON and OFF.
- If the flag is
ON, serve the variation
- If the flag is
OFF, serve the variation
Click Save and Close. The Feature Flag is created. By default, the flag is set to OFF.
Once you have created your flag type, optionally you can manage targets and modify the variations, rules, and add prerequisites of your flag.
Use Harness Feature Flags SDKs with Java applications
In summary, here is how the server-side SDK works:
The client authenticates with a server using an API Key.
The configuration is fetched and cached in the memory.
- One call to fetch all Feature Flag configurations for the environment.
- One call to fetch all Target Group configurations for the environment.
The SDK maintains all the Feature Flag details locally in its cache and evaluates Flags based on it. The SDKs use streaming and polling strategy to keep the local cache in sync with the flag configurations.
Streaming Mode Polling Mode In the case of streaming mode, a connection is open to the stream endpoint. Config is polled periodically (default 60 seconds), and the cache is updated. Anytime the flag configuration updates, a stream event (indicating which Flag/Target Group changed) is pushed to the application on this connection. SDK fetches the latest flag configuration for the Flag/Target group specified in the event and updates the cache.
All the Evaluations are run against the cached data.
Set up your application
You need a Java application to test your Feature Flag. If you do not have your Java Application, you can download a sample application from the Java SDK GitHub repository.
On the Java SDK GitHub page, click Code and then clone the sample application. For more information, go to Cloning a repository.
Install the FF SDK as a dependency in your application using your application's dependency manager. You can use Maven, Gradle, SBT, etc. Refer to the Harness Feature Flag Java Server SDK to identify the latest version for your build automation tool.
This quickstart uses Maven. Add the following Maven dependency in your
If you are using the Harness Java sample application from the Java SDK GitHub repository, then do not add the Maven dependency in the
pom.xmlfile. The Maven dependencies are added in the Harness Java sample application.
Authorize your Application to Connect to the FF Client. After installing the SDK, enter the SDK keys that you created for your environment. The SDK keys authorize your application to connect to the FF client.
CfClient cfClient = new CfClient(apiKey, Config.builder().build());
Add a Target. Targets are used to control which user sees the variation of a Feature Flag. You can add Targets and define their attributes in your code directly as well. The Targets added in the application code are discovered automatically and populated in the Harness UI.
A Target can be any attribute that can be uniquely identified with an entity.
A Target is identified by a name and an identifier.
Enter a name that will identify this target.
Enter a unique identifier for your target. For example, an application, systems, services, machines, resources uniquely identified by an IP address, email ID, user ID, etc.
For more information about Targets, go to Target Management.
Add a Target
Target target = Target.builder().name("User1").identifier("firstname.lastname@example.org").build();
Add a Target with attributes
Attributes are the properties of your Target and used to define rules for your Feature Flags and Target Groups.
Target target = Target.builder()
.attributes(new HashMap<String, Object>())
Evaluate a Target for your Feature Flag. Once you have added the Target, evaluate it for your Feature Flag.
boolean result = cfClient.boolVariation(<feature_identifier>, target, <default_result>);
<feature_identifier> is your Feature Flag identifier. When you name a Feature Flag, Harness automatically generates its identifier. For more information, go to Entity Identifier Reference and Create a flag type.
Sample code for a boolean Flag
boolean result = cfClient.boolVariation("toggle", target, false);
Test your Flag
Verify flag configuration updates in your code. SDK maintains all the Feature Flag details locally in its cache and evaluates Flags based on it. The SDKs use streaming and a polling strategy to keep the local cache in sync with the flag configurations.