# 运行取数任务

用同一接口运行三种平台任务，返回标准化列、结果行、提示和实际上游请求数。

POST `/api/run`

- **认证** 示例可匿名运行。实时取数使用控制台会话 Cookie 或获准运行该任务的 Social2api API Key。
- **成功状态** `200`
- **计费与限额** 示例使用合成数据，上游请求数为 0。实时任务按实际发出的上游请求计入每日限额，包括失败、重试或切换数据源后的请求。在 social2api.com，每次尝试扣 1 次预付额度。
- **分页** 服务在同一次调用内部处理可用的上游分页或关键词分批，响应没有客户端游标。

## 请求参数

| 参数 | 类型 | 位置 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `Authorization` | Bearer &lt;API key&gt; | `header` | 否 | 实时调用可用站内 API Key 进行认证。示例可省略。 |
| `X-API-Key` | string | `header` | 否 | Authorization 的替代项。若同时提供，两者必须是同一密钥。 |
| `task` | rednote \| douyin \| channels | `body` | 是 | 待运行的平台任务。 |
| `mode` | demo \| live | `body` | 是 | 选择合成样例或实时数据。 |
| `params` | object | `body` | 是 | 任务参数对象。每个平台有各自的字段。 |
| `maxRequests` | integer | `body` | 否 | 实时任务上游请求预算，1–36 次，默认 36。样例模式忽略此字段。 |

## 请求示例

### 免费示例

**cURL**

```bash
curl -i -X POST 'https://social2api.com/api/run' \
  -H 'Content-Type: application/json' \
  --data-binary '{
  "task": "rednote",
  "mode": "demo",
  "params": {
    "query": "城市骑行",
    "since": "2026-01-15",
    "until": "2026-01-15",
    "limit": 1
  }
}'
```

**Python**

```python
from urllib.error import HTTPError
from urllib.request import Request, urlopen

request = Request(
    "https://social2api.com/api/run",
    method="POST",
    headers={
        "Content-Type": "application/json",
    },
    data="{\"task\":\"rednote\",\"mode\":\"demo\",\"params\":{\"query\":\"城市骑行\",\"since\":\"2026-01-15\",\"until\":\"2026-01-15\",\"limit\":1}}".encode("utf-8"),
)
send = urlopen
try:
    with send(request) as response:
        print(response.status, response.read().decode("utf-8"))
except HTTPError as error:
    print(error.code, error.read().decode("utf-8"))
```

**JavaScript**

```javascript
const response = await fetch("/api/run", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "task": "rednote",
    "mode": "demo",
    "params": {
      "query": "城市骑行",
      "since": "2026-01-15",
      "until": "2026-01-15",
      "limit": 1
    }
  }),
});
console.log(response.status, await response.text());
```

### 实时数据

先在本机设置 SOCIAL2API\_API\_KEY 环境变量。JavaScript 示例使用 Node.js 运行。

实时请求会消耗可用的上游调用额度。

**cURL**

```bash
curl -i -X POST 'https://social2api.com/api/run' \
  -H "Authorization: Bearer $SOCIAL2API_API_KEY" \
  -H 'Content-Type: application/json' \
  --data-binary '{
  "task": "rednote",
  "mode": "live",
  "params": {
    "query": "城市骑行",
    "since": "2026-01-15",
    "until": "2026-01-15",
    "limit": 1
  }
}'
```

**Python**

```python
import os
from urllib.error import HTTPError
from urllib.request import Request, urlopen

request = Request(
    "https://social2api.com/api/run",
    method="POST",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer " + os.environ["SOCIAL2API_API_KEY"],
    },
    data="{\"task\":\"rednote\",\"mode\":\"live\",\"params\":{\"query\":\"城市骑行\",\"since\":\"2026-01-15\",\"until\":\"2026-01-15\",\"limit\":1}}".encode("utf-8"),
)
send = urlopen
try:
    with send(request) as response:
        print(response.status, response.read().decode("utf-8"))
except HTTPError as error:
    print(error.code, error.read().decode("utf-8"))
```

**JavaScript**

```javascript
const response = await fetch("https://social2api.com/api/run", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer " + process.env.SOCIAL2API_API_KEY,
  },
  body: JSON.stringify({
    "task": "rednote",
    "mode": "live",
    "params": {
      "query": "城市骑行",
      "since": "2026-01-15",
      "until": "2026-01-15",
      "limit": 1
    }
  }),
});
console.log(response.status, await response.text());
```

