Sapling Logo
Grammar API quickstart

Swift / iOS Grammar Checker

Add spelling and grammar suggestions to a Swift / iOS application with Sapling's edits API.

  • HTTPS POST
  • HTTP API
  • API key required
POST Swift / iOS logo Swift / iOS /api/v1/edits

Swift / iOS grammar checker quickstart

Send text to the edits endpoint. Sapling returns suggested replacements with character offsets so your application can review or apply each correction.

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/edits
import Foundation
#if canImport(FoundationNetworking)
  import FoundationNetworking
#endif

let json: [String: Any] = [
    "key": "<API_KEY>",
    "text": "Lets get started!",
    "session_id": "Test Document UUID"
]

let jsonData = try JSONSerialization.data(withJSONObject: json)

let url = URL(string: "https://api.sapling.ai/api/v1/edits")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("\(String(describing: `jsonData`.count))", forHTTPHeaderField: "Content-Length")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    print("URLSession dataTask")
    guard let data = data, error == nil else {
      print("No data")
      return;
    }
    let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
    if let responseJSON = responseJSON as? [String: Any] {
        print(responseJSON)
    }
}
task.resume()
Example response application/json
{
  "edits": [
    {
      "start": 0,
      "end": 4,
      "replacement": "Let's",
      "sentence": "Lets get started!",
      "error_type": "R:OTHER"
    }
  ]
}

Each edit identifies the source range and replacement text. Preserve the original offsets when applying more than one edit.

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.