REST API 參考

本頁內容

EmDash 在 /_emdash/api/ 公開 REST API,用於內容管理、媒體上傳和結構描述操作。

驗證

API 請求需要透過 Bearer 權杖進行驗證:

Authorization: Bearer <token>

透過管理介面或以程式方式產生權杖。

回應格式

所有回應遵循一致的格式:

// 成功
{
  "success": true,
  "data": { ... }
}

// 錯誤
{
  "success": false,
  "error": {
    "code": "ERROR_CODE",
    "message": "Human-readable message",
    "details": { ... }
  }
}

內容端點

列出內容

GET /_emdash/api/content/:collection

參數

參數類型說明
collectionstring集合 slug(路徑)
cursorstring分頁游標(查詢)
limitnumber每頁項目數(查詢,預設:50)
statusstring依狀態篩選(查詢)
orderBystring排序欄位(查詢)
orderstring排序方向:ascdesc(查詢)

回應

{
  "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/:collection
Content-Type: application/json

請求主體

{
  "data": {
    "title": "New Post",
    "content": [...]
  },
  "slug": "new-post",
  "status": "draft"
}

回應

{
  "success": true,
  "data": {
    "item": { ... }
  }
}

更新內容

PUT /_emdash/api/content/:collection/:id
Content-Type: application/json

請求主體

{
	"data": {
		"title": "Updated Title"
	},
	"status": "published"
}

刪除內容

DELETE /_emdash/api/content/:collection/:id

回應

{
	"success": true,
	"data": {
		"success": true
	}
}

媒體端點

列出媒體

GET /_emdash/api/media

參數

參數類型說明
cursorstring分頁游標
limitnumber每頁項目數(預設:20)
mimeTypestring依 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/:id

建立媒體

POST /_emdash/api/media
Content-Type: application/json

請求主體

{
	"filename": "photo.jpg",
	"mimeType": "image/jpeg",
	"size": 102400,
	"width": 1920,
	"height": 1080,
	"storageKey": "uploads/photo.jpg"
}

更新媒體

PUT /_emdash/api/media/:id
Content-Type: application/json

請求主體

{
	"alt": "Photo description",
	"caption": "Photo caption"
}

刪除媒體

DELETE /_emdash/api/media/:id

取得媒體檔案

GET /_emdash/api/media/file/:key

提供實際檔案內容。僅限本地儲存。

修訂版本端點

列出修訂版本

GET /_emdash/api/content/:collection/:entryId/revisions

參數

參數類型說明
limitnumber最大回傳修訂版本數(預設:50)

回應

{
  "success": true,
  "data": {
    "items": [
      {
        "id": "01HXK5MZSN...",
        "collection": "posts",
        "entryId": "01HXK5MZSN...",
        "data": { ... },
        "createdAt": "2025-01-24T12:00:00Z"
      }
    ],
    "total": 5
  }
}

取得修訂版本

GET /_emdash/api/revisions/:revisionId

還原修訂版本

POST /_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

參數

參數類型說明
includeFieldsboolean是否包含欄位定義(查詢)

建立集合

POST /_emdash/api/schema/collections
Content-Type: application/json

請求主體

{
	"slug": "products",
	"label": "Products",
	"labelSingular": "Product",
	"description": "Product catalog",
	"supports": ["drafts", "revisions"]
}

更新集合

PATCH /_emdash/api/schema/collections/:slug
Content-Type: application/json

刪除集合

DELETE /_emdash/api/schema/collections/:slug

參數

參數類型說明
forceboolean即使集合有內容也強制刪除(查詢)

列出欄位

GET /_emdash/api/schema/collections/:slug/fields

建立欄位

POST /_emdash/api/schema/collections/:slug/fields
Content-Type: application/json

請求主體

{
	"slug": "price",
	"label": "Price",
	"type": "number",
	"required": true,
	"validation": {
		"min": 0
	}
}

更新欄位

PATCH /_emdash/api/schema/collections/:collectionSlug/fields/:fieldSlug
Content-Type: application/json

刪除欄位

DELETE /_emdash/api/schema/collections/:collectionSlug/fields/:fieldSlug

重新排序欄位

POST /_emdash/api/schema/collections/:slug/fields/reorder
Content-Type: application/json

請求主體

{
	"fieldSlugs": ["title", "content", "author", "publishedAt"]
}

結構描述匯出

匯出結構描述(JSON)

GET /_emdash/api/schema
Accept: application/json

匯出結構描述(TypeScript)

GET /_emdash/api/schema?format=typescript
Accept: text/typescript

回傳所有集合的 TypeScript 介面。

外掛端點

列出外掛

GET /_emdash/api/plugins

取得外掛

GET /_emdash/api/plugins/:pluginId

啟用外掛

POST /_emdash/api/plugins/:pluginId/enable

停用外掛

POST /_emdash/api/plugins/:pluginId/disable

錯誤代碼

代碼HTTP 狀態碼說明
NOT_FOUND404找不到資源
VALIDATION_ERROR400無效的輸入資料
UNAUTHORIZED401缺少或無效的權杖
FORBIDDEN403權限不足
CONTENT_LIST_ERROR500列出內容失敗
CONTENT_CREATE_ERROR500建立內容失敗
CONTENT_UPDATE_ERROR500更新內容失敗
CONTENT_DELETE_ERROR500刪除內容失敗
MEDIA_LIST_ERROR500列出媒體失敗
MEDIA_CREATE_ERROR500建立媒體失敗
SCHEMA_ERROR400結構描述操作失敗
DUPLICATE_SLUG409Slug 已存在
RESERVED_SLUG400Slug 為保留字

搜尋端點

全域搜尋

GET /_emdash/api/search?q=hello+world

參數

參數類型說明
qstring搜尋查詢(必填)
collectionsstring以逗號分隔的集合 slug
statusstring依狀態篩選(預設:published)
limitnumber最大結果數(預設:20)
cursorstring分頁游標

回應

{
  "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

回傳前綴比對的標題,供自動完成使用。

重建搜尋索引

POST /_emdash/api/search/rebuild

為全部或特定集合重建 FTS 索引。

搜尋統計

GET /_emdash/api/search/stats

回傳每個集合的索引文件計數。

區塊端點

列出區塊

GET /_emdash/api/sections
GET /_emdash/api/sections?source=theme
GET /_emdash/api/sections?search=newsletter

取得區塊

GET /_emdash/api/sections/:slug

建立區塊

POST /_emdash/api/sections
Content-Type: application/json

{
  "slug": "my-section",
  "title": "My Section",
  "keywords": ["keyword1"],
  "content": [...]
}

更新區塊

PUT /_emdash/api/sections/:slug

刪除區塊

DELETE /_emdash/api/sections/:slug

設定端點

取得所有設定

GET /_emdash/api/settings

更新設定

POST /_emdash/api/settings
Content-Type: application/json

{
  "siteTitle": "My Site",
  "tagline": "A great site",
  "postsPerPage": 10
}

選單端點

列出選單

GET /_emdash/api/menus

取得選單

GET /_emdash/api/menus/:name

建立選單

POST /_emdash/api/menus
Content-Type: application/json

{
  "name": "footer",
  "label": "Footer Navigation"
}

更新選單

PUT /_emdash/api/menus/:name

刪除選單

DELETE /_emdash/api/menus/:name

新增選單項目

POST /_emdash/api/menus/:name/items
Content-Type: application/json

{
  "type": "page",
  "referenceCollection": "pages",
  "referenceId": "page_about",
  "label": "About Us"
}

重新排序選單項目

POST /_emdash/api/menus/:name/reorder
Content-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 }
  ]
}

