Skip to main content
ShipFree uses environment-based feature flags to enable/disable features and control application behavior. The feature flag system is defined in src/config/feature-flags.ts.

Environment Detection

ShipFree provides utilities to detect the current runtime environment:

Available Environment Flags

boolean
Returns true when NODE_ENV === 'production'
boolean
Returns true when NODE_ENV === 'development'
boolean
Returns true when NODE_ENV === 'test'

Core Feature Flags

These flags control major application features.

Billing

boolean
default:"false"
Controls whether billing and subscription features are enforced.Set via environment variable:
Implementation:
Usage:

Email Verification

boolean
default:"false"
Controls whether new users must verify their email address.Set via environment variable:
Implementation:
Usage:

Payment Provider Detection

These flags detect which payment providers are configured.

Stripe

boolean
Returns true if Stripe credentials are configured.Implementation:
Required environment variables:
  • STRIPE_SECRET_KEY
  • STRIPE_WEBHOOK_SECRET

Polar

boolean
Returns true if Polar credentials are configured.Implementation:
Required environment variables:
  • POLAR_ACCESS_TOKEN

Any Provider

boolean
Returns true if any payment provider is configured.Implementation:
Usage:

Helper Functions

ShipFree provides utility functions for working with boolean environment variables.

isTruthy

function
Converts string or boolean values to boolean.Returns true for:
  • "true" (case-insensitive)
  • "1"
  • true
  • 1
Implementation:
Usage:

isFalsy

function
Checks if a value is explicitly false.Returns true for:
  • "false" (case-insensitive)
  • "0"
  • false
Implementation:
Usage:

Adding Custom Feature Flags

You can extend the feature flag system with your own flags.

Step 1: Add Environment Variable

Add to .env.example and .env:

Step 2: Validate in env.ts

Add to src/config/env.ts:

Step 3: Create Feature Flag

Add to src/config/feature-flags.ts:

Step 4: Use in Your Code

Advanced Patterns

Conditional API Routes

Protect beta API endpoints:

Feature Gating with Multiple Flags

Combine multiple flags for complex gating:

Environment-Specific Behavior

Server vs Client Feature Flags

For client-accessible flags, use NEXT_PUBLIC_ prefix:

Best Practices

1. Explicit Defaults

Always provide explicit default values:

2. Descriptive Names

Use clear, descriptive flag names:

3. Documentation

Document flags in both code and .env.example:

4. Gradual Rollout

Use feature flags for gradual rollouts:

5. Cleanup Old Flags

Remove feature flags after features are stable:
  1. Enable feature in production
  2. Monitor for issues
  3. If stable, remove flag and make feature permanent
  4. Clean up flag from all environments

Testing with Feature Flags

Test features in isolation:

Migration Guide

When deprecating a feature flag:
  1. Announce deprecation in release notes
  2. Set default to final state (e.g., true for enabled features)
  3. Remove conditional logic and make feature permanent
  4. Remove from env.ts and feature-flags.ts
  5. Update documentation
Example: