Use Reflag in your CLI

High-level guide to flagging features in your CLI

If you use API token for auth

First, create an endpoint in your backend for the CLI to call:

POST /get-flags
Authorization: Bearer <APIKEY>

The endpoint will take the API key and use it to look up the associated user.

Then, use the API Reference to get enabled flags for the authenticated user. Return the flags to the CLI:

{
  flags: ['flag1', 'flag2']
}

Finally, check for access with a simple:

/function isFlagEnabled(flag: string) {
    return flags.contains('myflag');
}

If you use API token for auth AND Node.js

First, create an endpoint in your backend for the CLI to call:

POST /get-flags
Authorization: Bearer <APIKEY>

The endpoint will take the API key and use it to look up the associated user.

Use the Node.js SDK get enabled flags for the authenticated user.

import { ReflagClient } from "@reflag/node-sdk";

// configure the client
const boundClient = reflagClient.bindClient({
  user: {
    id: "c1_u1",
    name: "John Doe"
  },
  company: {
    id: "c1",
    name: "Acme, Inc."
  },
});

// get flags
const flags = boundClient.getFlags();

Finally, check for access with a simple:

if(flags['myflag'].isEnabled) {
    //feature access
}

If you use OAuth for auth

Use Node.js SDK or API Reference to get flags for the authenticated user.

In Node, configure the SDK like so:

import { ReflagClient } from "@reflag/node-sdk";

// configure the client
const boundClient = reflagClient.bindClient({
  user: {
    id: "c1_u1",
    name: "John Doe"
  },
  company: {
    id: "c1",
    name: "Acme, Inc."
  },
});

Check access for individual flag:

const { isEnabled } = boundClient.getFlag("myflag");

if (isEnabled) {
    //feature access
}    

Or get all flags:

const flags = boundClient.getFlags();

Last updated

Was this helpful?