Sapling Logo
Tone API quickstart

Go Tone Checker

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

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

Go 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.
Go /api/v1/tone
package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "io"
  "net/http"
  "os"
)

func main() {
  apiKey := os.Getenv("SAPLING_API_KEY")
  if apiKey == "" {
    panic("set SAPLING_API_KEY first")
  }

  payload, err := json.Marshal(map[string]string{
    "text": "Really stoked about this! Will it be ready by next week?",
  })
  if err != nil {
    panic(err)
  }

  req, err := http.NewRequest(
    http.MethodPost,
    "https://api.sapling.ai/api/v1/tone",
    bytes.NewReader(payload),
  )
  if err != nil {
    panic(err)
  }
  req.Header.Set("Authorization", "Bearer "+apiKey)
  req.Header.Set("Content-Type", "application/json")

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  body, err := io.ReadAll(resp.Body)
  if err != nil {
    panic(err)
  }
  if resp.StatusCode < 200 || resp.StatusCode >= 300 {
    panic(fmt.Sprintf("Sapling API error %s: %s", resp.Status, body))
  }

  fmt.Println(string(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 Go

Go is a programming language designed at Google, to have performance similarity to C but with easier readability, memory safety, garbage collection and easier concurrency.