# Class: BackendScriptApi

## BackendScriptApi

Source:
  - services/backend_script_api.js, line 24

### Members

#### __private :Object

This object contains "at your risk" and "no BC guarantees" objects for advanced use cases.

##### Type:

- Object

Source:
  - services/backend_script_api.js, line 435

#### axios

##### Properties:

Name | Type | Description
--- | --- | ---
`Axios` | axios | library for HTTP requests. See https://axios-http.com/ for documentation

Source:
  - services/backend_script_api.js, line 37

#### currentNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | Note | where script is currently executing. Don't mix this up with concept of active note

Source:
  - services/backend_script_api.js, line 28

#### dayjs

##### Properties:

Name | Type | Description
--- | --- | ---
`day.js` | dayjs | library for date manipulation. See https://day.js.org/ for documentation

Source:
  - services/backend_script_api.js, line 39

#### originEntity

##### Properties:

Name | Type | Description
--- | --- | ---
`entity` | Entity | whose event triggered this executions

Source:
  - services/backend_script_api.js, line 30

#### sql

##### Properties:

Name | Type | Description
--- | --- | ---
`sql` | module:sql |

Source:
  - services/backend_script_api.js, line 417

#### startNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | Note | where script started executing

Source:
  - services/backend_script_api.js, line 26

#### xml2js

##### Properties:

Name | Type | Description
--- | --- | ---
`xml2js` | axios | library for XML parsing. See https://github.com/Leonidas-from-XIV/node-xml2js for documentation

Source:
  - services/backend_script_api.js, line 41

### Methods

#### createDataNote(parentNoteId, title, content) → {Object}

Create data note - data in this context means object serializable to JSON. Created note will be of type 'code' and JSON MIME type. See also createNewNote() for more options.

##### Parameters:

Name | Type | Description
--- | --- | ---
`parentNoteId` | string |
`title` | string |
`content` | object |

Source:
  - services/backend_script_api.js, line 200

##### Returns:

object having "note" and "branch" keys representing respective objects

Type
  Object

#### createNewNote(paramsopt) → {Object}

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`params` | CreateNewNoteParams | <optional> |

Source:
  - services/backend_script_api.js, line 227

##### Returns:

object contains newly created entities note and branch

Type
  Object

#### createNote(parentNoteId, title, contentopt, extraOptionsopt) → {Object}

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`parentNoteId` | string |  |  | create new note under this parent
`title` | string |  |  |
`content` | string | <optional> | "" |
`extraOptions` | CreateNoteExtraOptions | <optional> | {} |

Deprecated:
  - please use createTextNote() with similar API for simpler use cases or createNewNote() for more complex needs
Source:
  - services/backend_script_api.js, line 255

##### Returns:

object contains newly created entities note and branch

Type
  Object

#### createTextNote(parentNoteId, title, content) → {Object}

Create text note. See also createNewNote() for more options.

##### Parameters:

Name | Type | Description
--- | --- | ---
`parentNoteId` | string |
`title` | string |
`content` | string |

Source:
  - services/backend_script_api.js, line 184

##### Returns:

- object having "note" and "branch" keys representing respective objects

Type
  Object

#### ensureNoteIsAbsentFromParent(noteId, parentNoteId) → {void}

If there's a branch between note and parent note, remove it. Otherwise, do nothing.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`parentNoteId` | string |

Source:
  - services/backend_script_api.js, line 155

##### Returns:

Type
  void

#### ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) → {void}

If there's no branch between note and parent note, create one. Otherwise, do nothing.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`parentNoteId` | string |
`prefix` | string | if branch will be created between note and parent note, set this prefix

Source:
  - services/backend_script_api.js, line 145

##### Returns:

Type
  void

#### escapeHtml(string) → {string}

##### Parameters:

Name | Type | Description
--- | --- | ---
`string` | string | to escape

Source:
  - services/backend_script_api.js, line 405

##### Returns:

escaped string

Type
  string

#### getAppInfo() → {Object|*}

Source:
  - services/backend_script_api.js, line 428

##### Returns:

- object representing basic info about running Trilium version

Type
  Object | *

#### getAttribute(attributeId) → {Attribute|null}

##### Parameters:

Name | Type | Description
--- | --- | ---
`attributeId` | string |

Source:
  - services/backend_script_api.js, line 75

##### Returns:

Type
  Attribute | null

#### getBranch(branchId) → {Branch|null}

##### Parameters:

Name | Type | Description
--- | --- | ---
`branchId` | string |

Source:
  - services/backend_script_api.js, line 68

##### Returns:

Type
  Branch | null

#### getDateNote(date) → {Note|null}

Returns day note for given date. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM-DD format

Deprecated:
  - use getDayNote instead
Source:
  - services/backend_script_api.js, line 314

##### Returns:

Type
  Note | null

#### getDayNote(date) → {Note|null}

Returns day note for given date. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM-DD format

Source:
  - services/backend_script_api.js, line 323

