This ASP.NET Core Web API provides a comprehensive REST API for evaluating feature flags using the FeatBit feature flags management platform. The API supports different data types and provides detailed evaluation information for advanced use cases.
- Complete Feature Flag API: REST endpoints for all major data types
- FeatBit .NET SDK Integration: Server-side SDK for feature flag evaluation
- Multiple Data Types: Support for boolean, string, integer, float, double, and JSON feature flags
- User Targeting: Custom user properties and targeting capabilities
- Detailed Evaluation: Get evaluation reasons and variation details
- Dependency Injection: Proper DI setup for
IFbClient
and customIFeatureFlagService
- Configuration Management: Environment-specific FeatBit settings
POST /api/FeatureFlag/boolean
- Evaluate boolean feature flagPOST /api/FeatureFlag/boolean/detail
- Evaluate with detailed informationGET /api/FeatureFlag/enabled/{flagKey}
- Simple check if feature is enabled
POST /api/FeatureFlag/string
- Evaluate string feature flagPOST /api/FeatureFlag/string/detail
- Evaluate with detailed information
POST /api/FeatureFlag/integer
- Evaluate integer feature flagPOST /api/FeatureFlag/integer/detail
- Evaluate with detailed informationPOST /api/FeatureFlag/float
- Evaluate float feature flagPOST /api/FeatureFlag/float/detail
- Evaluate with detailed informationPOST /api/FeatureFlag/double
- Evaluate double feature flagPOST /api/FeatureFlag/double/detail
- Evaluate with detailed information
POST /api/FeatureFlag/json
- Evaluate JSON string feature flagPOST /api/FeatureFlag/json/detail
- Evaluate with detailed information
POST /api/FeatureFlag/all
- Get all feature flags for a specific user
{
"FeatBit": {
"EnvSecret": "your-environment-secret-key-here",
"StreamingUri": "wss://app-eval.featbit.co",
"EventUri": "https://app-eval.featbit.co",
"StartWaitTimeSeconds": 3
}
}
{
"FeatBit": {
"EnvSecret": "your-development-env-secret-key-here",
"StreamingUri": "ws://localhost:5100",
"EventUri": "http://localhost:5100",
"StartWaitTimeSeconds": 3
}
}
-
Get FeatBit Environment Secret:
- Sign up for FeatBit or set up a local instance
- Create an environment and copy the environment secret key
- Update the
EnvSecret
in your configuration files
-
Install Dependencies:
dotnet restore
-
Build the Project:
dotnet build
-
Run the Application:
dotnet run
# Evaluate a boolean feature flag
curl -X POST "https://localhost:5001/api/FeatureFlag/boolean" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-123",
"name": "John Doe",
"customProperties": {
"country": "US",
"subscription": "premium"
},
"flagKey": "new-checkout-flow",
"defaultValue": false
}'
Response:
{
"flagKey": "new-checkout-flow",
"value": true,
"userId": "user-123",
"evaluatedAt": "2024-01-15T10:30:00Z"
}
# Get detailed evaluation information
curl -X POST "https://localhost:5001/api/FeatureFlag/string/detail" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-456",
"flagKey": "welcome-message",
"defaultValue": "Welcome!"
}'
Response:
{
"flagKey": "welcome-message",
"value": "Welcome Premium User!",
"userId": "user-456",
"reason": "RULE_MATCH",
"kind": "RULE",
"variationId": "var-123",
"evaluatedAt": "2024-01-15T10:30:00Z"
}
# Integer feature flag
curl -X POST "https://localhost:5001/api/FeatureFlag/integer" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-789",
"customProperties": {
"subscription": "enterprise"
},
"flagKey": "max-uploads",
"defaultValue": 10
}'
# Float feature flag
curl -X POST "https://localhost:5001/api/FeatureFlag/float" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-999",
"flagKey": "discount-rate",
"defaultValue": 0.0
}'
# JSON feature flag for complex configurations
curl -X POST "https://localhost:5001/api/FeatureFlag/json" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-222",
"flagKey": "feature-config",
"defaultValue": "{\"timeout\":5000,\"retries\":3}"
}'
# Retrieve all feature flags for a user
curl -X POST "https://localhost:5001/api/FeatureFlag/all" \
-H "Content-Type: application/json" \
-d '{
"userKey": "user-333",
"name": "Charlie Brown",
"customProperties": {
"country": "US",
"subscription": "premium",
"role": "admin"
}
}'
# Quick check if a feature is enabled
curl "https://localhost:5001/api/FeatureFlag/enabled/new-checkout-flow?userKey=user-123&defaultValue=false"
All POST endpoints accept request bodies with the following structure:
{
"userKey": "string (required)",
"name": "string (optional)",
"customProperties": {
"key1": "value1",
"key2": "value2"
},
"flagKey": "string (required)"
}
Each data type has its own request model that extends the base:
- BooleanFeatureFlagRequest: Adds
defaultValue: boolean
- StringFeatureFlagRequest: Adds
defaultValue: string
- IntegerFeatureFlagRequest: Adds
defaultValue: number
- FloatFeatureFlagRequest: Adds
defaultValue: number
- DoubleFeatureFlagRequest: Adds
defaultValue: number
- JsonFeatureFlagRequest: Adds
defaultValue: string
- IFeatureFlagService: Comprehensive interface for all feature flag operations
- FeatureFlagService: Implementation using FeatBit SDK with logging and error handling
- FeatureFlagController: RESTful API controller with endpoints for all data types
- FeatBitOptions: Strongly-typed configuration class
- Request Models: Strongly-typed request models for each data type
- Response Models: Generic response models with evaluation metadata
- Program.cs: Service registration and FeatBit configuration
The API supports sophisticated user targeting through:
- User Key: Unique identifier for the user
- User Name: Optional display name
- Custom Properties: Key-value pairs for targeting rules (country, subscription, role, etc.)
Test different targeting scenarios by varying user properties:
# Premium user from US
curl -X POST "https://localhost:5001/api/FeatureFlag/boolean" \
-d '{"userKey":"user-1","customProperties":{"country":"US","subscription":"premium"},"flagKey":"new-feature"}'
# Basic user from UK
curl -X POST "https://localhost:5001/api/FeatureFlag/boolean" \
-d '{"userKey":"user-2","customProperties":{"country":"UK","subscription":"basic"},"flagKey":"new-feature"}'
# Enterprise admin user
curl -X POST "https://localhost:5001/api/FeatureFlag/boolean" \
-d '{"userKey":"user-3","customProperties":{"subscription":"enterprise","role":"admin"},"flagKey":"new-feature"}'
The application includes comprehensive logging for feature flag evaluations, making it easy to debug and monitor feature flag usage in development and production.
The FeatureFlagService
includes proper error handling that:
- Logs errors when feature flag evaluation fails
- Returns default values when errors occur
- Ensures the application continues to function even if FeatBit is unavailable
- Security: Store environment secrets securely (Azure Key Vault, environment variables, etc.)
- Monitoring: Monitor FeatBit connection status and feature flag evaluation metrics
- Performance: The SDK maintains an in-memory cache and WebSocket connection for optimal performance
- Graceful Degradation: Application continues working with default values if FeatBit is unavailable