siyuan/API.md

834 lines
16 KiB
Markdown
Raw Normal View History

* [Specification](#Specification)
* [Parameters and return values](#Parameters-and-return-values)
2021-09-17 12:04:12 +08:00
* [鉴权](#鉴权)
2021-09-17 10:41:05 +08:00
* [笔记本](#笔记本)
2021-09-17 11:38:54 +08:00
* [列出笔记本](#列出笔记本)
* [打开笔记本](#打开笔记本)
* [关闭笔记本](#关闭笔记本)
* [重命名笔记本](#重命名笔记本)
2021-09-17 21:57:36 +08:00
* [创建笔记本](#创建笔记本)
2021-09-17 11:38:54 +08:00
* [删除笔记本](#删除笔记本)
* [获取笔记本配置](#获取笔记本配置)
* [保存笔记本配置](#保存笔记本配置)
2021-09-17 10:40:22 +08:00
* [文档](#文档)
2021-09-17 11:38:54 +08:00
* [通过 Markdown 创建文档](#通过-markdown-创建文档)
* [重命名文档](#重命名文档)
* [删除文档](#删除文档)
2021-09-17 22:03:31 +08:00
* [移动文档](#移动文档)
2021-09-17 22:07:55 +08:00
* [根据路径获取人类可读路径](#根据路径获取人类可读路径)
2021-09-17 10:40:22 +08:00
* [资源文件](#资源文件)
2021-09-17 11:38:54 +08:00
* [上传资源文件](#上传资源文件)
* [块操作](#块操作)
* [插入块](#插入块)
* [插入前置子块](#插入前置子块)
* [插入后置子块](#插入后置子块)
* [更新块](#更新块)
* [删除块](#删除块)
2021-09-17 10:41:05 +08:00
* [属性](#属性)
2021-09-17 11:38:54 +08:00
* [设置块属性](#设置块属性)
* [获取块属性](#获取块属性)
* [SQL](#SQL)
* [SQL 查询](#SQL-查询)
2021-09-17 10:40:22 +08:00
* [模板](#模板)
2021-09-17 11:38:54 +08:00
* [渲染模板](#渲染模板)
2021-09-17 10:40:22 +08:00
* [导出](#导出)
2021-09-17 11:38:54 +08:00
* [导出 Markdown 文本](#导出-markdown-文本)
2021-09-17 10:40:22 +08:00
* [系统](#系统)
2021-09-17 11:54:13 +08:00
* [获取启动进度](#获取启动进度)
2021-09-17 11:58:41 +08:00
* [获取系统版本](#获取系统版本)
* [获取系统当前时间](#获取系统当前时间)
2021-09-17 12:03:15 +08:00
* [Webhook](#Webhook)
2021-09-17 10:40:22 +08:00
---
## Specification
2021-09-17 12:04:12 +08:00
### Parameters and return values
2021-09-17 10:40:22 +08:00
* Endpoint: `http://127.0.0.1:6806`
* Both are POST methods
* An interface with parameters is required, the parameter is a JSON string, placed in the body, and the header Content-Type is `application/json`
* Return value
2021-09-17 10:40:22 +08:00
````json
{
"code": 0,
"msg": "",
"data": {}
}
````
2021-09-17 10:40:22 +08:00
* `code`: non-zero for exceptions
* `msg`: an empty string under normal circumstances, an error text will be returned under abnormal conditions
* `data`: may be `{}`, `[]` or `NULL`, depending on the interface
2021-09-17 10:40:22 +08:00
### Authentication
2021-09-17 10:40:22 +08:00
View API token in <kbd>Settings - About</kbd>, request header: `Authorization: Token xxx`
2021-09-17 10:40:22 +08:00
## Notebooks
2021-09-17 10:40:22 +08:00
### List notebooks
2021-09-17 10:40:22 +08:00
* `/api/notebook/lsNotebooks`
* No parameters
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
2021-09-20 22:01:00 +08:00
"notebooks": [
2021-09-20 21:57:02 +08:00
{
"id": "20210817205410-2kvfpfn",
"name": "Test Notebook",
"icon": "1f41b",
"sort": 0,
"closed": false
2021-09-20 21:57:02 +08:00
},
{
"id": "20210808180117-czj9bvb",
"name": "SiYuan User Guide",
"icon": "1f4d4",
"sort": 1,
"closed": false
2021-09-20 21:57:02 +08:00
}
2021-09-17 10:40:22 +08:00
]
}
}
```
### Open a notebook
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
* `/api/notebook/openNotebook`
* Parameters
2021-09-17 11:38:54 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0"
}
```
* `notebook`: Notebook ID
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
2021-09-17 10:40:22 +08:00
### Close a notebook
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
* `/api/notebook/closeNotebook`
* Parameters
2021-09-17 11:38:54 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0"
}
```
* `notebook`: Notebook ID
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
2021-09-17 10:40:22 +08:00
### Rename a notebook
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
* `/api/notebook/renameNotebook`
* Parameters
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0",
"name": "New name for notebook"
2021-09-17 11:38:54 +08:00
}
```
* `notebook`: Notebook ID
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
### Create a notebook
2021-09-17 11:38:54 +08:00
* `/api/notebook/createNotebook`
* Parameters
2021-09-17 11:38:54 +08:00
```json
{
"name": "Notebook name"
2021-09-17 11:38:54 +08:00
}
```
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"notebook": {
"id": "20220126215949-r1wvoch",
"name": "Notebook name",
"icon": "",
"sort": 0,
"closed": false
}
}
2021-09-17 11:38:54 +08:00
}
```
2021-09-17 11:58:41 +08:00
### Remove a notebook
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
* `/api/notebook/removeNotebook`
* Parameters
2021-09-17 11:38:54 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0"
}
```
* `notebook`: Notebook ID
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
2021-09-17 10:40:22 +08:00
### Get notebook configuration
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
* `/api/notebook/getNotebookConf`
* Parameters
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
```json
{
"notebook": "20210817205410-2kvfpfn"
}
```
2021-09-17 10:40:22 +08:00
* `notebook`: Notebook ID
* Return value
2021-09-17 10:40:22 +08:00
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"box": "20210817205410-2kvfpfn",
"conf": {
"name": "Test Notebook",
2021-09-17 11:38:54 +08:00
"closed": false,
"refCreateSavePath": "",
"createDocNameTemplate": "",
"dailyNoteSavePath": "/daily note/{{now | date \"2006/01\"}}/{{now | date \"2006-01-02\"}}",
"dailyNoteTemplatePath": ""
},
"name": "Test Notebook"
2021-09-17 11:38:54 +08:00
}
}
```
### Save notebook configuration
2021-09-17 11:38:54 +08:00
* `/api/notebook/setNotebookConf`
* Parameters
2021-09-17 10:40:22 +08:00
```json
{
2021-09-17 11:38:54 +08:00
"notebook": "20210817205410-2kvfpfn",
"conf": {
"name": "Test Notebook",
2021-09-17 11:38:54 +08:00
"closed": false,
"refCreateSavePath": "",
"createDocNameTemplate": "",
"dailyNoteSavePath": "/daily note/{{now | date \"2006/01\"}}/{{now | date \"2006-01-02\"}}",
"dailyNoteTemplatePath": ""
}
2021-09-17 10:40:22 +08:00
}
```
* `notebook`: Notebook ID
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
2021-09-17 11:38:54 +08:00
"data": {
"name": "Test Notebook",
2021-09-17 11:38:54 +08:00
"closed": false,
"refCreateSavePath": "",
"createDocNameTemplate": "",
"dailyNoteSavePath": "/daily note/{{now | date \"2006/01\"}}/{{now | date \"2006-01-02\"}}",
"dailyNoteTemplatePath": ""
}
2021-09-17 10:40:22 +08:00
}
```
## Documents
2021-09-17 10:40:22 +08:00
### Create a document with Markdown
2021-09-17 10:40:22 +08:00
* `/api/filetree/createDocWithMd`
* Parameters
2021-09-17 10:40:22 +08:00
```json
{
2021-09-21 23:15:02 +08:00
"notebook": "20210817205410-2kvfpfn",
2021-09-17 10:40:22 +08:00
"path": "/foo/bar",
"markdown": ""
}
```
* `notebook`: Notebook ID
* `path`: Document path, which needs to start with / and separate levels with / (path here corresponds to the database hpath field)
* `markdown`: GFM Markdown content
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": "20210914223645-oj2vnx2"
}
```
* `data`: Created document ID
* If you use the same `path` to call this interface repeatedly, the existing document will not be overwritten, but a new document ending with a random number will be created
2021-09-17 10:40:22 +08:00
### Rename a document
2021-09-17 10:40:22 +08:00
2021-09-17 11:43:47 +08:00
* `/api/filetree/renameDoc`
* Parameters
2021-09-17 11:43:47 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0",
"path": "/20210902210113-0avi12f.sy",
"title": "Document new title"
2021-09-17 11:43:47 +08:00
}
```
* `notebook`: Notebook ID
* `path`: Document path
* Return value
2021-09-17 11:43:47 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
2021-09-17 10:40:22 +08:00
### Remove a document
2021-09-17 10:40:22 +08:00
2021-09-17 11:43:47 +08:00
* `/api/filetree/removeDoc`
* Parameters
2021-09-17 11:43:47 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0",
"path": "/20210902210113-0avi12f.sy"
}
```
* `notebook`: Notebook ID
* `path`: Document path
* Return value
2021-09-17 10:40:22 +08:00
2021-09-17 11:43:47 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
2021-09-17 11:58:41 +08:00
### Move a document
2021-09-17 22:03:31 +08:00
* `/api/filetree/moveDoc`
* Parameters
2021-09-17 22:03:31 +08:00
```json
{
"fromNotebook": "20210831090520-7dvbdv0",
"fromPath": "/20210917220056-yxtyl7i.sy",
"toNotebook": "20210817205410-2kvfpfn",
"toPath": "/"
}
```
* `fromNotebook`: Source notebook ID
* `fromPath`: Source path
* `toNotebook`: Target notebook ID
* `toPath`: Target path
* Return value
2021-09-17 22:03:31 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
### Get human-readable path based on path
2021-09-17 22:07:55 +08:00
* `/api/filetree/getHPathByPath`
* Parameters
2021-09-17 22:07:55 +08:00
```json
{
"notebook": "20210831090520-7dvbdv0",
"path": "/20210917220500-sz588nq/20210917220056-yxtyl7i.sy"
}
```
* `notebook`: Notebook ID
* `path`: Document path
* Return value
2021-09-17 22:07:55 +08:00
```json
{
"code": 0,
"msg": "",
"data": "/foo/bar"
}
```
## Assets
2021-09-17 10:40:22 +08:00
### Upload assets
2021-09-17 10:40:22 +08:00
* `/api/asset/upload`
* The parameter is an HTTP Multipart form
2021-09-17 10:40:22 +08:00
* `assetsDirPath`: The folder path where the assets are stored. The arguments have the following three cases
2021-09-17 10:40:22 +08:00
1. `"/assets/"`: Workspace/data/assets folder
2. `"/Test Notebook/assets/"`: Assets folder under `Test Notebook`
3. `"/Test Notebook/foo/assets/"`: Assets folder under foo folder under `Test notebook`
2021-09-17 10:40:22 +08:00
It is recommended to use the first one, which is stored in the workspace assets folder uniformly.
* `file[]`: Uploaded file list
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"errFiles": [""],
"succMap": {
"foo.png": "assets/foo-20210719092549-9j5y79r.png"
}
}
}
```
* `errFiles`: List of filenames with errors in upload processing
* `succMap`: For successfully processed files, the key is the file name when uploading, and the value is assets/foo-id.png, which is used to replace the asset link address in the existing Markdown content with the uploaded address
2021-09-17 10:40:22 +08:00
## Blocks
### Insert blocks
* `/api/block/insertBlock`
* Parameters
```json
{
"dataType": "markdown",
"data": "foo**bar**{: style=\"color: var(--b3-font-color8);\"}baz",
"previousID": "20211229114650-vrek5x6"
}
```
* `dataType`: The data type to be inserted, the value can be `markdown` or `dom`
* `data`: Data to be inserted
* `previousID`: The ID of the previous block, used to anchor the insertion position
* Return value
```json
{
"code": 0,
"msg": "",
"data": [
{
"doOperations": [
{
"action": "insert",
"data": "<div data-node-id=\"20211230115020-g02dfx0\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">foo<strong style=\"color: var(--b3-font-color8);\">bar</strong>baz</div><div class=\"protyle-attr\" contenteditable=\"false\"></div></div>",
"id": "20211230115020-g02dfx0",
"parentID": "",
"previousID": "20211229114650-vrek5x6",
"retData": null
}
],
"undoOperations": null
}
]
}
```
* `action.data`: DOM generated by the newly inserted block
* `action.id`: ID of the newly inserted block
### Prepend blocks
* `/api/block/prependBlock`
* Parameters
```json
{
"data": "foo**bar**{: style=\"color: var(--b3-font-color8);\"}baz",
"dataType": "markdown",
"parentID": "20220107173950-7f9m1nb"
}
```
* `dataType`: The data type to be inserted, the value can be `markdown` or `dom`
* `data`: Data to be inserted
* `parentID`: The ID of the parent block, used to anchor the insertion position
* Return value
```json
{
"code": 0,
"msg": "",
"data": [
{
"doOperations": [
{
"action": "insert",
"data": "<div data-node-id=\"20220108003710-hm0x9sc\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">foo<strong style=\"color: var(--b3-font-color8);\">bar</strong>baz</div><div class=\"protyle-attr\" contenteditable=\"false\"></div></div>",
"id": "20220108003710-hm0x9sc",
"parentID": "20220107173950-7f9m1nb",
"previousID": "",
"retData": null
}
],
"undoOperations": null
}
]
}
```
* `action.data`: DOM generated by the newly inserted block
* `action.id`: ID of the newly inserted block
### Append blocks
* `/api/block/appendBlock`
* Parameters
```json
{
"data": "foo**bar**{: style=\"color: var(--b3-font-color8);\"}baz",
"dataType": "markdown",
"parentID": "20220107173950-7f9m1nb"
}
```
* `dataType`: The data type to be inserted, the value can be `markdown` or `dom`
* `data`: Data to be inserted
* `parentID`: The ID of the parent block, used to anchor the insertion position
* Return value
```json
{
"code": 0,
"msg": "",
"data": [
{
"doOperations": [
{
"action": "insert",
"data": "<div data-node-id=\"20220108003642-y2wmpcv\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">foo<strong style=\"color: var(--b3-font-color8);\">bar</strong>baz</div><div class=\"protyle-attr\" contenteditable=\"false\"></div></div>",
"id": "20220108003642-y2wmpcv",
"parentID": "20220107173950-7f9m1nb",
"previousID": "20220108003615-7rk41t1",
"retData": null
}
],
"undoOperations": null
}
]
}
```
* `action.data`: DOM generated by the newly inserted block
* `action.id`: ID of the newly inserted block
### Update a block
* `/api/block/updateBlock`
* Parameters
```json
{
"dataType": "markdown",
"data": "foobarbaz",
"id": "20211230161520-querkps"
}
```
* `dataType`: The data type to be updated, the value can be `markdown` or `dom`
* `data`: Data to be updated
* `id`: ID of the block to be updated
* Return value
```json
{
"code": 0,
"msg": "",
"data": [
{
"doOperations": [
{
"action": "update",
"data": "<div data-node-id=\"20211230161520-querkps\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">foo<strong>bar</strong>baz</div><div class=\"protyle-attr\" contenteditable=\"false\"></div></div>",
"id": "20211230161520-querkps",
"parentID": "",
"previousID": "",
"retData": null
}
],
"undoOperations": null
}
]
}
```
* `action.data`: DOM generated by the updated block
### Delete a block
* `/api/block/deleteBlock`
* Parameters
```json
{
"id": "20211230161520-querkps"
}
```
* `id`: ID of the block to be deleted
* Return value
```json
{
"code": 0,
"msg": "",
"data": [
{
"doOperations": [
{
"action": "delete",
"data": null,
"id": "20211230162439-vtm09qo",
"parentID": "",
"previousID": "",
"retData": null
}
],
"undoOperations": null
}
]
}
```
## Attributes
2021-09-17 10:40:22 +08:00
### Set block properties
2021-09-17 10:40:22 +08:00
* `/api/attr/setBlockAttrs`
* Parameters
2021-09-17 10:40:22 +08:00
```json
{
"id": "20210912214605-uhi5gco",
"attrs": {
"custom-attr1": "line1\nline2"
}
}
```
* `id`: Block ID
* `attrs`: Block attributes, custom attributes must be prefixed with `custom-`
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": null
}
```
### Get block attributes
2021-09-17 10:40:22 +08:00
* `/api/attr/getBlockAttrs`
* Parameters
2021-09-17 10:40:22 +08:00
```json
{
"id": "20210912214605-uhi5gco"
}
```
* `id`: Block ID
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"custom-attr1": "line1\nline2",
"id": "20210912214605-uhi5gco",
"title": "PDF Annotation Demo",
2021-09-17 10:40:22 +08:00
"type": "doc",
"updated": "20210916120715"
}
}
```
2021-09-17 11:38:54 +08:00
## SQL
### Execute SQL query
2021-09-17 11:38:54 +08:00
* `/api/query/sql`
* Parameters
2021-09-17 11:38:54 +08:00
```json
{
"stmt": "SELECT * FROM blocks WHERE content LIKE'%content%' LIMIT 7"
}
```
* `stmt`: SQL statement
* Return value
2021-09-17 11:38:54 +08:00
```json
{
"code": 0,
"msg": "",
"data": [
{ "col": "val" }
2021-09-17 11:38:54 +08:00
]
}
```
2021-09-17 11:58:41 +08:00
## Templates
2021-09-17 10:40:22 +08:00
### Render a template
2021-09-17 10:40:22 +08:00
/template/render
## Export
2021-09-17 10:40:22 +08:00
### Export Markdown
2021-09-17 10:40:22 +08:00
* `/api/export/exportMdContent`
* Parameters
2021-09-17 10:40:22 +08:00
```json
{
"id": ""
}
```
* `id`: ID of the doc block to export
* Return value
2021-09-17 10:40:22 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"hPath": "/Please Start Here",
"content": "## 🍫 Content Block\n\nIn SiYuan, the only important core concept is..."
2021-09-17 10:40:22 +08:00
}
}
```
* `hPath`: human-readable path
* `content`: Markdown content
2021-09-17 10:40:22 +08:00
## System
2021-09-17 10:40:22 +08:00
### Get boot progress
2021-09-17 11:54:13 +08:00
* `/api/system/bootProgress`
* No parameters
* Return value
2021-09-17 11:54:13 +08:00
```json
{
"code": 0,
"msg": "",
"data": {
"details": "Finishing boot...",
"progress": 100
}
}
```
2021-09-17 11:58:41 +08:00
### Get system version
2021-09-17 10:40:22 +08:00
2021-09-17 11:54:13 +08:00
* `/api/system/version`
* No parameters
* Return value
2021-09-17 10:40:22 +08:00
2021-09-17 11:54:13 +08:00
```json
{
"code": 0,
"msg": "",
"data": "1.3.5"
}
```
### Get the current time of the system
2021-09-17 11:58:41 +08:00
* `/api/system/currentTime`
* No parameters
* Return value
2021-09-17 11:58:41 +08:00
```json
{
"code": 0,
"msg": "",
"data": 1631850968131
}
```
* `data`: Precision in milliseconds
2021-09-17 11:58:41 +08:00
2021-09-17 12:03:15 +08:00
## Webhook
TBD
https://ld246.com/article/1627956688432