C++ AI detector quickstart
Send at least 150 characters for a more reliable signal. The response score ranges from 0 (more likely human-written) to 1 (more likely AI-generated); use it as evidence, not proof.
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":"I am an artificial intelligence system designed to help people solve complex problems. My capabilities include natural language processing, machine learning, and predictive analytics.","sent_scores":true})";
curl_easy_setopt(curl, CURLOPT_URL, "https://api.sapling.ai/api/v1/aidetect");
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;
}
{
"score": 0.9989,
"sentence_scores": [
{
"score": 0.9764,
"sentence": "I am an artificial intelligence system designed to help people solve complex problems."
},
{
"score": 0.9812,
"sentence": "My capabilities include natural language processing, machine learning, and predictive analytics."
}
],
"text": "I am an artificial intelligence system designed to help people solve complex problems. My capabilities include natural language processing, machine learning, and predictive analytics."
}
The overall score and sentence scores use different methods and may not match exactly. Choose thresholds based on the false-positive and false-negative costs of your use case.
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++.