TypeScript tone checker quickstart
Send text to the tone endpoint. The response ranks overall tones and returns a separate probability breakdown for every sentence.
Sapling provides an SDK for this stack. The direct HTTP example below remains useful for server-side integrations; see the SDK overview for the higher-level client.
TypeScript
/api/v1/tone
Node.js 18+ includes fetch. This example keeps the API key in an environment variable and sends it as a bearer token.
type ToneScore = [probability: number, tone: string, emoji: string];
interface ToneResponse {
overall: ToneScore[];
results: ToneScore[][];
sents: string[];
}
async function checkTone(text: string): Promise<ToneResponse> {
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/tone', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({text}),
});
if (!response.ok) {
throw new Error(`Sapling API error ${response.status}: ${await response.text()}`);
}
return response.json() as Promise<ToneResponse>;
}
checkTone('Really stoked about this! Will it be ready by next week?')
.then((result) => console.log(JSON.stringify(result, null, 2)))
.catch(console.error);
Example response
application/json
{
"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 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.