Official GrowthBook provider for OpenFeature .NET SDK. Use GrowthBook feature flags with the OpenFeature standard.
$ dotnet add package GrowthBook.OpenFeatureThis is the official GrowthBook provider for the OpenFeature .NET SDK. It allows you to leverage GrowthBook as your feature flag system through the standardized OpenFeature API.
dotnet add package GrowthBook.OpenFeature
using GrowthBook.OpenFeature;
using OpenFeature;
using OpenFeature.Model;
// Initialize the GrowthBook provider
var provider = new GrowthBookProvider(
clientKey: "sdk-abc123",
apiHostUrl: "https://cdn.growthbook.io"
);
// Register the provider with OpenFeature
Api.Instance.SetProvider(provider);
// Create an evaluation context with targeting information
var context = new EvaluationContext(
targetingKey: "user-123",
new Dictionary<string, Value>
{
{ "email", new Value("user@example.com") },
{ "country", new Value("USA") },
{ "premium", new Value(true) }
}
);
// Get the client
var client = Api.Instance.GetClient();
// Evaluate a feature flag
bool isEnabled = await client.GetBooleanValue("new-dashboard", false, context);
// Use the feature flag in your application logic
if (isEnabled)
{
// Show the new dashboard
}
else
{
// Show the old dashboard
}
This provider supports all OpenFeature evaluation types:
// Boolean flags
bool enabled = await client.GetBooleanValue("feature-enabled", false, context);
// String flags
string variant = await client.GetStringValue("button-color", "blue", context);
// Integer flags
int limit = await client.GetIntegerValue("api-request-limit", 100, context);
// Double flags
double price = await client.GetDoubleValue("subscription-price", 9.99, context);
// Object flags
Value config = await client.GetObjectValue("feature-config", new Value(new Dictionary<string, Value>()), context);
If you already have a GrowthBook instance configured:
// Create GrowthBook context
var gbContext = new GrowthBook.Context
{
ApiHost = "https://cdn.growthbook.io",
ClientKey = "sdk-abc123",
Enabled = true,
// Add additional configuration as needed
};
// Create and configure a GrowthBook instance
var growthBook = new GrowthBook.GrowthBook(gbContext);
// Initialize the GrowthBook provider with the existing instance
var provider = new GrowthBookProvider(growthBook);
// Register the provider with OpenFeature
Api.Instance.SetProvider(provider);
You can update the evaluation context at any time:
// Create new context when user logs in
var userContext = new EvaluationContext(
targetingKey: "user-456",
new Dictionary<string, Value>
{
{ "email", new Value("new-user@example.com") },
{ "subscriptionTier", new Value("premium") }
}
);
// Update the client with the new context
client.SetEvaluationContext(userContext);
// All subsequent evaluations will use the new context
var result = await client.GetBooleanValue("premium-feature", false);
The solution consists of:
clientKey is correct and that the flag exists in your GrowthBook projectapiHostUrl is correct and network connectivity is availableEnable verbose logging to see detailed evaluation information:
OpenFeature.Api.Instance.SetLoggerFactory(new ConsoleLoggerFactory(LogLevel.Debug));
Contributions are welcome! See CONTRIBUTING.md for details.
MIT