- [小红书笔记导出](<https://social2api.com/tools/xiaohongshu-notes-export/>)
- [抖音指数查询](<https://social2api.com/tools/douyin-keyword-index/>)
- [视频号评论导出](<https://social2api.com/tools/wechat-channels-comments-export/>)

[在工作台试用](<https://social2api.com/#workspace>)

## 响应

**200 · 示例响应**

```json
{
  "task": "rednote",
  "mode": "demo",
  "provider": "样例数据",
  "columns": [
    {
      "key": "title",
      "label": "笔记"
    },
    {
      "key": "author",
      "label": "作者"
    },
    {
      "key": "publishedAt",
      "label": "发布时间"
    },
    {
      "key": "likes",
      "label": "点赞"
    },
    {
      "key": "comments",
      "label": "评论"
    },
    {
      "key": "snippet",
      "label": "摘要"
    },
    {
      "key": "url",
      "label": "原帖"
    },
    {
      "key": "id",
      "label": "笔记 ID"
    }
  ],
  "rows": [
    {
      "id": "demo-note-1",
      "title": "城市骑行｜入门经验整理",
      "author": "周末记录员",
      "publishedAt": "2026-01-15",
      "likes": 824,
      "comments": 32,
      "url": null,
      "snippet": "示例摘要：从城市骑行的实际场景出发，整理体验和使用建议。"
    }
  ],
  "warnings": [
    "这些是合成样例，供查看字段格式和测试导出。"
  ],
  "requestCount": 0
}
```

**错误示例**

```json
{
  "error": "请选择取数任务。",
  "requestCount": 0
}
```

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `task` | rednote \| douyin \| channels | 实际执行的平台任务。 |
| `mode` | demo \| live | 取数模式。 |
| `provider` | string | 数据源的显示名称。示例固定为“样例数据”。 |
| `columns` | object\[\] | 按显示顺序排列的列定义。 |
| `columns[].key` | string | 在结果行中查找对应值的键。 |
| `columns[].label` | string | 字段显示名称。 |
| `rows` | object\[\] | 标准化结果行，字段随任务变化。 |
| `warnings` | string\[\] | 样例提示、上游缺失字段或部分结果等信息。 |
| `requestCount` | integer | 该任务实际尝试的上游请求数。示例为 0。 |

## 错误状态

| HTTP | 原因与处理 |
| --- | --- |
| `400` | 请求格式或字段无效。 |
| `401` | 会话无效或收到 invalid\_api\_key（密钥无效、到期或撤销）。 |
| `402` | 预付实时调用额度不足（insufficient\_paid\_credits）。 |
| `403` | 来源校验失败，或收到 api\_key\_permission\_denied（密钥未授权此任务）。 |
| `404` | 供应商未找到指定对象。 |
| `405` | 请求方法不受支持。 |
| `409` | 该账号已有一项实时任务在运行。 |
| `413` | 请求体超出 16,000 字节。 |
| `415` | 需要 application/json 请求体。 |
| `422` | 适用数据源无法识别该账号、链接或其他目标，或供应商拒绝该操作。 |
| `429` | 达到本次请求预算、账户每日额度或密钥每日额度（api\_key\_daily\_limit）。 |
| `502` | 上游授权、响应解析或查询执行失败。 |
| `503` | 无可用数据源、供应商限流、额度不足或服务暂不可用。 |
| `504` | 请求或结果等待超时。 |

## 使用细节

-   API Key 可设置可用工具和各自的每日限额。使用 Key 的实时请求同时计入所属账号与该 Key 的额度。
-   返回 200 时仍可能只有部分结果。保留 rows，并检查 warnings 后再决定是否重试。
-   每日调用额度按 UTC 00:00 重置。服务器在能完成查询的已配置供应商之间切换，一次任务共享上游请求预算和 95 秒总超时。
-   错误响应通常包含 error 和 requestCount。API Key 错误还包含 code，方法错误和跨站请求只返回 error。

## 同组接口

-   [GET 检查数据源配置](<https://social2api.com/docs/api/status/>)
-   [POST 调用 MCP 服务](<https://social2api.com/docs/api/mcp/>)
-   [GET 读取 OAuth 授权服务元数据](<https://social2api.com/docs/api/oauth-server-metadata/>)
-   [GET 读取 OAuth 受保护资源元数据](<https://social2api.com/docs/api/oauth-resource-metadata/>)
-   [GET 读取 MCP 资源的 OAuth 元数据](<https://social2api.com/docs/api/oauth-mcp-metadata/>)
-   [POST 注册 MCP OAuth 客户端](<https://social2api.com/docs/api/oauth-register/>)
-   [GET 启动 MCP OAuth 授权](<https://social2api.com/docs/api/oauth-authorize-start/>)
-   [POST 提交 MCP OAuth 授权决定](<https://social2api.com/docs/api/oauth-authorize-consent/>)
-   [POST 兑换或刷新 MCP OAuth 令牌](<https://social2api.com/docs/api/oauth-token/>)

规范页面：<https://social2api.com/docs/api/run/>
