Python SDK Reference

Updated 3 months ago by Archana Singh

This topic explains how to use the Harness Feature Flags SDK in your Python application. To learn more about using a Feature Flag SDK with a Python application, clone and run a sample application from the Python SDK GitHub repository.

In this topic:

Before You Begin


  1. Create a feature flag in Harness Feature Flags. Feature flags wrap your code and allow you to manage the feature release in a controlled way. See Create a Feature Flag.
  2. Ensure that you have created your SDK Key. See Create an SDK Key.
  3. A Python application to test your feature flag. If you do not have your Python Application, you can download a sample application from the Python SDK GitHub repository.
    1. On the GitHub page, click Code and then clone the sample application. For more information, see Cloning a repository.
    2. Import your project in an IDE such as Visual Studio Code.

Use Feature Flags SDKs with Python Applications

Perform the following steps to use the Harness Feature Flag SDK in your Python application:

Step 1: Install the FF SDK Dependency

The first step is to install the FF SDK as a dependency in your application. Using terminal install the lib as the following:

pip install harness-featureflags

Step 2: 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.

target represents the desired target for which you want the features to be evaluated.

"YOUR_API_KEY" is an authentication key. See Create an SDK Key.

Put the API Key here from your environment
api_key = "YOUR_API_KEY";

cf = CfClient(api_key);

Define you target on which you would like to evaluate
the featureFlag
target = Target(identifier="user1")
target = Target(name="User1")

This initializes the SDK.

Step 3: Evaluate Target for Your Feature Flag

Once you have added the target, evaluate the target for your feature flag. A feature flag is evaluated for a particular target.

Bool Variation

result = cf.bool_variation("sample_boolean_flag", target, False);  
Number Variation

result = cf.number_variation("sample_number_flag", target, 0);  
String Variation

result = cf.string_variation("sample_string_flag", target, "");  

Step 4: Shut Down the SDK

When SDK is not needed, for example, when the app is not running, you can shut down the SDK. This can avoid potential memory leaks.


Step 5: Verify Your Feature Flag

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created.

Toggle the flag on and off to verify if your application is getting updated.

Use Feature Flag Metrics

Metrics API endpoint can be changed as the following:

cf = CfClient(api_key, with_events_url('METRICS_API_EVENTS_URL'));

Otherwise, the default metrics endpoint URL is used.

Public API Methods

The Public API exposes a few methods that you can utilize:

bool_variation(key: str, target: Target, default: bool)

string_variation(key: str, target: Target, default: str)

number_variation(key: str, target: Target, default: float)

json_variation(String key, Target target, default: dict)


Please Provide Feedback