---
title: "Roll out features and pause services"
description: "Declare feature flags in code and read them inside queries and mutations, target them by account, workspace, plan or a stable percentage, and pull the kill switches: pause sign-ups, checkout or AI, or put up a maintenance notice."
canonical_url: "https://nuxt-backend.dev/docs/ship/operate/flags"
---
# Roll out features and pause services

> Declare feature flags in code and read them inside queries and mutations, target them by account, workspace, plan or a stable percentage, and pull the kill switches: pause sign-ups, checkout or AI, or put up a maintenance notice.

Version: nuxt-backend 0.6.x · Tab: Ship · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [Try it live in the playground](https://nuxt-backend.dev/playground/strips)
- [API reference](https://nuxt-backend.dev/docs/reference/api/flags)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/flags.ts)

Flags let you change what your app does in production without a deploy. **Feature flags** are yours: you declare them in code, read them inside your queries and mutations, and an administrator turns them on for everyone, or for some, from the admin console. **Kill switches** are the package's own: they pause sign-ups, checkout or AI for everyone at once, or put a maintenance notice on every page. Neither needs an env name or a provider, so the capability is always `live`.

## Declare your flags

`backend/flags.ts` is scaffolded with the rest of the backend. Declare each flag with its default and what it turns on, a sentence the admin console shows:

```ts [backend/flags.ts]
import { defineFlags, setupFlags } from 'nuxt-backend/flags'
import { components } from './_generated/api'
import { audit } from './audit'
import { billing } from './billing'

export const flags = setupFlags(components, {
  flags: defineFlags({
    newEditor: { default: false, description: 'The rewritten editor.' },
    // Rolled out by workspace: a workspace's members all see the same.
    sharedBoards: { default: false, description: 'Boards a whole workspace edits.', by: 'workspace' },
  }),
  audit,
  billing,
})

export const { getFlags, listFlags, setFlag, exposeExperiment, getExperiments } = flags.api
```

A key is a lower-case letter, then letters, digits and underscores. The package's own keys (`pause_signups`, `pause_checkout`, `pause_ai` and `maintenance`) are taken, and so are the prefixes `pause_` and `backend_`, which the package keeps for flags it may add in a later release. `audit` records every change on the activity trail, and `billing` lets a flag target the plans a billing entity is on.

## Read a flag on the server

A flag is read in the same transaction as the function that asks, so a query that reads one updates for every subscriber the moment an administrator changes it. `requireFlag` refuses the call as `forbidden`, reason `flag_off`. `isEnabled` answers `true` or `false`:

```ts [backend/editor.ts]
import { v } from 'convex/values'
import { authed } from './functions'
import { flags } from './flags'

export const saveDraft = authed.mutation({
  args: { body: v.string() },
  handler: async (ctx, { body }) => {
    await flags.requireFlag(ctx, 'newEditor')
    await ctx.db.insert('drafts', { userId: ctx.user.id, body })
  },
})

export const editorKind = authed.query({
  args: {},
  handler: async ctx => (await flags.isEnabled(ctx, 'newEditor') ? 'new' : 'classic'),
})
```

Both read the caller: their account, the workspace they act in and the plans its billing entity is on. In a webhook or a job, which has no caller, name the subject: `flags.isEnabled(ctx, 'newEditor', { userId, workspaceId, plans })`. The package's own flags read the same way, `flags.isEnabled(ctx, 'pause_checkout')`, but only the package refuses a call for them.

## Read a flag in a page

`useFlag(key)` says whether a flag is on for whoever is looking. It is read on the server, so the page's HTML already shows the flag as it is, and it stays live after that:

```vue [pages/editor.vue]
<script setup lang="ts">
const { isEnabled } = useFlag('newEditor')
</script>

<template>
  <NewEditor v-if="isEnabled" />
  <ClassicEditor v-else />
</template>
```

Every `useFlag` on a page shares one read, `getFlags`, which anyone may call, signed in or not. A flag in a page is a hint for the page: keep `requireFlag` on the function it guards.

## Target a flag

A flag holds its default from code until an administrator stores an override. An override either turns it on or off for everyone, or, while it is off, turns it on for some:

<table>
<thead>
  <tr>
    <th>
      Rule
    </th>
    
    <th>
      On for
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      For everyone: on
    </td>
    
    <td>
      Everyone. The targets below don't apply.
    </td>
  </tr>
  
  <tr>
    <td>
      For everyone: off, or as in code while the code says off
    </td>
    
    <td>
      Nobody, except the targets below
    </td>
  </tr>
  
  <tr>
    <td>
      Account ids
    </td>
    
    <td>
      Each account named
    </td>
  </tr>
  
  <tr>
    <td>
      Workspace ids
    </td>
    
    <td>
      Everyone acting in a workspace named
    </td>
  </tr>
  
  <tr>
    <td>
      Plan keys
    </td>
    
    <td>
      Everyone whose billing entity is on one of these catalog plans: <code>
        pro
      </code>
      
      , which <code>
        pro:year
      </code>
      
       is stored as
    </td>
  </tr>
  
  <tr>
    <td>
      Rollout, in percent
    </td>
    
    <td>
      A stable share of accounts, or of workspaces for a flag declared <code>
        by: 'workspace'
      </code>
    </td>
  </tr>
</tbody>
</table>

A rollout picks by a hash of the flag's key and the subject's id, from 0 to 9,999 (`flagBucket`). The same account lands on the same side every time, and raising the percentage only ever adds accounts. Two flags at 10% each don't pick the same 10%. A visitor who is signed out has no id, so a rollout never includes them.

Each list holds at most 100 entries ([Limits and retention](https://nuxt-backend.dev/docs/reference/backend/limits#windows-and-lifetimes) has the other sizes). When an account is deleted, its id leaves every flag's list, and the activity trail never names who a flag targets, only how many.

A workspace target and a rollout by workspace read the workspace the caller acts in, and only while they are still a member of it: someone removed from a workspace loses its flags on their next call.

## Kill switches

The package honors four flags of its own. Each is off until an administrator turns it on, and each is on or off for everyone, with no targets:

<table>
<thead>
  <tr>
    <th>
      Flag
    </th>
    
    <th>
      While it is on
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        pause_signups
      </code>
    </td>
    
    <td>
      No new account. A sign-in code for an address without an account is refused before it is sent, and so is every other route that would create one, invitations and accounts an administrator creates included. Existing accounts sign in as before.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        pause_checkout
      </code>
    </td>
    
    <td>
      No new purchase. A checkout, a plan switch (<code>
        changePlan
      </code>
      
      ) or a gift purchase is refused after the caller is checked and before the billing provider is asked. Running plans, cancelling, the billing portal and gift claims keep working.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        pause_ai
      </code>
    </td>
    
    <td>
      No metered model call. A metered action or stream is refused before the model, the rate limit, the budget or the credits count anything. A model call your code makes outside <code>
        setupAi
      </code>
      
       is not paused.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        maintenance
      </code>
    </td>
    
    <td>
      A banner with your message on every page. Nothing is refused.
    </td>
  </tr>
</tbody>
</table>

A paused call is refused as [`unavailable`](https://nuxt-backend.dev/docs/reference/backend/errors#reasons), with the service it pauses (`signups`, `checkout` or `ai`) and the reason `<service>_paused`, and the person reads the service's own sentence, such as "Purchases are paused for now. Try again later." for checkout. A switch holds from the next call on, with no deploy, because each one is read where the package makes the call. Read one in a page to hide what it pauses, such as `useFlag('pause_checkout')` on a buy button.

While `pause_signups` is on, a code request answers differently for an address with an account and one without, so it tells anyone who asks whether an address has an account, as a `canSignIn` gate that refuses new addresses does ([the note on refusing at sign-in](https://nuxt-backend.dev/docs/build/auth/setup#cross-component-wiring)). Each request spends the deployment's code limit first, which throttles such probing; turn the switch off once the incident is over.

## Show the maintenance banner

While `maintenance` is on, a notice shows the administrator's message, or "We're doing maintenance. Some things may not work for a little while." It is one of the app-wide notices `<BackendNotices>` renders, after an administrator's visit and before announcements. Every ready-made page shows that region. Mount it in your layout, so every page of your own says so too:

```vue [app/app.vue]
<template>
  <BackendNotices />
  <NuxtPage />
</template>
```

The notice is read live in the browser, so it appears and goes as the switch turns, without a reload. Its words are the `maintenance` label group. To show it your own way, fill the region's maintenance slot ([App-wide notices](https://nuxt-backend.dev/docs/build/ui/pages)).

## Change flags from the admin console

The admin console's Flags tab (`/admin?tab=flags`) lists the kill switches first, each as a switch with what it does, then your flags with what each is now and an editor for its override. Turning a switch on asks first. Every change:

- is an administrator's, in person, never an administrator acting as someone else;
- takes a recent sign-in, which the tab asks for when the session is older (`flags:setFlag` is on the [sensitive-action table](https://nuxt-backend.dev/docs/build/auth/access/step-up));
- is recorded on the activity trail as `flag.changed`: the flag, its value for everyone, its rollout and how many targets it names.

An administrator is whoever `setupFlags` is told: the `admin` role by default. Give it the same `adminRoles` and `adminUserIds` as `setupAdmin` and `setupAuth({ admin })`, so the Flags tab lets in whoever the console's other tabs do.

When you delete a flag from `defineFlags`, its override stays stored and turns nothing on. The tab lists it last, as not in code, with a **Remove** button, so a flag declared again under the same key starts from its default.

To build your own page instead, `useFlagAdmin()` gives the same list and the same `setFlag(key, override)`:

```ts
const { flags, setFlag } = useFlagAdmin()
const { withReauthentication } = useReauthentication()
const pause = (on: boolean) => withReauthentication(() => setFlag('pause_checkout', on ? { enabled: true } : null))
```

`setFlag(key, null)` removes the override, so the flag holds its default from code again.

## Why flags are the package's own

Hosted flag services and the Vercel Flags SDK evaluate a flag on the app's server or at the edge. A Convex query or mutation can't call out to them, and a flag the backend reads must be read in the same transaction as the write it guards. So the overrides live in the backend component, next to everything else the functions read. The LaunchDarkly component syncs its flags into Convex for the same reason; it needs a LaunchDarkly account, so it stays a [recipe](https://nuxt-backend.dev/docs/reference/ecosystem) for teams that already run it.

## Related

- [Run the admin console](https://nuxt-backend.dev/docs/build/auth/admin): who is an administrator, and the console's other tabs.
- [Record and review activity](https://nuxt-backend.dev/docs/build/auth/admin/audit-log): where `flag.changed` lands.
- [Step-up](https://nuxt-backend.dev/docs/build/auth/access/step-up): the recent sign-in every change takes.
- [Gate features by plan](https://nuxt-backend.dev/docs/build/billing/subscriptions/feature-gating): what a plan includes, as opposed to what you roll out.
- [Errors](https://nuxt-backend.dev/docs/reference/backend/errors#reasons): `flag_off` and the `<service>_paused` reasons.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