分類法端點

列出分類法定義

GET /_emdash/api/taxonomies

建立分類法

POST /_emdash/api/taxonomies
Content-Type: application/json

{
  "name": "genre",
  "label": "Genres",
  "labelSingular": "Genre",
  "hierarchical": true,
  "collections": ["books", "movies"]
}

列出詞彙

GET /_emdash/api/taxonomies/:name/terms

建立詞彙

POST /_emdash/api/taxonomies/:name/terms
Content-Type: application/json

{
  "slug": "tutorials",
  "label": "Tutorials",
  "parentId": "term_abc",
  "description": "How-to guides"
}

更新詞彙

PUT /_emdash/api/taxonomies/:name/terms/:slug

刪除詞彙

DELETE /_emdash/api/taxonomies/:name/terms/:slug

設定條目詞彙

POST /_emdash/api/content/:collection/:id/terms/:taxonomy
Content-Type: application/json

{
  "termIds": ["term_news", "term_featured"]
}

小工具區域端點

列出小工具區域

GET /_emdash/api/widget-areas

取得小工具區域

GET /_emdash/api/widget-areas/:name

建立小工具區域

POST /_emdash/api/widget-areas
Content-Type: application/json

{
  "name": "sidebar",
  "label": "Main Sidebar",
  "description": "Appears on posts"
}

