> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 节点、主题与回复

> 通过 NodeLoc 的 /api/v1 查找节点、浏览主题、发帖和回帖

`/api/v1` 是 NodeLoc 网页和原生 App 共用的站内接口。公开资源可以匿名读取。要读取当前用户的权限或发送内容，需要通过本站会话认证。以下命令用原生 App 同类的会话令牌，通过 `Authorization: Bearer` 发送。浏览器同源请求可以沿用 `nl_session` Cookie。开放登录签发的第三方 OAuth 令牌用于获取授权的账号资料。见 [v1 概览](https://docs.nodeloc.com/api-reference/v1/overview.md)、[账号与认证](https://docs.nodeloc.com/api-reference/v1/accounts.md) 和 [开放登录](https://docs.nodeloc.com/api-reference/introduction.md)。

以下示例使用本地站点。将 `BASE_URL`、`NODE_SLUG` 和 `TOPIC_ID` 替换为你正在调试的站点及资源。需要登录的示例假定你已设置环境变量 `NODELOC_SESSION_TOKEN`。接口路径无需添加界面语言前缀。

```bash theme={null}
BASE_URL='http://127.0.0.1:8090'
NODE_SLUG='vps'
TOPIC_ID=2
```

## 查找节点

用 `GET /api/v1/nodes` 列出可见节点。使用 `q` 搜索，`cursor=` 从第一批开始。把 `next_cursor` 原样传给下一次请求的 `cursor`，并保持搜索条件不变。末尾省略 `next_cursor`。也可以使用 `page=1` 及返回的 `next_page` 按页码翻页。每页默认 20 条，`limit` 支持 1–100。一次请求使用一种翻页方式。不带任何分页或搜索参数时，响应只有 `nodes`。

```bash theme={null}
curl --silent --show-error --fail-with-body --get "$BASE_URL/api/v1/nodes" \
  --data-urlencode "q=$NODE_SLUG" \
  --data-urlencode 'cursor=' \
  --data-urlencode 'limit=10'
```

末页响应摘录。

```json theme={null}
{
  "nodes": [
    {
      "id": 7,
      "parent_id": 1,
      "slug": "vps",
      "name": "VPS",
      "visibility": "public",
      "joined": false
    }
  ],
  "per_page": 10
}
```

节点详情使用 `slug`。发帖前携带当前会话读取详情，检查 `permissions.create_topic` 和 `posting`。`min_tags` 是最少标签数，`tags_restricted` 为真时只能使用 `tag_options` 中的标签。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  "$BASE_URL/api/v1/nodes/$NODE_SLUG"
```

已登录且有发帖和回帖权限时，节点详情的响应摘录。

```json theme={null}
{
  "node": {"id": 7, "slug": "vps", "joined": true},
  "posting": {
    "slow_mode_seconds": 0,
    "min_tags": 1,
    "tags_restricted": false,
    "tag_options": ["求助", "测评", "优惠"]
  },
  "permissions": {"create_topic": true, "reply": true, "moderate": false}
}
```

## 浏览主题与帖子

`GET /api/v1/topics` 不加筛选时，按最后活动倒序返回首页主题。使用 `node` 或 `tag` 缩小范围。主题列表、主题详情和回复列表的 `limit` 均为 1–50，默认 30。

| 列表方式 | 请求参数 | 下一页 |
| - | - | - |
| 最新或新鲜 | 默认按最后活动排序，`sort=fresh` 按发帖时间排序。可用 `node`、`tag`、`joined=1` 或 `featured=1` 筛选 | 将 `next_cursor` 作为 `cursor`，保留原筛选和排序。无下一页时为 `null` |
| 热门或上升中 | `sort=hot` 或 `sort=rising`。可同时使用 `node` 和 `tag` | 将 `next_offset` 作为 `offset`，无下一页时为 `null` |

```bash theme={null}
curl --silent --show-error --fail-with-body --get "$BASE_URL/api/v1/topics" \
  --data-urlencode "node=$NODE_SLUG" \
  --data-urlencode 'sort=fresh' \
  --data-urlencode 'limit=10'
```

从 `topics[].id` 取主题 ID。主题详情返回 `topic`、一页 `posts`、`next_after` 和当前读者的 `permissions`。帖子按楼层顺序排列，第一楼是首帖。将 `next_after` 原样作为下一页的 `after`。值为 `null` 时已到末页。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  "$BASE_URL/api/v1/topics/$TOPIC_ID?after=0&limit=1"
```

匿名读取主题详情的响应摘录。

```json theme={null}
{
  "topic": {
    "id": 2,
    "node_slug": "vps",
    "title": "年付 20 美元以内的 VPS 推荐",
    "reply_count": 4
  },
  "posts": [
    {
      "id": 6,
      "post_number": 1,
      "reply_to_post_number": null,
      "kind": "post",
      "html": "<p>想找一台用来跑探针的小机器，有什么推荐？</p>"
    }
  ],
  "next_after": 1,
  "permissions": {
    "reply": false,
    "moderate": false
  }
}
```

只查看回帖时，使用 `GET /api/v1/topics/{id}/replies?sort=best&offset=0&limit=10`。它排除首帖和系统操作记录，`sort` 支持 `best`、`new`、`old` 和 `controversial`。继续翻页时使用 `next_offset`，末页省略该字段。

## 发布主题

将节点 `slug` 写入 `node`，正文用 Markdown 填入 `raw`。标题需要 6–255 个字符，首帖正文至少 8 个字符。按目标节点的规则填写 `tags`。如果发到其他节点，修改下面 JSON 中的 `node` 和 `tags`。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  --request POST "$BASE_URL/api/v1/topics" \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{
    "node": "vps",
    "title": "年付 VPS 选购经验交流",
    "raw": "我正在比较两款 VPS，想了解大家实际使用时遇到的**网络延迟**。",
    "tags": ["求助"]
  }'
```

成功创建返回 HTTP 201。保存返回的 `topic.id`，可用来打开主题或发送回复。`held` 为真表示内容待审核，通过后才公开显示。

```json theme={null}
{
  "topic": {"id": 42, "title": "年付 VPS 选购经验交流"},
  "post": {
    "id": 87,
    "post_number": 1,
    "html": "<p>我正在比较两款 VPS，想了解大家实际使用时遇到的<strong>网络延迟</strong>。</p>"
  },
  "held": false
}
```

## 回复主题或某一楼

使用登录会话读取主题详情并检查 `permissions.reply`。写入 `raw` 后可以直接回复主题，也可以在 `reply_to_post_number` 指定同一主题的楼层。直接回复主题时省略该字段或传 `0`。`post_number` 是该主题内的楼层号，`id` 是帖子的全站编号。

将 `TOPIC_ID` 设为上一条请求返回的 `topic.id`。下例回复该主题的首帖。

```bash theme={null}
TOPIC_ID='REPLACE_WITH_TOPIC_ID'
curl --silent --show-error --fail-with-body \
  --request POST "$BASE_URL/api/v1/topics/$TOPIC_ID/posts" \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{"raw":"建议先测试晚高峰延迟。","reply_to_post_number":1}'
```

成功创建返回 HTTP 201。

```json theme={null}
{
  "post": {
    "id": 88,
    "post_number": 2,
    "reply_to_post_number": 1,
    "html": "<p>建议先测试晚高峰延迟。</p>",
    "created_at": "2026-11-29T02:15:00Z"
  },
  "held": false
}
```

## 处理权限与错误

列表与详情按读者的可见范围返回数据。主题标题可能出现在列表中，但正文仍受登录或段位门槛限制。发帖和回帖会重新检查权限、账号状态、节点规则及限流。

| HTTP 状态 | 示例 `error.code` | 含义 |
| - | - | - |
| 400 | `invalid_cursor`、`invalid_json` | 分页游标或 JSON 不合要求。该校验也会拒绝未知的 JSON 字段 |
| 401 | `unauthorized` | 写入需要有效会话 |
| 403 | `login_required`、`trust_level_too_low`、`members_only`、`topic_closed` | 阅读、发帖或回复未满足权限要求 |
| 404 | `not_found`、`merged` | 资源不存在、不可见，或主题已合并。可见的合并去向由 `error.merged_into` 返回 |
| 422 | `invalid` | 查看 `error.fields` 定位不合要求的标题、正文、标签或回复楼层 |
| 429 | `rate_limited`、`slow_mode` | 请求过快或达到限额。若响应带 `Retry-After`，按它给出的秒数等待 |

用 HTTP 状态与 `error.code` 处理分支，`error.message` 和 `error.fields` 的值用于显示，可能随语言变化。下例中的合并去向是原主题首帖的新位置。

```json theme={null}
{
  "error": {
    "code": "merged",
    "message": "这个主题已经合并到另一个主题",
    "merged_into": {"topic_id": 43, "post_number": 7}
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.