> For the complete documentation index, see [llms.txt](https://docs.reflag.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/vue-sdk.md).

# Vue SDK (beta)

Feature flags, remote configuration, usage tracking and feedback for Vue 3 applications. Built on the [Browser SDK](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/browser-sdk.md).

This page covers SDK v2.

## Get started <a href="#get-started" id="get-started"></a>

Install the SDK:

```sh
npm install @reflag/vue-sdk
```

Wrap your application in `ReflagProvider` with your **publishable** key and the signed-in user's context:

```vue
<script setup lang="ts">
import { ReflagProvider } from "@reflag/vue-sdk";
</script>

<template>
  <ReflagProvider
    publishable-key="your-publishable-key"
    :context="{
      user: { id: 'alice' },
      company: { id: 'acme', plan: 'enterprise' },
    }"
  >
    <template #loading>Loading flags…</template>
    <App />
  </ReflagProvider>
</template>
```

In Nuxt, wrap the provider in `<ClientOnly>`.

Then read a flag in any component:

```vue
<script setup lang="ts">
import { useFlag, useTrack } from "@reflag/vue-sdk";

const enabled = useFlag("huddles");
const track = useTrack();
</script>

<template>
  <button v-if="enabled" @click="track('Started huddle')">Start huddle</button>
</template>
```

## Read flags <a href="#read-flags" id="read-flags"></a>

`useFlag()` returns a ref that updates when the flag changes. Vue unwraps refs in templates; use `.value` in script.

Pass a flag key to check whether the flag is enabled:

```ts
const enabled = useFlag("huddles"); // ref of true or false
```

To read a configuration value, pass a default as the second argument. The ref holds a value of the same type as the default:

```ts
const model = useFlag("model", "quick"); // ref of string
const settings = useFlag("huddle-settings", { maxParticipants: 10 }); // ref of { maxParticipants: number }
```

While flags are loading, if the flag doesn't exist, or if its value has a different type, the ref holds `false`, or your default if you passed one. Pass `null` as the default to accept any JSON value and narrow it yourself.

The SDK checks only the top-level type of a value. If your application depends on nested fields, validate them.

Reading a flag records a check, even if you don't use the value. Reflag uses checks for launch monitoring.

## Loading states <a href="#loading-states" id="loading-states"></a>

`useIsLoading()` returns a Boolean ref that is `true` while the provider loads flags. You can also put loading content in the provider's `#loading` slot, as in the example above.

## Track usage and feedback <a href="#track-usage-and-feedback" id="track-usage-and-feedback"></a>

```ts
const track = useTrack();
await track("Started huddle", { button: "cta" });

const requestFeedback = useRequestFeedback();
requestFeedback({ flagKey: "huddles", title: "How did it go?" });

const sendFeedback = useSendFeedback();
await sendFeedback({ flagKey: "huddles", score: 5 });
```

Configure your flag's adoption rules to match the event names you send. See the [Browser feedback documentation](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/browser-sdk/feedback.md) to configure feedback prompts and the feedback UI.

## Update context <a href="#update-context" id="update-context"></a>

`useUpdateUser()`, `useUpdateCompany()` and `useUpdateOtherContext()` return functions that merge new attributes into the current context. To replace the whole context, call `useClient().setContext(context)`. Flag values are refetched for the new context.

## Bootstrap from the server <a href="#bootstrap-from-the-server" id="bootstrap-from-the-server"></a>

To render flags immediately, without waiting for a network request, evaluate them on your server with the **Node.js SDK**:

```ts
const flags = serverClient.getFlagsForBootstrap(context);
```

Then pass the whole result to `ReflagBootstrappedProvider`, serialized with your framework's usual mechanism:

```vue
<ReflagBootstrappedProvider :publishable-key="publishableKey" :flags="flags">
  <App />
</ReflagBootstrappedProvider>
```

Leave secrets and server-only attributes out of the context you bootstrap, because the browser can read it. Later refreshes evaluate flags using the browser's context. If values that depend on server-only attributes must not change, set `:enable-live-flag-updates="false"`.

## Let users opt in to flags <a href="#let-users-opt-in-to-flags" id="let-users-opt-in-to-flags"></a>

```ts
const { flags, isLoading } = useOptInFlags();
const setOptIn = useSetOptIn();

await setOptIn("huddles", { optedIn: true });
await setOptIn("huddles", { optedIn: false, scope: "company" });
```

Each entry includes the flag's `key`, `name`, `description` and whether the user or company has opted in. User and company opt-ins are independent. Check `isLoading` before rendering an empty state.

## Toolbar <a href="#toolbar" id="toolbar"></a>

The toolbar shows on `localhost` by default and lets you toggle Boolean flags for yourself. Use the `toolbar` prop to show it elsewhere, for example to internal users in production:

```vue
<ReflagProvider
  publishable-key="your-publishable-key"
  :context="context"
  :toolbar="user.isInternal"
>
  <App />
</ReflagProvider>
```

To override a flag from code, call `useClient().setFlagOverride(key, value)`. Pass `undefined` to remove the override.

## Type safety <a href="#type-safety" id="type-safety"></a>

Optionally declare your flags to get autocompletion for keys and type checks for defaults:

```ts
import "@reflag/vue-sdk";

declare module "@reflag/vue-sdk" {
  interface Flags {
    huddles: boolean;
    "huddle-settings": { maxParticipants: number };
  }
}
```

The [Reflag CLI](https://docs.reflag.com/api/cli) can generate these declarations for you with `--format vue`. Declarations are checked at compile time only.

## Use an existing client <a href="#use-an-existing-client" id="use-an-existing-client"></a>

`useClient()` returns the underlying Browser SDK client. If you already create a Browser SDK client yourself, pass it to `ReflagClientProvider`; your application is then responsible for initializing and stopping it. `useOnEvent(type, handler)` subscribes to client events and unsubscribes when the component unmounts.

## Configuration <a href="#configuration" id="configuration"></a>

The provider accepts the Browser SDK's configuration options, such as `fallbackFlags`, `enableTracking`, `offline`, `feedback` and `logger`, as props. Live updates are on by default in Vue; set `:enable-live-flag-updates="false"` to turn them off. Set `debug` to log to the console.

For Content Security Policy requirements and the full list of options, see the [Browser SDK](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/browser-sdk.md).

Using v1? [Legacy docs](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/legacy-sdks/v1/vue-sdk.md) · [Upgrade guide](https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/migrating-from-v1.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.reflag.com/~/revisions/C4Up1JMcXEiiErcxbgyl/supported-languages/vue-sdk.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
