Skip to content

featbit/feature-flag-api-based-on-dotnet-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Feature Flag API with FeatBit Integration

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.

Features

  • 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 custom IFeatureFlagService
  • Configuration Management: Environment-specific FeatBit settings

API Endpoints

Boolean Feature Flags

  • POST /api/FeatureFlag/boolean - Evaluate boolean feature flag
  • POST /api/FeatureFlag/boolean/detail - Evaluate with detailed information
  • GET /api/FeatureFlag/enabled/{flagKey} - Simple check if feature is enabled

String Feature Flags

  • POST /api/FeatureFlag/string - Evaluate string feature flag
  • POST /api/FeatureFlag/string/detail - Evaluate with detailed information

Numeric Feature Flags

  • POST /api/FeatureFlag/integer - Evaluate integer feature flag
  • POST /api/FeatureFlag/integer/detail - Evaluate with detailed information
  • POST /api/FeatureFlag/float - Evaluate float feature flag
  • POST /api/FeatureFlag/float/detail - Evaluate with detailed information
  • POST /api/FeatureFlag/double - Evaluate double feature flag
  • POST /api/FeatureFlag/double/detail - Evaluate with detailed information

JSON Feature Flags

  • POST /api/FeatureFlag/json - Evaluate JSON string feature flag
  • POST /api/FeatureFlag/json/detail - Evaluate with detailed information

User-specific Operations

  • POST /api/FeatureFlag/all - Get all feature flags for a specific user

Configuration

appsettings.json

{
  "FeatBit": {
    "EnvSecret": "your-environment-secret-key-here",
    "StreamingUri": "wss://app-eval.featbit.co",
    "EventUri": "https://app-eval.featbit.co", 
    "StartWaitTimeSeconds": 3
  }
}

appsettings.Development.json

{
  "FeatBit": {
    "EnvSecret": "your-development-env-secret-key-here",
    "StreamingUri": "ws://localhost:5100",
    "EventUri": "http://localhost:5100",
    "StartWaitTimeSeconds": 3
  }
}

Setup Instructions

  1. 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
  2. Install Dependencies:

    dotnet restore
  3. Build the Project:

    dotnet build
  4. Run the Application:

    dotnet run

API Usage Examples

Boolean Feature Flag Evaluation

# 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"
}

String Feature Flag with Detail

# 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"
}

Numeric Feature Flags

# 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 Configuration

# 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}"
  }'

Get All Feature Flags

# 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"
    }
  }'

Simple GET Endpoint

# Quick check if a feature is enabled
curl "https://localhost:5001/api/FeatureFlag/enabled/new-checkout-flow?userKey=user-123&defaultValue=false"

Request Models

All POST endpoints accept request bodies with the following structure:

FeatureFlagRequest (Base)

{
  "userKey": "string (required)",
  "name": "string (optional)",
  "customProperties": {
    "key1": "value1",
    "key2": "value2"
  },
  "flagKey": "string (required)"
}

Type-Specific Requests

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

Architecture

Services

  • 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

Key Components

  • 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

User Targeting

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.)

Testing Different Users and Targeting

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"}'

Logging

The application includes comprehensive logging for feature flag evaluations, making it easy to debug and monitor feature flag usage in development and production.

Error Handling

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

Production Considerations

  1. Security: Store environment secrets securely (Azure Key Vault, environment variables, etc.)
  2. Monitoring: Monitor FeatBit connection status and feature flag evaluation metrics
  3. Performance: The SDK maintains an in-memory cache and WebSocket connection for optimal performance
  4. Graceful Degradation: Application continues working with default values if FeatBit is unavailable

About

for getting only one feature flag value of one user

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages