API接口设计

RESTful API 设计草案,服务于第三方应用集成。


基础信息

项目内容
协议HTTPS
格式JSON
编码UTF-8
版本v1 (路径级版本控制)
文档OpenAPI 3.0 / Swagger

通用约定

响应格式

{
  "status": "success",
  "data": { ... },
  "pagination": {
    "page": 1,
    "page_size": 20,
    "total": 64
  },
  "links": {
    "self": "/api/v1/hexagrams/1",
    "related": "/api/v1/graph?from=hexagram-001&depth=2"
  }
}

错误格式

{
  "status": "error",
  "code": "NOT_FOUND",
  "message": "卦象 hexagram-065 不存在"
}

通用参数

参数类型默认值说明
pageint1页码
page_sizeint20每页数量(max 100)
langstringzh语言 (zh-CN / en / zh-Hant)
fieldsstring-返回字段过滤 (逗号分隔)
sortstringid排序字段
orderstringasc排序方向 (asc/desc)

端点列表

1. 六十四卦 - /api/v1/hexagrams

GET /api/v1/hexagrams

获取六十四卦列表。

参数类型说明
trigram_upperstring按上卦过滤 (乾/坤/…)
trigram_lowerstring按下卦过滤
wuxingstring按五行过滤 (金/木/水/火/土)
directionstring按方位过滤
keywordstring按关键词搜索
qstring全文检索

GET /api/v1/hexagrams/{id}

获取单卦详情。

GET /api/v1/hexagrams/{id}/variants

获取卦变信息 (综卦、错卦、互卦)。

GET /api/v1/hexagrams/{id}/comments

获取历代注疏。


2. 八卦 - /api/v1/trigrams

GET /api/v1/trigrams

获取八卦列表。

GET /api/v1/trigrams/{id}

获取单卦详情。

GET /api/v1/trigrams/{id}/hexagrams

获取属于该八卦卦宫的所有六十四卦。


3. 概念 - /api/v1/concepts

GET /api/v1/concepts

获取概念列表。

参数类型说明
categorystring按分类过滤 (理论基础/术数/哲学)
depthint概念树层级 (用于上位/下位概念展开)

GET /api/v1/concepts/{id}

获取概念详情 (含经典原文引用)。

GET /api/v1/concepts/{id}/tree?direction=up

获取概念层级关系树。


4. 经典典籍 - /api/v1/classics

GET /api/v1/classics

获取典籍列表。

参数类型说明
dynastystring按朝代过滤
schoolstring按学派过滤 (象数派/义理派/…)
authorstring按作者过滤

GET /api/v1/classics/{id}

获取典籍详情 (含篇章结构)。

GET /api/v1/classics/{id}/content

获取典籍内容 (分页)。

GET /api/v1/classics/{id}/references

获取引用该典籍的所有实体。


5. 人物 - /api/v1/persons

GET /api/v1/persons

获取人物列表。

GET /api/v1/persons/{id}

获取人物详情。

GET /api/v1/persons/{id}/lineage?type=teacher

获取师承谱系。

GET /api/v1/persons/{id}/works

获取人物著作。


6. 术数 - /api/v1/methods

GET /api/v1/methods

获取术数列表。

参数类型说明
typestring按类型过滤 (占卜/风水/命理/…)

GET /api/v1/methods/{id}

获取术数详情 (含操作步骤)。


7. 知识图谱 - /api/v1/graph

GET /api/v1/graph

知识图谱查询接口。

参数类型必需说明
fromstring*起始节点 ID
tostring目标节点 ID (最短路径查询)
depthint遍历深度 (默认 2, max 5)
relationstring关系类型过滤
node_typestring节点类型过滤 (concept/hexagram/person/…)

GET /api/v1/graph/neighbors?id={id}&depth=2

获取某节点的邻域图。


8. 搜索 - /api/v1/search

GET /api/v1/search

参数类型说明
qstring搜索关键词
scopestring搜索范围 (all/hexagrams/concepts/classics/persons)
langstring搜索语言
fuzzybool是否模糊搜索 (默认 true)

9. 日历 - /api/v1/calendar

GET /api/v1/calendar/solar-terms

获取二十四节气列表。

GET /api/v1/calendar/sexagenary?date=2026-05-06

获取指定日期的干支纪日/纪月/纪年信息。


数据格式

Hexagram 响应示例

{
  "id": "hexagram-001",
  "name": "乾卦",
  "symbol": "䷀",
  "order": 1,
  "upper_trigram": { "id": "trigram-001", "name": "乾", "symbol": "☰" },
  "lower_trigram": { "id": "trigram-001", "name": "乾", "symbol": "☰" },
  "gua_ci": "元亨利贞",
  "tuan_ci": "大哉乾元...",
  "xiang_ci": "天行健,君子以自强不息",
  "yao_ci": [
    { "position": 1, "text": "潜龙勿用", "commentary": "..." },
    { "position": 2, "text": "见龙在田", "commentary": "..." }
  ],
  "wuxing": "金",
  "direction": "西北",
  "keywords": ["天", "刚健", "元亨利贞", "龙"],
  "variants": {
    "reverse": { "id": "hexagram-001", "name": "乾卦" },
    "complementary": { "id": "hexagram-002", "name": "坤卦" }
  },
  "sources": [
    {
      "title": "周易·上经",
      "url": "https://ctext.org/book-of-changes/qian1/zh",
      "dynasty": "西周"
    }
  ],
  "links": {
    "self": "/api/v1/hexagrams/hexagram-001",
    "comments": "/api/v1/hexagrams/hexagram-001/comments",
    "graph": "/api/v1/graph?from=hexagram-001&depth=2"
  }
}

认证与限流

项目方案
认证API Key (X-API-Key header)
免费额度1000 req/day
限流100 req/min per IP
跨域CORS 开放 (配置白名单)

SDK 计划

  • JavaScript/TypeScript SDK (@iching/sdk)
  • Python SDK (iching-sdk)
  • 微信小程序 SDK