Skip to content

C++ SDK

SDK Download

Prerequisites

Install the C++ REST SDK (cpprestsdk) for your platform:

sudo apt-get install libcpprest-dev
sudo dnf install cpprest-devel
brew install cpprestsdk
vcpkg install cpprestsdk cpprestsdk:x64-windows

Setup

Download the C++ REST SDK archive from SDK Overview, extract it, then build and install it.

cd <PATH_TO_EXTRACTED_CPP_SDK>
mkdir build && cd build
cmake ..
make
sudo make install

Configure the shared environment variables from Getting Started, and point KOLDAN_FILE_PATH to the audio file you want to transcribe.

Upload And Transcribe With uploadAndTranscribe

The example below keeps the request intentionally minimal: upload one file, start transcription with the general model alias, poll until the job finishes, and print either the transcript or the error details.

#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <sstream>

#include "koldan_sdk/ApiClient.h"
#include "koldan_sdk/api/SpeechTranscriptionsApi.h"

using namespace com::dixilang::koldan::sdk::api;
using namespace com::dixilang::koldan::sdk::model;

int main() {
  try {
    auto config = std::make_shared<ApiConfiguration>();
    config->setBaseUrl(utility::conversions::to_string_t(std::string(std::getenv("KOLDAN_BASE_URL"))));
    config->setApiKey(utility::conversions::to_string_t("apiKey"), utility::conversions::to_string_t(std::string(std::getenv("KOLDAN_API_KEY"))));

    auto apiClient = std::make_shared<ApiClient>(config);
    SpeechTranscriptionsApi api(apiClient);

    auto httpContent = std::make_shared<HttpContent>();
    httpContent->setData(std::make_shared<std::ifstream>(std::getenv("KOLDAN_FILE_PATH"), std::ios::binary));
    httpContent->setContentType(utility::conversions::to_string_t("audio/wav"));
    httpContent->setFileName(utility::conversions::to_string_t("audio.wav"));
    httpContent->setName(utility::conversions::to_string_t("file"));
    httpContent->setContentDisposition(utility::conversions::to_string_t("form-data"));

    auto response = api.uploadAndTranscribe(
        utility::conversions::to_string_t("meeting-recording"),
        utility::conversions::to_string_t(R"({"model":"general","punctuation":false,"capitalization":false})"),
        boost::none,
        boost::optional<std::shared_ptr<HttpContent>>(httpContent),
        boost::none,
        boost::none,
        boost::none,
        boost::optional<utility::string_t>(utility::conversions::to_string_t(R"({"enabled":false})")),
        boost::none,
        boost::none,
        boost::none,
        boost::none
    ).get();

    auto job = response->getJob();
    while (true) {
        job = api.getJob(job->getId()).get();
        auto statusStr = utility::conversions::to_utf8string(utility::string_t(*job->getStatus()));
        std::cout << "Job status: " << statusStr << std::endl;

        using eStatus = com::dixilang::koldan::sdk::model::TranscriptionJobStatus::eTranscriptionJobStatus;
        const auto status = job->getStatus()->getValue();
        if (status == eStatus::COMPLETED || status == eStatus::FAILED || status == eStatus::CANCELLED) {
            break;
        }
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }

    using eStatus = com::dixilang::koldan::sdk::model::TranscriptionJobStatus::eTranscriptionJobStatus;
    if (job->getStatus()->getValue() == eStatus::COMPLETED) {
        std::cout << utility::conversions::to_utf8string(job->getResult()->getText()) << std::endl;
    } else {
        std::cout << "Job did not complete successfully." << std::endl;
    }

    return 0;
  } catch (const ApiException& e) {
    std::cerr << "ApiException: code=" << e.error_code().value() << " msg=" << e.what() << std::endl;
    if (e.getContent()) {
        std::ostringstream oss;
        oss << e.getContent()->rdbuf();
        std::cerr << "Response body: " << oss.str() << std::endl;
    }
    return 1;
  } catch (const std::exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
    return 1;
  }
}

Compile and Run

g++ -std=c++14 -o koldan_example main.cpp \
    -lkoldan_sdk -lcpprest -lssl -lcrypto -lboost_system -lboost_thread -lpthread

./koldan_example

Expected Result

A successful run prints Job status: ... updates and then the transcription text from your uploaded audio.

If the job fails, the example prints the error details returned by the API.