# 更新自动切换状态

为当前账号开启或关闭自动数据源切换。

PATCH `/api/settings/fallback`

- **认证** 控制台会话 Cookie。
- **成功状态** `200`

## 请求参数

| 参数 | 类型 | 位置 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `Origin` | URI | `header` | 是 | 当前站点的完整来源。 |
| `enabled` | boolean | `body` | 是 | 是否启用自动数据源切换。 |

## 请求示例

JavaScript 示例可在本站已登录的浏览器中运行。cURL 和 Python 示例读取你本机的 cookies.txt Cookie 文件。请将会话 Cookie 视为私密凭据。

**cURL**

```bash
curl -i -X PATCH 'https://social2api.com/api/settings/fallback' \
  --cookie ./cookies.txt \
  -H 'Content-Type: application/json' \
  -H 'Origin: https://social2api.com' \
  --data-binary '{
  "enabled": false
}'
```

**Python**

```python
from urllib.error import HTTPError
from urllib.request import Request, HTTPCookieProcessor, build_opener
from http.cookiejar import MozillaCookieJar

request = Request(
    "https://social2api.com/api/settings/fallback",
    method="PATCH",
    headers={
        "Content-Type": "application/json",
        "Origin": "https://social2api.com",
    },
    data="{\"enabled\":false}".encode("utf-8"),
)
cookies = MozillaCookieJar("./cookies.txt")
cookies.load(ignore_discard=True)
send = build_opener(HTTPCookieProcessor(cookies)).open
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/settings/fallback", {
  method: "PATCH",
  credentials: "same-origin",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "enabled": false
  }),
});
console.log(response.status, await response.text());
```

## 响应

**200 · 示例响应**

```json
{
  "enabled": false,
  "providers": [
    {
      "id": "tikhub",
      "label": "TikHub",
      "configured": false,
      "status": "missing_config",
      "tasks": [
        "rednote",
        "douyin",
        "channels"
      ],
      "cooldownUntil": null,
      "lastError": null
    },
    {
      "id": "dataxapi",
      "label": "DataXAPI",
      "configured": false,
      "status": "missing_config",
      "tasks": [
        "rednote",
        "douyin",
        "channels"
      ],
      "cooldownUntil": null,
      "lastError": null
    },
    {
      "id": "socialtoai",
      "label": "SocialToAI",
      "configured": false,
      "status": "missing_config",
      "tasks": [
        "rednote",
        "douyin",
        "channels"
      ],
      "cooldownUntil": null,
      "lastError": null
    }
  ],
  "events": []
}
```

**错误示例**

```json
{
  "error": "请先登录。"
}
```

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `enabled` | boolean | 该账号是否允许自动切换数据源。 |
| `providers[].id` | string | 供应商 ID。 |
| `providers[].label` | string | 供应商显示名称。 |
| `providers[].configured` | boolean | 当前部署是否配置该供应商。 |
| `providers[].status` | missing\_config \| unchecked \| healthy \| cooldown | 当前实例的供应商状态。 |
| `providers[].tasks` | string\[\] | 该供应商在当前部署启用的任务 ID。 |
| `providers[].cooldownUntil` | ISO 8601 date-time \| null | 当前冷却结束时间。 |
| `providers[].lastError` | string \| null | 最近记录的供应商故障原因。 |
| `events[].id` | string | 切换记录 ID。 |
| `events[].at` | ISO 8601 date-time | 记录时间。 |
| `events[].task` | string | 触发切换的取数任务。 |
| `events[].from` | string | 来源供应商 ID。 |
| `events[].to` | string | 目标供应商 ID。 |
| `events[].reason` | string | 触发切换的原因。 |

## 错误状态

| HTTP | 原因与处理 |
| --- | --- |
| `400` | 请求格式或字段无效。 |
| `403` | 浏览器来源校验未通过。 |
| `401` | 控制台会话缺失、过期或无效。 |
| `405` | 请求方法不受支持。 |
| `503` | 此部署未配置该控制台服务。 |

## 使用细节

-   写请求须包含与站点完全相同的 Origin。
-   默认依次尝试 TikHub、DataXAPI、SocialToAI，跳过未配置、冷却或不支持输入的供应商。关闭自动切换时只尝试第一个已配置且适用的供应商。
-   JSON 请求体最多 1,024 字节且只能包含 enabled。

## 同组接口

-   [GET 读取设置与每日额度](<https://social2api.com/docs/api/settings-get/>)
-   [PATCH 更新偏好和每日额度](<https://social2api.com/docs/api/settings-update/>)
-   [GET 查询上游调用明细](<https://social2api.com/docs/api/settings-usage/>)
-   [GET 读取计费用量摘要](<https://social2api.com/docs/api/settings-billing/>)
-   [GET 查看自动切换状态](<https://social2api.com/docs/api/settings-fallback-get/>)
-   [GET 列出 API Key](<https://social2api.com/docs/api/api-keys-list/>)
-   [POST 创建 API Key](<https://social2api.com/docs/api/api-keys-create/>)
-   [PATCH 更新 API Key](<https://social2api.com/docs/api/api-keys-update/>)
-   [DELETE 撤销 API Key](<https://social2api.com/docs/api/api-keys-revoke/>)

规范页面：<https://social2api.com/docs/api/settings-fallback-update/>
