# Sapling JS

Node.js client for the [Sapling.ai API](https://sapling.ai/api).

Try out a [grammar check demo](https://sapling.ai/grammar-check).
Compare against grammar checking tools and APIs like Grammarly (Grammerly), LanguageTool, ProWritingAid and Ginger.

## Functionality

Sapling is an AI messaging assistant.
The API currently offers spelling and grammar checking endpoints.

Benefits include:
- **60% more grammar corrections**: Compared to [other systems](https://sapling.ai/comparison/api) at similar accuracy using state-of-the-art generative AI models.
- **Low Latency**: Achieve the same real-time performance that users of Sapling's own interface experience.
- **Over 20 error types**: Error categories such as preposition, noun form, and verb tense, including both high-level and fine-grained error information.
- **Custom Models**: Get corrections optimized for your content type, from academic writing, to medical notes, to user-generated reviews.
- **Enterprise Security**: Contact us for our no data retention policies, [self-hosted/on-premises](https://sapling.ai/onprem) deployment options, and other [cybersecurity policies and procedures](https://sapling.ai/security).
- **Rich Text Editor Support**: TinyMCE, CKEditor, QuillJS, Trix, ProseMirror, WordPress, Draft.js, Froala, Lexical and others. Sapling's [HTML SDK](https://sapling.ai/docs/sdk/HTML/quickstart) can also be directly imported into HTML pages as JavaScript.

### Node.js

Sapling can be run in a Node.js server or script environment.

See [this folder](https://github.com/saplingai/sapling-samples/tree/master/javascript/script) for an example of grammar checking without UI.

### React

Sapling can be imported directly into a React project.

See [this React App](https://github.com/saplingai/sapling-samples/tree/master/javascript/react-app) for a React demo app.

### Angular

For Angular support, refer to [this Angular App](https://github.com/saplingai/sapling-samples/tree/master/javascript/angular-app) for an Angular demo app.

### TypeScript

For TypeScript declarations and how Sapling can be imported directly into a TypeScript project, see [this TypeScript App](https://github.com/saplingai/sapling-samples/tree/master/javascript/angular-app) for a demo app.

## Installation

- `npm install @saplingai/sapling-js`
- Create a key by following [these instructions](https://sapling.ai/docs/api/api-access).

## Backend Quickstart

```javascript
import { Client } from "@saplingai/sapling-js/client";

const apiKey = '<YOUR_API_KEY>';
const client = new Client(apiKey);
client
  .edits('I have a apple!')
  .then(function (response) {
    console.log(response.data);
  })
```

You should see something like the following output:

```javascript
{
  edits: [
    {
      end: 8,
      error_type: "R:DET:ART",
      general_error_type: "Grammar",
      id: "740071c9-8ea3-583d-a86b-7ef80e5fc91e",
      replacement: "an",
      sentence: "I have a apple",
      sentence_start: 0,
      start: 7
    }
  ]
}
```

## Frontend Quickstart

The SDK provides a way to automatically apply edit suggestions to any HTML contenteditable and textarea elements.

Do **not** serve a page like this publicly, as it exposes the API key. See documentation [Deploying to Production](https://sapling.ai/docs/sdk/JavaScript/frontend_documentation#deploying-to-production) for an example of setting up the API key with a separate backend environment.

```javascript
import { useEffect } from 'react';
import { Sapling } from "@saplingai/sapling-js/observer";

function App() {
  useEffect(() => {
    Sapling.init({
      key: '<YOUR_API_KEY>',
      endpointHostname: 'https://api.sapling.ai',
      editPathname: '/api/v1/edits',
      statusBadge: true,
      mode: 'dev',
    });

    const editor = document.getElementById('editor');
    Sapling.observe(editor);
  });

  return (
    <div id="editor" sapling-ignore="true" contentEditable="true">
      Lets get started!
    </div>
  );
}
```

## Further Documentation

- Full Sapling HTTP API and SDK [documentation](https://sapling.ai/docs).
- Create a key by following [these instructions](https://sapling.ai/docs/api/api-access).
- JavaScript, HTML, and Python [starter code](https://github.com/saplingai/sapling-samples).