src/config/feature-flags.ts.
Environment Detection
ShipFree provides utilities to detect the current runtime environment:Available Environment Flags
Returns
true when NODE_ENV === 'production'Returns
true when NODE_ENV === 'development'Returns
true when NODE_ENV === 'test'Core Feature Flags
These flags control major application features.Billing
Controls whether billing and subscription features are enforced.Set via environment variable:Implementation:
Email Verification
Controls whether new users must verify their email address.Set via environment variable:Implementation:
Payment Provider Detection
These flags detect which payment providers are configured.Stripe
Returns
true if Stripe credentials are configured.Implementation:STRIPE_SECRET_KEYSTRIPE_WEBHOOK_SECRET
Polar
Returns
true if Polar credentials are configured.Implementation:POLAR_ACCESS_TOKEN
Any Provider
Returns
true if any payment provider is configured.Implementation:Helper Functions
ShipFree provides utility functions for working with boolean environment variables.isTruthy
Converts string or boolean values to boolean.Returns
true for:"true"(case-insensitive)"1"true1
isFalsy
Checks if a value is explicitly false.Returns
true for:"false"(case-insensitive)"0"false
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 tosrc/config/env.ts:
Step 3: Create Feature Flag
Add tosrc/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, useNEXT_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:- Enable feature in production
- Monitor for issues
- If stable, remove flag and make feature permanent
- Clean up flag from all environments
Testing with Feature Flags
Test features in isolation:Migration Guide
When deprecating a feature flag:- Announce deprecation in release notes
- Set default to final state (e.g.,
truefor enabled features) - Remove conditional logic and make feature permanent
- Remove from env.ts and feature-flags.ts
- Update documentation