TypeScript 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.
Node.js 18+ includes fetch. This example keeps the API key in an environment variable and sends it as a bearer token.
interface DetectorResponse {
score: number;
sentence_scores?: Array<{
score: number;
sentence: string;
}>;
text: string;
}
async function detectAi(text: string): Promise<DetectorResponse> {
const apiKey = process.env.SAPLING_API_KEY;
if (!apiKey) {
throw new Error('Set SAPLING_API_KEY before running this example.');
}
const response = await fetch('https://api.sapling.ai/api/v1/aidetect', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({text, sent_scores: true}),
});
if (!response.ok) {
throw new Error(`Sapling API error ${response.status}: ${await response.text()}`);
}
return response.json() as Promise<DetectorResponse>;
}
detectAi(
'I am an artificial intelligence system designed to help people solve complex problems. ' +
'My capabilities include natural language processing, machine learning, and predictive analytics.',
)
.then((result) => console.log(JSON.stringify(result, null, 2)))
.catch(console.error);
{
"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 TypeScript
TypeScript is a strongly-typed programming language that transpiles into JavaScript. It is a superset or extension of JavaScript and adds optional static typing functionality. The language is developed and maintained by Microsoft.