REST API 参考
EmDash 在 /_emdash/api/ 处公开了一个 REST API,用于内容管理、媒体上传和架构操作。
API 请求需要通过 Bearer 令牌进行认证:
Authorization: Bearer <token>可以通过管理界面或编程方式生成令牌。
所有响应都遵循一致的格式:
// Success{ "success": true, "data": { ... }}
// Error{ "success": false, "error": { "code": "ERROR_CODE", "message": "Human-readable message", "details": { ... } }}GET /_emdash/api/content/:collection| 参数 | 类型 | 描述 |
|---|---|---|
collection | string | 集合 slug(路径) |
cursor | string | 分页游标(查询) |
limit | number | 每页项目数(查询,默认:50) |
status | string | 按状态筛选(查询) |
orderBy | string | 排序字段(查询) |
order | string | 排序方向:asc 或 desc(查询) |
{ "success": true, "data": { "items": [ { "id": "01HXK5MZSN...", "type": "posts", "slug": "hello-world", "data": { "title": "Hello World", ... }, "status": "published", "createdAt": "2025-01-24T12:00:00Z", "updatedAt": "2025-01-24T12:00:00Z" } ], "nextCursor": "eyJpZCI6..." }}GET /_emdash/api/content/:collection/:id{ "success": true, "data": { "item": { "id": "01HXK5MZSN...", "type": "posts", "slug": "hello-world", "data": { "title": "Hello World", ... }, "status": "published", "createdAt": "2025-01-24T12:00:00Z", "updatedAt": "2025-01-24T12:00:00Z" } }}POST /_emdash/api/content/:collectionContent-Type: application/json{ "data": { "title": "New Post", "content": [...] }, "slug": "new-post", "status": "draft"}{ "success": true, "data": { "item": { ... } }}PUT /_emdash/api/content/:collection/:idContent-Type: application/json{ "data": { "title": "Updated Title" }, "status": "published"}DELETE /_emdash/api/content/:collection/:id{ "success": true, "data": { "success": true }}GET /_emdash/api/media| 参数 | 类型 | 描述 |
|---|---|---|
cursor | string | 分页游标 |
limit | number | 每页项目数(默认:20) |
mimeType | string | 按 MIME 类型前缀筛选 |
{ "success": true, "data": { "items": [ { "id": "01HXK5MZSN...", "filename": "photo.jpg", "mimeType": "image/jpeg", "size": 102400, "width": 1920, "height": 1080, "url": "https://cdn.example.com/photo.jpg", "createdAt": "2025-01-24T12:00:00Z" } ], "nextCursor": "eyJpZCI6..." }}GET /_emdash/api/media/:idPOST /_emdash/api/mediaContent-Type: application/json{ "filename": "photo.jpg", "mimeType": "image/jpeg", "size": 102400, "width": 1920, "height": 1080, "storageKey": "uploads/photo.jpg"}PUT /_emdash/api/media/:idContent-Type: application/json{ "alt": "Photo description", "caption": "Photo caption"}DELETE /_emdash/api/media/:id获取媒体文件
Section titled “获取媒体文件”GET /_emdash/api/media/file/:key提供实际的文件内容。仅适用于本地存储。
GET /_emdash/api/content/:collection/:entryId/revisions| 参数 | 类型 | 描述 |
|---|---|---|
limit | number | 要返回的最大修订数(默认:50) |
{ "success": true, "data": { "items": [ { "id": "01HXK5MZSN...", "collection": "posts", "entryId": "01HXK5MZSN...", "data": { ... }, "createdAt": "2025-01-24T12:00:00Z" } ], "total": 5 }}GET /_emdash/api/revisions/:revisionIdPOST /_emdash/api/revisions/:revisionId/restore将内容恢复到此修订的状态并创建一个新修订。
GET /_emdash/api/schema/collections{ "success": true, "data": { "items": [ { "id": "01HXK5MZSN...", "slug": "posts", "label": "Posts", "labelSingular": "Post", "supports": ["drafts", "revisions", "preview"] } ] }}GET /_emdash/api/schema/collections/:slug| 参数 | 类型 | 描述 |
|---|---|---|
includeFields | boolean | 包含字段定义(查询) |
POST /_emdash/api/schema/collectionsContent-Type: application/json{ "slug": "products", "label": "Products", "labelSingular": "Product", "description": "Product catalog", "supports": ["drafts", "revisions"]}PATCH /_emdash/api/schema/collections/:slugContent-Type: application/jsonDELETE /_emdash/api/schema/collections/:slug| 参数 | 类型 | 描述 |
|---|---|---|
force | boolean | 即使集合有内容也删除(查询) |
GET /_emdash/api/schema/collections/:slug/fieldsPOST /_emdash/api/schema/collections/:slug/fieldsContent-Type: application/json{ "slug": "price", "label": "Price", "type": "number", "required": true, "validation": { "min": 0 }}PATCH /_emdash/api/schema/collections/:collectionSlug/fields/:fieldSlugContent-Type: application/jsonDELETE /_emdash/api/schema/collections/:collectionSlug/fields/:fieldSlug重新排序字段
Section titled “重新排序字段”POST /_emdash/api/schema/collections/:slug/fields/reorderContent-Type: application/json{ "fieldSlugs": ["title", "content", "author", "publishedAt"]}导出架构(JSON)
Section titled “导出架构(JSON)”GET /_emdash/api/schemaAccept: application/json导出架构(TypeScript)
Section titled “导出架构(TypeScript)”GET /_emdash/api/schema?format=typescriptAccept: text/typescript返回所有集合的 TypeScript 接口。
GET /_emdash/api/pluginsGET /_emdash/api/plugins/:pluginIdPOST /_emdash/api/plugins/:pluginId/enablePOST /_emdash/api/plugins/:pluginId/disable| 代码 | HTTP 状态码 | 描述 |
|---|---|---|
NOT_FOUND | 404 | 资源未找到 |
VALIDATION_ERROR | 400 | 输入数据无效 |
UNAUTHORIZED | 401 | 令牌缺失或无效 |
FORBIDDEN | 403 | 权限不足 |
CONTENT_LIST_ERROR | 500 | 列出内容失败 |
CONTENT_CREATE_ERROR | 500 | 创建内容失败 |
CONTENT_UPDATE_ERROR | 500 | 更新内容失败 |
CONTENT_DELETE_ERROR | 500 | 删除内容失败 |
MEDIA_LIST_ERROR | 500 | 列出媒体失败 |
MEDIA_CREATE_ERROR | 500 | 创建媒体失败 |
SCHEMA_ERROR | 400 | 架构操作失败 |
DUPLICATE_SLUG | 409 | Slug 已存在 |
RESERVED_SLUG | 400 | Slug 是保留的 |
GET /_emdash/api/search?q=hello+world| 参数 | 类型 | 描述 |
|---|---|---|
q | string | 搜索查询(必需) |
collections | string | 逗号分隔的集合 slugs |
status | string | 按状态筛选(默认:published) |
limit | number | 最大结果数(默认:20) |
cursor | string | 分页游标 |
{ "results": [ { "collection": "posts", "id": "01HXK5MZSN...", "slug": "hello-world", "title": "Hello World", "snippet": "...this is a <mark>hello</mark> <mark>world</mark> example...", "score": 0.95 } ], "nextCursor": "eyJvZmZzZXQiOjIwfQ"}GET /_emdash/api/search/suggest?q=hel&limit=5返回前缀匹配的标题用于自动补全。
重建搜索索引
Section titled “重建搜索索引”POST /_emdash/api/search/rebuild为所有或特定集合重建 FTS 索引。
GET /_emdash/api/search/stats返回每个集合的索引文档数量。
GET /_emdash/api/sectionsGET /_emdash/api/sections?source=themeGET /_emdash/api/sections?search=newsletterGET /_emdash/api/sections/:slugPOST /_emdash/api/sectionsContent-Type: application/json
{ "slug": "my-section", "title": "My Section", "keywords": ["keyword1"], "content": [...]}PUT /_emdash/api/sections/:slugDELETE /_emdash/api/sections/:slug获取所有设置
Section titled “获取所有设置”GET /_emdash/api/settingsPOST /_emdash/api/settingsContent-Type: application/json
{ "siteTitle": "My Site", "tagline": "A great site", "postsPerPage": 10}GET /_emdash/api/menusGET /_emdash/api/menus/:namePOST /_emdash/api/menusContent-Type: application/json
{ "name": "footer", "label": "Footer Navigation"}PUT /_emdash/api/menus/:nameDELETE /_emdash/api/menus/:namePOST /_emdash/api/menus/:name/itemsContent-Type: application/json
{ "type": "page", "referenceCollection": "pages", "referenceId": "page_about", "label": "About Us"}重新排序菜单项
Section titled “重新排序菜单项”POST /_emdash/api/menus/:name/reorderContent-Type: application/json
{ "items": [ { "id": "item_1", "parentId": null, "sortOrder": 0 }, { "id": "item_2", "parentId": null, "sortOrder": 1 }, { "id": "item_3", "parentId": "item_2", "sortOrder": 0 } ]}列出分类法定义
Section titled “列出分类法定义”GET /_emdash/api/taxonomiesPOST /_emdash/api/taxonomiesContent-Type: application/json
{ "name": "genre", "label": "Genres", "labelSingular": "Genre", "hierarchical": true, "collections": ["books", "movies"]}GET /_emdash/api/taxonomies/:name/termsPOST /_emdash/api/taxonomies/:name/termsContent-Type: application/json
{ "slug": "tutorials", "label": "Tutorials", "parentId": "term_abc", "description": "How-to guides"}PUT /_emdash/api/taxonomies/:name/terms/:slugDELETE /_emdash/api/taxonomies/:name/terms/:slug设置条目术语
Section titled “设置条目术语”POST /_emdash/api/content/:collection/:id/terms/:taxonomyContent-Type: application/json
{ "termIds": ["term_news", "term_featured"]}小部件区域端点
Section titled “小部件区域端点”列出小部件区域
Section titled “列出小部件区域”GET /_emdash/api/widget-areas获取小部件区域
Section titled “获取小部件区域”GET /_emdash/api/widget-areas/:name创建小部件区域
Section titled “创建小部件区域”POST /_emdash/api/widget-areasContent-Type: application/json
{ "name": "sidebar", "label": "Main Sidebar", "description": "Appears on posts"}删除小部件区域
Section titled “删除小部件区域”DELETE /_emdash/api/widget-areas/:namePOST /_emdash/api/widget-areas/:name/widgetsContent-Type: application/json
{ "type": "content", "title": "About", "content": [...]}PUT /_emdash/api/widget-areas/:name/widgets/:idDELETE /_emdash/api/widget-areas/:name/widgets/:id重新排序小部件
Section titled “重新排序小部件”POST /_emdash/api/widget-areas/:name/reorderContent-Type: application/json
{ "widgetIds": ["widget_1", "widget_2", "widget_3"]}用户管理端点
Section titled “用户管理端点”GET /_emdash/api/admin/usersGET /_emdash/api/admin/users?role=40GET /_emdash/api/admin/users?search=johnGET /_emdash/api/admin/users/:idPATCH /_emdash/api/admin/users/:idContent-Type: application/json
{ "name": "John Doe", "role": 40}POST /_emdash/api/admin/users/:id/enablePOST /_emdash/api/admin/users/:id/disableGET /_emdash/api/setup/status返回设置是否完成以及是否存在用户。
通行密钥登录
Section titled “通行密钥登录”POST /_emdash/api/auth/passkey/options获取 WebAuthn 认证选项。
POST /_emdash/api/auth/passkey/verifyContent-Type: application/json
{ "id": "credential-id", "rawId": "...", "response": {...}, "type": "public-key"}验证通行密钥并创建会话。
POST /_emdash/api/auth/magic-link/sendContent-Type: application/json
{ "email": "user@example.com"}GET /_emdash/api/auth/magic-link/verify?token=xxxPOST /_emdash/api/auth/logoutGET /_emdash/api/auth/mePOST /_emdash/api/auth/inviteContent-Type: application/json
{ "email": "newuser@example.com", "role": 30}通行密钥管理
Section titled “通行密钥管理”GET /_emdash/api/auth/passkey列出用户的通行密钥。
POST /_emdash/api/auth/passkey/register/optionsPOST /_emdash/api/auth/passkey/register/verify注册新的通行密钥。
PATCH /_emdash/api/auth/passkey/:idContent-Type: application/json
{ "name": "MacBook Pro"}重命名通行密钥。
DELETE /_emdash/api/auth/passkey/:id删除通行密钥。
分析 WordPress 导出
Section titled “分析 WordPress 导出”POST /_emdash/api/import/wordpress/analyzeContent-Type: multipart/form-data
file: <WXR file>执行 WordPress 导入
Section titled “执行 WordPress 导入”POST /_emdash/api/import/wordpress/executeContent-Type: application/json
{ "analysisId": "...", "options": { "includeMedia": true, "includeTaxonomies": true, "includeMenus": true }}API 端点可能会根据部署配置进行速率限制。当被限制时,响应包含:
HTTP/1.1 429 Too Many RequestsRetry-After: 60该 API 支持浏览器请求的 CORS。请在您的部署中配置允许的来源。