JavaScript tone checker quickstart
Send text to the tone endpoint. The response ranks overall tones and returns a separate probability breakdown for every sentence.
Node.js 18+ includes fetch. Keep the API key in an environment variable rather than browser code.
async function checkTone(text) {
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();
}
checkTone('Really stoked about this! Will it be ready by next week?')
.then((result) => console.log(JSON.stringify(result, null, 2)))
.catch(console.error);
{
"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 JavaScript
JavaScript is a scripting language used in browsers to provide client-side dynamic application behavior. Almost all websites use it. Additionally, JavaScript is also used in server-side applications through environments like Node.js. Cross-platform and desktop applications can be developed using the Electron framework.