Sapling Logo
Tone API quickstart

Kotlin / Android Tone Checker

Analyze overall and sentence-level tone from a Kotlin / Android application across 28 fine-grained categories.

  • HTTPS POST
  • JSON HTTP API
  • Bearer API key
POST Kotlin / Android logo Kotlin / Android /api/v1/tone

Kotlin / Android tone checker quickstart

Send text to the tone endpoint. The response ranks overall tones and returns a separate probability breakdown for every sentence.

No language-specific SDK is required. The example uses a standard or commonly used HTTP client to call Sapling's JSON API.
Kotlin / Android /api/v1/tone

This example uses the HTTP client included with Java 11 and newer.

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

fun main() {
    val apiKey = System.getenv("SAPLING_API_KEY")
        ?: error("Set SAPLING_API_KEY first.")
    val payload = """
        {
          "text": "Really stoked about this! Will it be ready by next week?"
        }
    """.trimIndent()

    val request = HttpRequest.newBuilder()
        .uri(URI.create("https://api.sapling.ai/api/v1/tone"))
        .header("Authorization", "Bearer $apiKey")
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(payload))
        .build()

    val response = HttpClient.newHttpClient().send(
        request,
        HttpResponse.BodyHandlers.ofString(),
    )

    check(response.statusCode() in 200..299) {
        "Sapling API error ${response.statusCode()}: ${response.body()}"
    }
    println(response.body())
}
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 Kotlin / Android

Kotlin is a programming language that interoperates with Java on the JVM. While it is cross-platform, widespread adoption of Kotlin stems from its first-class support on the Android platform for Android applications.