刪除小工具區域

DELETE /_emdash/api/widget-areas/:name

新增小工具

POST /_emdash/api/widget-areas/:name/widgets
Content-Type: application/json

{
  "type": "content",
  "title": "About",
  "content": [...]
}

更新小工具

PUT /_emdash/api/widget-areas/:name/widgets/:id

刪除小工具

DELETE /_emdash/api/widget-areas/:name/widgets/:id

重新排序小工具

POST /_emdash/api/widget-areas/:name/reorder
Content-Type: application/json

{
  "widgetIds": ["widget_1", "widget_2", "widget_3"]
}

使用者管理端點

列出使用者

GET /_emdash/api/admin/users
GET /_emdash/api/admin/users?role=40
GET /_emdash/api/admin/users?search=john

取得使用者

GET /_emdash/api/admin/users/:id

更新使用者

PATCH /_emdash/api/admin/users/:id
Content-Type: application/json

{
  "name": "John Doe",
  "role": 40
}

啟用使用者

POST /_emdash/api/admin/users/:id/enable

停用使用者

POST /_emdash/api/admin/users/:id/disable

驗證端點

設定狀態

GET /_emdash/api/setup/status

回傳設定是否已完成以及是否存在使用者。

通行密鑰登入

POST /_emdash/api/auth/passkey/options

取得 WebAuthn 驗證選項。

POST /_emdash/api/auth/passkey/verify
Content-Type: application/json

{
  "id": "credential-id",
  "rawId": "...",
  "response": {...},
  "type": "public-key"
}

驗證通行密鑰並建立工作階段。

魔法連結

POST /_emdash/api/auth/magic-link/send
Content-Type: application/json

{
  "email": "[email protected]"
}
GET /_emdash/api/auth/magic-link/verify?token=xxx

登出

POST /_emdash/api/auth/logout

目前使用者

GET /_emdash/api/auth/me

邀請使用者

POST /_emdash/api/auth/invite
Content-Type: application/json

{
  "email": "[email protected]",
  "role": 30
}

通行密鑰管理

GET /_emdash/api/auth/passkey

列出使用者的通行密鑰。

POST /_emdash/api/auth/passkey/register/options
POST /_emdash/api/auth/passkey/register/verify

註冊新通行密鑰。

PATCH /_emdash/api/auth/passkey/:id
Content-Type: application/json

{
  "name": "MacBook Pro"
}

重新命名通行密鑰。

DELETE /_emdash/api/auth/passkey/:id

刪除通行密鑰。

匯入端點

分析 WordPress 匯出檔

POST /_emdash/api/import/wordpress/analyze
Content-Type: multipart/form-data

file: <WXR file>

執行 WordPress 匯入

POST /_emdash/api/import/wordpress/execute
Content-Type: application/json

{
  "analysisId": "...",
  "options": {
    "includeMedia": true,
    "includeTaxonomies": true,
    "includeMenus": true
  }
}

速率限制

API 端點可能依部署設定進行速率限制。當觸發速率限制時,回應包含:

HTTP/1.1 429 Too Many Requests
Retry-After: 60

CORS

API 支援瀏覽器請求的 CORS。在你的部署中設定允許的來源。