FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
samwho
samwhofeatureflags
Public
Like
1
featureflags
Home
Code
2
README.md
H
main.tsx
Branches
1
Pull requests
Remixes
1
History
Environment variables
Settings
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in miliseconds.
Sign up now
Code
/
Code
/
Search
main.tsx
https://samwho--834ab98227a511f09edc569c3dd06744.web.val.run
README.md

Feature Flags

This val demonstrates a very simple feature flag implementation. There is a const in the file called FLAGS which stores the feature flags defined, fork this val and modify this value to setup your own flags.

Defining flags

The example FLAGS value is:

const FLAGS: Record<string, Flag> = { "flag1": Flag.rollout(0.5), "flag2": Flag.enabled().block("user_123"), "flag3": Flag.disabled().allow("hello@samwho.dev"), };

This demonstrates:

  • flag1 -- a flag that will be enabled for 50% of users (more on how users are defined later)
  • flag2 -- a flag that is enabled for everyone except user_123
  • flag3 -- a flag that is disabled for everyone except hello@samwho.dev

Endpoints

There are two endpoints:

  • / -- the root endpoint fetches all flags for the given user.
  • /:id -- fetches just one flag value, given by :id, for the given user.

Specifying users

By default, the user is determined by IP address. If you want to be more specific, you can pass in the ?userId query parameter to any endpoint and that will be used instead.

How it works

This val works by hashing the userId and using the resulting value to determine whether a flag should be enabled or disabled. In a 50% rollout, for example, the numeric hash of the userId is taken and divided by the maximum hash value. If the result is less than the rollout percentage, the flag is enabled. This allows for completely stateless feature flags, no database required.

To prevent the same users getting features first all of the time, the flag name is prepended to the userId before hashing.

HTTP
  • main.tsx
    samwho--83…44.web.val.run
Code
README.md
H
main.tsx
Go to top
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Product
FeaturesPricing
Developers
DocsStatusAPI ExamplesNPM Package Examples
Explore
ShowcaseTemplatesNewest ValsTrending ValsNewsletter
Company
AboutBlogCareersBrandhi@val.town
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.