Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
docs(llm): note backend scripting is disabled by default and how to detect it
Elian Doran8d35f7388d84
diff --git a/apps/server/src/assets/llm/skills/backend_scripting.md b/apps/server/src/assets/llm/skills/backend_scripting.mdindex 3b9ee127b6..8cc508d003 100644--- a/apps/server/src/assets/llm/skills/backend_scripting.md+++ b/apps/server/src/assets/llm/skills/backend_scripting.md@@ -2,6 +2,12 @@Backend scripts run in Node.js on the server. They have direct access to notes in memory and can interact with the system (files, processes).+## Disabled by default — IMPORTANT++Backend script execution is **turned off by default** for security (backend scripts have full server access). Until it is enabled, running a backend script — including a frontend `runOnBackend()` call, `#run` schedules, `~runOn*` triggers, and custom request handlers — fails with *"Backend script execution is disabled"*. When you create a backend script for the user, **tell them it will not run until they enable it** in **Options → Security** (equivalently `[Security] backendScriptingEnabled=true` in `config.ini`, or the `TRILIUM_SECURITY_BACKEND_SCRIPTING_ENABLED=true` environment variable).++A frontend script can detect this at runtime with `api.isBackendScriptingEnabled()` and degrade gracefully (see the `frontend_scripting` skill). Raw SQL access (the SQL Console note type and the `/sql/execute` route) is gated separately by `[Security] sqlConsoleEnabled` / `api.isSqlConsoleEnabled()`.+## Creating a backend script1. Create a Code note with language "JavaScript (Trilium backend)".diff --git a/apps/server/src/assets/llm/skills/frontend_scripting.md b/apps/server/src/assets/llm/skills/frontend_scripting.mdindex f34a7de945..97c72e47ae 100644--- a/apps/server/src/assets/llm/skills/frontend_scripting.md+++ b/apps/server/src/assets/llm/skills/frontend_scripting.md@@ -239,9 +239,25 @@ Concrete examples:- `showPromptDialog(msg)` - prompt dialog (returns user input)### Backend integration++Backend script execution is **disabled by default** (a security setting, since backend scripts have full server access). When it is off, `runOnBackend` and `runAsyncOnBackendWithManualTransactionHandling` reject with *"Backend script execution is disabled"*. **Prefer doing the work on the frontend** whenever possible (most `api.*` note/search/date operations are available on the frontend too). When the backend is genuinely required, guard the call with the check below and tell the user to enable it in **Options → Security**.++- `isBackendScriptingEnabled()` - returns whether backend script execution is enabled (the `[Security] backendScriptingEnabled` config toggle). Check this before calling `runOnBackend` / `runAsyncOnBackendWithManualTransactionHandling` so the script can degrade gracefully instead of throwing.+- `isSqlConsoleEnabled()` - returns whether the SQL console is enabled (the `[Security] sqlConsoleEnabled` config toggle). Check this before running raw SQL on the backend (`api.sql.*`).- `runOnBackend(func, params)` - execute a function on the backend. The function **MUST be synchronous** — do NOT pass an `async` function. `runOnBackend` wraps the call in a SQL transaction, which does not support async; passing an `async` function triggers the warning *"You're passing an async function to `api.runOnBackend()` which will likely not work as you intended"* and the transaction will not behave correctly.- `runAsyncOnBackendWithManualTransactionHandling(func, params)` - use this instead when the backend function genuinely needs to be `async` (e.g. it `await`s a `fetch()`). Automatic transaction management is disabled, so wrap any DB writes in `api.transactional(...)` yourself inside the function.+```jsx+// Guard backend work so a disabled instance shows a friendly message instead of an error toast+import { isBackendScriptingEnabled, runOnBackend, showError } from "trilium:api";++if (!isBackendScriptingEnabled()) {+ showError("This feature needs backend scripting — enable it in Options → Security.");+ return;+}+const title = await runOnBackend((noteId) => api.getNote(noteId).title, [noteId]);+```+```jsx// ✅ Synchronous backend work — use runOnBackendconst title = await runOnBackend((noteId) => {