# C++

URL: https://docs.hanzo.ai/docs/sdks/cpp

> A native client over every /v1 service, taken from a tag with CMake. C++17, cpprestsdk, generated from the API's own document.

One typed client over every `/v1` capability, generated from the document
`hanzoai/cloud` emits at the commit the repository's `.spec-lock` names. Nothing
under `include/` or `src/` is hand-written.

> **No registry, so it comes from a tag**
>
> C++ has no package registry, so a consumer pins a tag or a commit rather than a
> version range. Both forms below are supported by `GIT_TAG`.

## Install

Requires C++17, CMake 3.20+, and cpprestsdk with Boost and OpenSSL
(`apt install libcpprest-dev libboost-dev libssl-dev`).

Fetch it as part of your build:

```cmake
include(FetchContent)
FetchContent_Declare(
  hanzo
  GIT_REPOSITORY https://github.com/hanzo-cpp/sdk.git
  GIT_TAG        v8.5.156
)
FetchContent_MakeAvailable(hanzo)

add_executable(app main.cpp)
target_link_libraries(app PRIVATE hanzo::hanzo)
```

Or install it once and resolve it as a package:

```bash
cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local
cmake --build build -j"$(nproc)" --target install
```

```cmake
find_package(hanzo CONFIG REQUIRED)
target_link_libraries(app PRIVATE hanzo::hanzo)
```

## The shape

A configuration, a client, and one class per capability:

```cpp
#include <cstdlib>
#include <iostream>

#include "hanzo/ApiClient.h"
#include "hanzo/ApiConfiguration.h"
#include "hanzo/ApiException.h"
#include "hanzo/api/AskApi.h"

int main() {
    const char* key = std::getenv("HANZO_API_KEY");
    if (key == nullptr) {
        std::cerr << "HANZO_API_KEY is not set" << std::endl;
        return 1;
    }

    auto config = std::make_shared<hanzo::api::ApiConfiguration>();
    config->setBaseUrl(U("https://api.hanzo.ai"));
    config->getDefaultHeaders()[U("Authorization")] =
        U("Bearer ") + utility::conversions::to_string_t(key);

    hanzo::api::AskApi ask(std::make_shared<hanzo::api::ApiClient>(config));

    auto question = std::make_shared<hanzo::model::WebQuestion>();
    question->setQ(U("What are the three laws of thermodynamics?"));

    auto report = ask.researchWeb(question).get();
    std::cout << utility::conversions::to_utf8string(report->getAnswer()) << std::endl;
}
```

Every call returns a `pplx::task`, so `.get()` blocks and `.then()` composes.
Failures arrive as `hanzo::api::ApiException`. The class is the capability and
the method is the operation — the [reference](https://docs.hanzo.ai/docs/openapi) prints the pair per
operation, and [Authentication](https://docs.hanzo.ai/docs/api-keys) covers the key in that header.

## Resources

- Source — [hanzo-cpp/sdk](https://github.com/hanzo-cpp/sdk)
- [Every `/v1` operation →](https://docs.hanzo.ai/docs/openapi)
