Deploying to Production
Here's a sample deployment setup using the JavaScript SDK along with a Python backend. This is of course just one way to set up a deployment; if you have any questions, hop on our Discord and chat with us.
Steps
- Import the SDK by inserting it into a page as a JavaScript script.
- Initialize with API key using the
Sapling.init
method. - Observe any textarea or contenteditable DOM element using the
Sapling.observe
method. - Set up a backend endpoint with your API key to request edits.
Sample Quickstart
- Save the Sample HTML Frontend code to
sdk.html
and open in a browser. - Save the Sample Python (Flask) code below to
sdk_flask.py
. - Update
<API_KEY>
in the Python code. - Then run the following in command line:
pip install flask requests flask_cors
FLASK_APP=sdk_flask.py flask run --port 3000
Sample HTML Frontend
<html>
<head>
<script src="https://sapling.ai/static/js/sapling-sdk-v1.0.9.min.js"></script>
</head>
<body>
<div contenteditable="true" id="editor" sapling-ignore="true">Lets get started!</div>
<script type="text/javascript">
Sapling.init({
endpointHostname: 'http://localhost:3000',
saplingPathPrefix: '/sapling',
});
const contentEditable = document.getElementById('editor');
Sapling.observe(contentEditable);
</script>
</body>
</html>
For more information please see the JavaScript documentation.
Sample Python (Flask) Backend
Here's the backend in Python (Flask). Adapt the code below according to the framework that you're using.
import requests
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origins='*')
SAPLING_API_HOSTNAME = 'https://api.sapling.ai'
API_KEY = '<api-key>'
@app.route('/sapling/<path:path>', methods=['POST'])
def sapling_proxy(path):
request_url = f'{SAPLING_API_HOSTNAME}/{path}'
args = request.get_json()
args['key'] = API_KEY
result = requests.post(request_url, json=args)
result = result.json()
return jsonify(result), 200