# flagbearer for C++

The C++ SDK for Flagbearer. Requires C++17, CMake 3.16 or later, and libcurl
7.68 or later, which macOS and most Linux distributions already include.

## Install

Add it to your CMake build:

```cmake
include(FetchContent)
FetchContent_Declare(flagbearer
  GIT_REPOSITORY https://github.com/flagbearer-dev/sdks.git
  GIT_TAG cpp-v0.1.0
  SOURCE_SUBDIR cpp)
FetchContent_MakeAvailable(flagbearer)

target_link_libraries(your_app PRIVATE flagbearer::flagbearer)
```

An installed copy works with `find_package(flagbearer 0.1 REQUIRED)` too.

```cpp
#include <flagbearer/flagbearer.hpp>
```

## Keys

Use a server key, which starts with `fbs_`. Server keys are secret: read them
from the environment, such as `FLAGBEARER_SERVER_KEY`, and never put them in
browser code or in the repository. Browsers use client keys, which start with
`fbc_`, through [`@flagbearer/web`](/sdks/web).

## Sync at startup

Create one client per process, when the app starts, and wait for it to sync
before accepting requests:

```cpp
const char* key = std::getenv("FLAGBEARER_SERVER_KEY");
if (!key) throw std::runtime_error("Set FLAGBEARER_SERVER_KEY");
auto flags = flagbearer::Client::create(key);

try {
  flags->sync(std::chrono::seconds(10));
} catch (const flagbearer::Error& error) {
  // Keep starting: checks use their defaults, and the SDK retries in the background.
  std::fprintf(stderr, "Initial Flagbearer sync failed: %s\n", error.what());
}
```

## Check per request

Pass each request's user explicitly, and a default:

```cpp
flagbearer::User user{request.user_id(), request.user_email()};
bool advanced = flags->is_enabled("advanced-search", user, false);
```

`User` holds a `key` and an optional `email`. Include the email to turn flags
on for testers by email. The default is used
until the client has synced, and when the flag doesn't exist.

## Shut down

Keep the client for the life of the process, not per request. Close it on
shutdown. Destroying the client closes it too:

```cpp
flags->close();
```

Short-lived processes should call `flags->flush()` before exiting.
