Editor scripts
The Server-side Editor API can be accessed through the POST /collaborations/{document_id}/evaluate-script
endpoint from the REST API. This endpoint allows you to execute JavaScript code that uses the CKEditor–5 API directly on the Cloud Services server.
# Prerequisites
- An editor bundle needs to be uploaded.
- The source document needs to be created after uploading the editor bundle.
# Request Structure
The request body should contain a script
parameter with the JavaScript code to be executed:
{
"script": "// Your JavaScript code here that uses the CKEditor 5 API"
}
The script
parameter should contain valid JavaScript code that can access the CKEditor–5 API. The script has access to:
- Editor instance: The CKEditor–5 editor instance is available in the script context
- Document model: Access to the document’s data model for content manipulation
- Editor plugins: All loaded plugins and their APIs
- Collaboration features: Access to comments, suggestions, and revision history
You can also include a user
object in the request body to control the user’s visibility and name during script execution.
By default, the system uses hardcoded user data with the ServerSideEditorAPI
identifier for the script execution. You can implement special handling for this user in your application, for example, to process webhook events or track script execution activities.
The example below shows a basic script for getting editor data, along with user context configuration that allows you to execute scripts with specific user context and to control how the user appears in the collaboration session:
{
"script": "editor.getData()",
"user": {
"id": "user-123",
"name": "John Doe",
"hidden_in_presence_list": true
}
}
# Syntax
Scripts can be written in both async and non-async ways. You can use await
for asynchronous operations:
// Non-async script
editor.getData();
// Async script
await Promise.resolve( editor.getData() );
You can return data from your script using the return
statement. The returned value will be available as literal data or as a JSON (in case of an object) in the API response:
// Return processed data
const content = editor.getData();
return {
content: content,
wordCount: content.split(' ').length
};
The CKEditor 5 editor instance is available globally as the editor
object. You can access all editor methods and properties:
// Get editor content
const content = editor.getData();
// Set editor content
editor.setData('<p>New content</p>');
// Access editor model
const model = editor.model;
// Use editor commands
editor.execute('bold');
Scripts have access to most browser and JavaScript APIs, with some restrictions for security reasons.
Refer to the security considerations guide for detailed implementation.
Regular HTTP calls using fetch()
or HTTP requests through the editor’s data layer are permitted and can be used to communicate with third-party services.
# Authentication
All requests to the Server-side Editor API require proper authentication using:
X-CS-Timestamp
header with the current timestampX-CS-Signature
header with a valid request signature
Refer to the request signature guide for detailed implementation.
# Response
The API returns a 201
status code on successful execution. The response contains the data returned by your script under the data
attribute:
{
"data": "<p>Editor content</p>"
}
For objects returned by your script, the response will be:
{
"data": {
"content": "<p>Editor content</p>",
"wordCount": 5
}
}
The script execution time is limited. For complex operations, consider breaking them into smaller, more focused scripts.
Make sure your script is well-tested before executing it on production documents, as it can permanently modify document content and structure.