C++ tone checker quickstart
Send text to the tone endpoint. The response ranks overall tones and returns a separate probability breakdown for every sentence.
C++ has no standard HTTP client, so this example uses libcurl.
#include <curl/curl.h>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <string>
int main() {
const char* key = std::getenv("SAPLING_API_KEY");
if (key == nullptr) {
throw std::runtime_error("Set SAPLING_API_KEY first.");
}
CURL* curl = curl_easy_init();
if (curl == nullptr) {
throw std::runtime_error("Could not initialize libcurl.");
}
std::string authorization = std::string("Authorization: Bearer ") + key;
curl_slist* headers = nullptr;
headers = curl_slist_append(headers, authorization.c_str());
headers = curl_slist_append(headers, "Content-Type: application/json");
const std::string payload =
R"({"text":"Really stoked about this! Will it be ready by next week?"})";
curl_easy_setopt(curl, CURLOPT_URL, "https://api.sapling.ai/api/v1/tone");
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
const CURLcode result = curl_easy_perform(curl);
long status = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if (result != CURLE_OK || status < 200 || status >= 300) {
std::cerr << "Request failed: " << curl_easy_strerror(result)
<< " (HTTP " << status << ")\n";
return 1;
}
return 0;
}
{
"overall": [
[0.9247, "curious", "🤓"],
[0.0455, "excited", "😀"],
[0.0057, "confused", "😕"]
],
"results": [
[[0.9937, "excited", "😀"]],
[[0.5469, "confused", "😕"], [0.3993, "curious", "🤓"]]
],
"sents": [
"Really stoked about this!",
"Will it be ready by next week?"
]
}
Probabilities are returned in descending order as probability, tone, and emoji tuples. Use sentence results when a message contains mixed tones.
About C++
C++ is an object-oriented programming language that provides a superset of features of C. It is a popular choice for large programming projects that require the performance characteristics of C but want code organized as classes. Cross-platform applications developed with the Qt framework typically are written in C++.