Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): improve creation of custom request handlers

Elian Doran4564ff81274b
diff --git a/apps/server/src/assets/llm/skills/backend_scripting.md b/apps/server/src/assets/llm/skills/backend_scripting.md
index 1efeb49866..a2e0677db9 100644
--- a/apps/server/src/assets/llm/skills/backend_scripting.md
+++ b/apps/server/src/assets/llm/skills/backend_scripting.md
@@ -152,6 +152,29 @@ These are defined as relations. `api.originEntity` contains the entity that trig
Relations can be inheritable — when set, they apply to all descendant notes.
+## Custom request handlers
+
+A backend script with a `#customRequestHandler` label becomes a public REST endpoint under `/custom/...`. The label value is a regular expression matched against the request path (e.g. `#customRequestHandler=create-note` is reachable at `/custom/create-note`).
+
+The Express request and response objects are exposed as **`api.req`** and **`api.res`** — **not** bare `req`/`res`. Write the HTTP response by calling methods on `api.res`.
+
+```javascript
+const { req, res } = api; // destructure from api — api.req / api.res, never global req/res
+const { secret, title, content } = req.body;
+
+if (req.method === "POST" && secret === "secret-password") {
+ const targetParentNoteId = api.currentNote.getRelationValue("targetNote");
+ const { note } = api.createTextNote(targetParentNoteId, title, content);
+ res.status(201).json(note.getPojo());
+} else {
+ res.sendStatus(400);
+}
+```
+
+- Regex capture groups from the matched path are available in **`api.pathParams`** (e.g. `#customRequestHandler=notes/([0-9]+)` → `api.pathParams[0]`).
+- Query params come from standard Express: `api.req.query.noteId`.
+- These endpoints are **unauthenticated** by default — handle authentication yourself (e.g. a shared secret as above).
+
## Example: auto-color notes by category
```javascript