Project: GO Feature Flag (Observability and Analysis / Feature Flagging)
File: using-openfeature.md

sidebar_position: 20 description: Deploy the relay proxy and use the OpenFeature SDKs :::note OpenFeature provides a shared, standardized feature flagging client - an SDK - which can be plugged into various 3rd-party feature flagging providers. Whether you're using an open-source system or a commercial product, whether it's self-hosted or cloud-hosted, OpenFeature provides a consistent, unified API for developers to use feature flagging in their applications. ::: GO Feature Flag believe in OpenSource and standardization, this is the reason why we decided not implementing any custom SDK and rely only on Open Feature. To be compatible with Open Feature, GO Feature Flag is providing a lightweight self-hosted API server (called ) that is using the GO Feature Flag module internally. When the relay proxy is running in your infrastructure, you can use the Open Feature SDKs in combination with GO Feature Flag providers to evaluate your flags. This schema is an overview on how Open Feature is working, you can have more information about all the concepts in the . Create a new `YAML` file containing your first flag configuration. ```yaml title="flag-config.yaml" test-flag: variations: my-new-feature: true my-old-feature: false defaultRule: percentage: my-new-feature: 20 my-old-feature: 80 ``` This flag split the usage of this flag, 20% will use the variation `my-new-feature` and 80% the variation `my-old-feature`. Create a new `YAML` file containing the configuration of your relay proxy. ```yaml title="goff-proxy.yaml" listen: 1031 pollingInterval: 1000 startWithRetrieverError: false retriever: kind: file path: /goff/flag-config.yaml exporter: kind: log ``` And we will run the relay proxy locally to make the API available. The default port will be `1031`. ```shell docker run \ -p 1031:1031 \ -v $(pwd)/flag-config.yaml:/goff/flag-config.yaml \ -v $(pwd)/goff-proxy.yaml:/goff/goff-proxy.yaml \ thomaspoignant/go-feature-flag-relay-proxy:latest ``` If you don't want to use docker to install the **relay proxy** you can follow the . In this example we are using the javascript SDK, but it is still relevant for all the languages. ```shell npm i @openfeature/js-sdk @openfeature/go-feature-flag-provider ``` In your app initialization your have to create a client using the Open Feature SDK and initialize it. ```javascript const {OpenFeature} = require("@openfeature/js-sdk"); const {GoFeatureFlagProvider} = require("@openfeature/go-feature-flag-provider"); // init Open Feature SDK with GO Feature Flag provider const goFeatureFlagProvider = new GoFeatureFlagProvider({ endpoint: 'http://localhost:1031/' // DNS of your instance of relay proxy }); OpenFeature.setProvider(goFeatureFlagProvider); const featureFlagClient = OpenFeature.getClient('my-app') ``` Now you can evaluate your flags anywhere in your code using this client. ```javascript // Context of your flag evaluation. // With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user. const userContext = { targetingKey: '1d1b9238-2591-4a47-94cf-d2bc080892f1', // user unique identifier (mandatory) firstname: 'john', lastname: 'doe', email: 'john.doe@gofeatureflag.org', admin: true, // this field is used in the targeting rule of the flag "flag-only-for-admin" // ... }; const adminFlag = await featureFlagClient.getBooleanValue('flag-only-for-admin', false, userContext); if (adminFlag) { // flag "flag-only-for-admin" is true for the user console.log("new feature"); } else { // flag "flag-only-for-admin" is false for the user } ```