Skip to main content

Feature Flags - Quick Start

Maturity: Beta

This module is actively being improved. Breaking changes are unlikely but possible.

Add feature toggles with percentage rollouts in under 5 minutes.

Complete Data Isolation

Primus Feature Flags runs entirely within your application. All flag evaluation happens locally using your configuration.

Providers

The Feature Flags module supports configuration-based providers.

Provider 1: AppSettings (Standard)

Step 1: Install Package

dotnet add package PrimusSaaS.FeatureFlags

Step 2: Configure in Program.cs

using PrimusSaaS.FeatureFlags;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddPrimusFeatureFlags(opts =>
builder.Configuration.GetSection("PrimusFeatureFlags").Bind(opts));

var app = builder.Build();
app.MapControllers();
app.Run();

Step 3: Configure appsettings.json

Add your flags configuration:

{
"PrimusFeatureFlags": {
"Flags": {
"NewDashboard": {
"Enabled": true,
"RolloutPercentage": 50
},
"BetaFeature": {
"Enabled": true,
"EnabledForUsers": ["user-123", "user-456"]
},
"MaintenanceMode": {
"Enabled": false
}
}
}
}
How to Configure Values
  • Enabled: Global switch (true/false)
  • RolloutPercentage: 0-100 integer for gradual rollout
  • EnabledForUsers: Array of specific User IDs allowed access

Step 4: Create Controller

Create Controllers/FeatureFlagController.cs:

using Microsoft.AspNetCore.Mvc;
using PrimusSaaS.FeatureFlags;

namespace YourApp.Controllers;

[ApiController]
[Route("api/[controller]")]
public class FeatureFlagController : ControllerBase
{
private readonly IFeatureFlagService _flags;

public FeatureFlagController(IFeatureFlagService flags)
{
_flags = flags;
}

[HttpGet("status/{flagName}")]
public IActionResult GetStatus(string flagName)
{
var isEnabled = _flags.IsEnabled(flagName);
return Ok(new { feature = flagName, enabled = isEnabled });
}
}

Step 5: Test Endpoints

# Check if NewDashboard is enabled
curl http://localhost:5000/api/featureflag/status/NewDashboard

Expected Response:

{
"feature": "NewDashboard",
"enabled": true
}

Configuration Reference

Feature Flag Options

OptionTypeDefaultDescription
EnabledboolfalseMaster switch for the feature
RolloutPercentageint0Percentage of users to enable for (0-100)
EnabledForUsersstring[][]Specific user IDs to allow list

Examples

Example 1: Conditional Logic

if (_flags.IsEnabled("NewDashboard"))
{
return Ok(GetNewDashboardData());
}
return Ok(GetLegacyDashboardData());

Node.js Quick Start

Add feature flags to a Node.js service with in-memory config.

Step 1: Install Package

npm install @primus-saas/feature-flags

Step 2: Create a FeatureFlagService

import { FeatureFlagService } from '@primus-saas/feature-flags';

const featureFlags = new FeatureFlagService({
flags: {
NewDashboard: { enabled: true, rolloutPercentage: 50 },
BetaFeature: { enabled: false, enabledForGroups: ['beta-testers'] },
},
});

if (featureFlags.isEnabled('NewDashboard')) {
console.log('New dashboard enabled');
}

Troubleshooting

Issue: Flag always false

Solution:

  1. Check if the flag name in appsettings.json matches exactly (case-sensitive).
  2. Ensure Enabled is set to true.