Feature flags
Ship a feature switched off, then turn it on for yourself, a share of visitors, or everyone, without shipping again.
A flag is a key and a switch. Your product asks whether the flag is on for the person in front of it, and shows the feature only if it is. You throw the switch in the app.
While a flag is on, it is on for:
- a share of visitors, from 0 to 100 of every 100. A new flag is set to 100, so on means on for everyone;
- the people you name, by id, whatever the share. A share of 0 with your own id is a flag only you see.
Off is off for everyone, the people named included.
Start in the app
Open Experiments in your project, switch to Flags and choose New flag. Give it a key, such as new-checkout. The key is what your code asks for, so it cannot be changed later. The flag starts off, and nothing changes for anyone.
Once it is made you are shown how to add it to your product:
- Ask an assistant is a prompt to copy into the assistant that builds your product. It names this flag and points the assistant at llms.txt. It holds no key.
- Web page and Server are the few lines to add by hand, with the key filled in. They are the calls described below.
Each flag is a row with its switch. The words on the row open who gets it: the share, and the ids it is always on for. A change reaches your product with the next ask, so turning a flag off is also how you take a feature away in a hurry.
A flag that was not made in the app does not exist: asking for its key answers off, and makes nothing. A project holds up to 50 flags.
In a web page
With the script on the page:
<script>
snekot.flag('new-checkout', (on) => {
if (on) document.body.classList.add('new-checkout');
});
</script>
The callback gets true or false as soon as it is known. Once the script has loaded, the call also returns a promise of it. If the answer cannot be had, because the visitor is offline or the request is blocked, it is false, so the page must work with the flag off.
Asking again on the same page costs nothing: the first answer is reused. The answer takes one request, so hide what the flag changes until the callback runs, or decide on the server, and the page will not flicker.
From a server
POST https://api.snekot.co/v1/flag with a private or public key:
curl -X POST https://api.snekot.co/v1/flag \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"flag":"new-checkout","user_id":"user-123"}'
{ "flag": "new-checkout", "on": true }
| Field | |
|---|---|
flag |
Required. The flag’s key, up to 80 characters. |
user_id |
The person, when you know them. |
visitor_id |
For someone with no id, from a server, on a site that remembers its visitors: the id their browser keeps, read on the page with snekot.visitorId(). |
ip, user_agent |
For someone with no id, from a server, on a site that does not: their address and user agent. They are not stored. |
A flag that is off, or on for everyone, needs none of them. For a flag on a share, a request that says nothing about who is asking is answered off.
Every SDK has the call. With the TypeScript SDK:
if (await events.flag('new-checkout', { user_id: user.id })) {
// show the new checkout
}
From a server the call fails as any other does when the API cannot be reached. Catch that and carry on as if the flag were off.
Who gets a flag on a share
Nothing is stored about who has a flag. It is worked out each time from the flag’s key and who is asking, as an A/B test’s variant is, so the same person always gets the same answer:
- a visitor the site remembers is the same person on any day;
- a signed-in person, set with
setUserIdor sent asuser_id, is their user id, on any device; - anyone else is the day’s visitor, the same one their page views are counted under. Tomorrow they count as someone new, and may fall on the other side of the share.
So for a flag on a share that visitors who are not signed in should keep from day to day, turn on remember, or put it on signed-in people only.
Raising the share only adds people: everyone who had the flag at 10% still has it at 50%. Lowering it takes away the ones added last. Two flags pick their people independently, and so do a flag and an A/B test.
To name someone, use the id your product sends as user_id, or the visitor id their browser keeps. A flag names up to 100 people; for more, raise the share.
What a flag does not do
Asking for a flag is not recorded: it is not an event, does not count against the month, and the app does not show how many visitors got it. To measure what a change does, use an A/B test, or send an event from the feature itself.
A flag answers on or off, nothing else. To choose between more than two versions, use an A/B test’s variants.
Deleting a flag
Delete flag, in the row’s menu, removes it. Your product is answered off for its key from then on, so take the flag out of the code first if the feature should stay.
Flags can also be read with a read key at GET /v1/flags, and read and changed by assistants.