这是一个用于与Obsidian Local REST API交互的Python客户端。
Python的requests库与Obsidian的HTTP服务器存在兼容性问题(返回502错误),因此本方案使用subprocess调用curl命令来绕过这个问题。
obsidian_simple.py- 最简单的API调用示例obsidian_api.py- 完整的API客户端类,包含文件类型识别和统计功能obsidian_test_env/- Python虚拟环境
- 在Obsidian中安装并启用"Local REST API"插件
- 在插件设置中:
- 复制API Key
- 启用"Enable Non-encrypted (HTTP) Server"开关
- HTTP端口通常是27123
import subprocess
import json
api_key = "your_api_key_here"
url = "http://127.0.0.1:27123/vault/"
curl_command = [
'curl',
'-s',
'-H', f'Authorization: Bearer {api_key}',
url
]
result = subprocess.run(curl_command, capture_output=True, text=True)
print(result.stdout)from obsidian_api import ObsidianAPI
api_key = "your_api_key_here"
client = ObsidianAPI(api_key)
# 列出所有文件
client.list_files(detailed=True){
"files": [
"file1.md",
"folder/",
"file2.png",
...
]
}