##### Returns:

Type
  Note | null

#### getInstanceName() → {string|null}

Instance name identifies particular Trilium instance. It can be useful for scripts if some action needs to happen on only one specific instance.

Source:
  - services/backend_script_api.js, line 54

##### Returns:

Type
  string | null

#### getMonthNote(date) → {Note|null}

Returns month note for given date. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM format

Source:
  - services/backend_script_api.js, line 350

##### Returns:

Type
  Note | null

#### getNote(noteId) → {Note|null}

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |

Source:
  - services/backend_script_api.js, line 61

##### Returns:

Type
  Note | null

#### getNotesWithLabel(name, valueopt) → {Array.<Note>}

Retrieves notes with given label name & value

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | string |  | attribute name
`value` | string | <optional> | attribute value

Source:
  - services/backend_script_api.js, line 124

##### Returns:

Type
  Array.<Note>

#### getNoteWithLabel(name, valueopt) → {Note|null}

Retrieves first note with given label name & value

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | string |  | attribute name
`value` | string | <optional> | attribute value

Source:
  - services/backend_script_api.js, line 134

##### Returns:

Type
  Note | null

#### getRootCalendarNote() → {Note|null}

Returns root note of the calendar.

Source:
  - services/backend_script_api.js, line 304

##### Returns:

Type
  Note | null

#### getTodayNote() → {Note|null}

Returns today's day note. If such note doesn't exist, it is created.

Source:
  - services/backend_script_api.js, line 331

##### Returns:

Type
  Note | null

#### getWeekNote(date, options) → {Note|null}

Returns note for the first date of the week of the given date.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM-DD format
`options` | object | "startOfTheWeek" - either "monday" (default) or "sunday"

Source:
  - services/backend_script_api.js, line 341

##### Returns:

Type
  Note | null

#### getYearNote(year) → {Note|null}

Returns year note for given year. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`year` | string | in YYYY format

Source:
  - services/backend_script_api.js, line 359

##### Returns:

Type
  Note | null

#### log(message)

Log given message to trilium logs.

##### Parameters:

Name | Type | Description
--- | --- | ---
`message` |  |

Source:
  - services/backend_script_api.js, line 296

#### randomString(length) → {string}

Return randomly generated string of given length. This random string generation is NOT cryptographically secure.

##### Parameters:

Name | Type | Description
--- | --- | ---
`length` | number | of the string

Source:
  - services/backend_script_api.js, line 398

##### Returns:

random string

Type
  string

#### refreshTree()

Deprecated:
  - - this is now no-op since all the changes should be gracefully handled per widget
Source:
  - services/backend_script_api.js, line 423

#### searchForNote(query, searchParamsopt) → {Note|null}

This is a powerful search method - you can search by attributes and their values, e.g.: "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`query` | string |  |
`searchParams` | Object | <optional> |

Source:
  - services/backend_script_api.js, line 110

##### Returns:

Type
  Note | null

#### searchForNotes(query, searchParamsopt) → {Array.<Note>}

This is a powerful search method - you can search by attributes and their values, e.g.: "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`query` | string |  |
`searchParams` | Object | <optional> |

Source:
  - services/backend_script_api.js, line 86

##### Returns:

Type
  Array.<Note>

#### setNoteToParent(noteId, prefix, parentNoteId)

This method finds note by its noteId and prefix and either sets it to the given parentNoteId or removes the branch (if parentNoteId is not given). This method looks similar to toggleNoteInParent() but differs because we're looking up branch by prefix.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`prefix` | string |
`parentNoteId` | string | null |

Deprecated:
  - - this method is pretty confusing and serves specialized purpose only
Source:
  - services/backend_script_api.js, line 379

#### sortNotesByTitle(parentNoteId)

##### Parameters:

Name | Type | Description
--- | --- | ---
`parentNoteId` | string | this note's child notes will be sorted

Source:
  - services/backend_script_api.js, line 365

#### toggleNoteInParent(present, noteId, parentNoteId, prefix) → {void}

Based on the value, either create or remove branch between note and parent note.

##### Parameters:

Name | Type | Description
--- | --- | ---
`present` | boolean | true if we want the branch to exist, false if we want it gone
`noteId` | string |
`parentNoteId` | string |
`prefix` | string | if branch will be created between note and parent note, set this prefix

Source:
  - services/backend_script_api.js, line 167

##### Returns:

Type
  void

#### transactional(func) → {?}

This functions wraps code which is supposed to be running in transaction. If transaction already exists, then we'll use that transaction.

##### Parameters:

Name | Type | Description
--- | --- | ---
`func` | function |

Source:
  - services/backend_script_api.js, line 389

##### Returns:

result of func callback

Type
  ?

#### unescapeHtml(string) → {string}

##### Parameters:

Name | Type | Description
--- | --- | ---
`string` | string | to unescape

Source:
  - services/backend_script_api.js, line 412

##### Returns:

unescaped string

Type
  string