Feature Flags

Control feature rollouts with built-in feature flag management. Target by user, organization, or percentage with flags automatically included in JWT claims.

Zero Latency Feature Checks
Feature flags are automatically embedded in JWT token claims, allowing you to check feature access client-side without additional API calls.

Creating Feature Flags

Create Feature Flagbash
curl -X POST https://api.accessiq.io/v1/feature-flags \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "new-dashboard",
    "name": "New Dashboard UI",
    "description": "Redesigned dashboard with improved analytics",
    "type": "boolean",
    "defaultValue": false,
    "environments": ["development", "staging", "production"]
  }'

Targeting Rules

Feature flags can be targeted at multiple levels:

User Targeting

Enable for specific users by email or user ID

Organization Targeting

Enable for entire organizations or specific plans

Percentage Rollout

Gradually roll out to a percentage of users

Custom Rules

Define complex rules based on user attributes

Add Targeting Rulesbash
curl -X PUT https://api.accessiq.io/v1/feature-flags/new-dashboard/rules \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "rules": [
      {
        "type": "user",
        "values": ["user_123", "user_456"],
        "enabled": true
      },
      {
        "type": "organization",
        "values": ["acme-corp", "techstart"],
        "enabled": true
      },
      {
        "type": "plan",
        "values": ["enterprise", "professional"],
        "enabled": true
      },
      {
        "type": "percentage",
        "percentage": 25,
        "enabled": true
      }
    ]
  }'

JWT Claims Integration

When a user authenticates, enabled feature flags are automatically included in their JWT token:

JWT Payload Examplejson
{
  "sub": "user_123",
  "email": "john@acme.com",
  "org_id": "acme-corp",
  "roles": ["admin"],
  "permissions": ["users:read", "users:write"],
  "feature_flags": {
    "new-dashboard": true,
    "beta-analytics": true,
    "dark-mode": true,
    "export-to-pdf": false
  },
  "iat": 1704067200,
  "exp": 1704153600
}

Checking Feature Flags

lib/feature-flags.tstypescript
import { getSession } from '@/lib/auth'

// Check feature flag from JWT
export function hasFeature(
  featureFlags: Record<string, boolean>,
  flag: string
): boolean {
  return featureFlags[flag] === true
}

// React hook for feature flags
export function useFeatureFlag(flag: string): boolean {
  const session = useSession()
  return session?.featureFlags?.[flag] ?? false
}

// Usage in component
function Dashboard() {
  const hasNewDashboard = useFeatureFlag('new-dashboard')

  if (hasNewDashboard) {
    return <NewDashboard />
  }

  return <LegacyDashboard />
}

// Server-side check
export async function GET(request: Request) {
  const session = await getSession()

  if (!hasFeature(session.featureFlags, 'beta-analytics')) {
    return new Response('Feature not available', { status: 403 })
  }

  // Return analytics data
}

Environments

Feature flags can have different states across environments:

FlagDevelopmentStagingProduction
new-dashboardEnabledEnabled25%
beta-analyticsEnabledEnabledDisabled
Best Practices
  • Use descriptive keys like feature-name not flag1
  • Start with 0% rollout and gradually increase
  • Use kill switches for critical features
  • Clean up stale flags after full rollout
  • Document what each flag controls