Sapling Logo
Tone API quickstart

Swift / iOS Tone Checker

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

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

Swift / iOS 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.
Swift / iOS /api/v1/tone
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@main
struct Main {
  static func main() async throws {
    guard let apiKey = ProcessInfo.processInfo.environment["SAPLING_API_KEY"] else {
      throw NSError(
        domain: "Quickstart",
        code: 1,
        userInfo: [NSLocalizedDescriptionKey: "Set SAPLING_API_KEY first."]
      )
    }

    let payload: [String: Any] = [
      "text": "Really stoked about this! Will it be ready by next week?",
    ]

    var request = URLRequest(
      url: URL(string: "https://api.sapling.ai/api/v1/tone")!
    )
    request.httpMethod = "POST"
    request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpBody = try JSONSerialization.data(withJSONObject: payload)

    let (data, response) = try await URLSession.shared.data(for: request)
    guard let httpResponse = response as? HTTPURLResponse,
          (200..<300).contains(httpResponse.statusCode) else {
      throw NSError(domain: "SaplingAPI", code: 1)
    }

    print(String(decoding: data, as: UTF8.self))
  }
}
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 Swift / iOS

Swift is a compiled programming language developed by Apple as a replacement for Objective-C. It is used with Cocoa and Cocoa Touch frameworks for application development for iOS and macOS platforms.