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

# 停止 Work

> 请求或确认停止 self-hosted work item。

`POST /api/v1/cloud/environments/{environment_id}/work/{work_id}/stop`

停止 work item。请求体可省略；空 body 或 `{}` 都表示默认的 `force=false` 请求，也就是 graceful stop（非强制停止）。

对 `starting` 或 `active` item，`force=false` 会将 item 推进到 `stopping`。worker 应 drain 当前 work，然后再次调用该接口确认完成并推进到 `stopped`。`force=true` 会将任意 live item 直接推进到 `stopped`。

## 路径参数

| 参数               | 类型     | 说明                        |
| ---------------- | ------ | ------------------------- |
| `environment_id` | string | Environment ID，前缀为 `env_` |
| `work_id`        | string | Work item ID，前缀为 `work_`  |

## 请求头

| 头部              | 必选 | 说明                            |
| --------------- | -- | ----------------------------- |
| `Authorization` | 是  | `Bearer $QODER_PAT`           |
| `Content-Type`  | 否  | 发送 body 时为 `application/json` |

## 请求体

| 字段      | 类型      | 必选 | 默认值     | 说明                                  |
| ------- | ------- | -- | ------- | ----------------------------------- |
| `force` | boolean | 否  | `false` | 为 `true` 时不等待 graceful drain，直接强制停止 |

## 示例请求：`force=false`

```bash theme={null}
curl -X POST "https://api.qoder.com/api/v1/cloud/environments/env_019e64e01a137caf953ac2ac7b42ec5c/work/work_019f3be4fd2475d9a784bf2c739e1194/stop" \
  -H "Authorization: Bearer $QODER_PAT" \
  -H "Content-Type: application/json" \
  -d '{}'
```

## 示例请求：`force=true`

```bash theme={null}
curl -X POST "https://api.qoder.com/api/v1/cloud/environments/env_019e64e01a137caf953ac2ac7b42ec5c/work/work_019f3be4fd2475d9a784bf2c739e1194/stop" \
  -H "Authorization: Bearer $QODER_PAT" \
  -H "Content-Type: application/json" \
  -d '{"force": true}'
```

## 示例响应

**HTTP 200 OK**

```json theme={null}
{
  "id": "work_019f3be4fd2475d9a784bf2c739e1194",
  "type": "work",
  "environment_id": "env_019e64e01a137caf953ac2ac7b42ec5c",
  "data": {
    "type": "session",
    "id": "sess_019f3be3fa66750bb9a1fbcde85b5fe1"
  },
  "state": "stopping",
  "created_at": "2026-07-01T08:15:01Z",
  "acknowledged_at": "2026-07-01T08:15:04Z",
  "started_at": "2026-07-01T08:15:06Z",
  "latest_heartbeat_at": "2026-07-01T08:16:06.120394Z",
  "stop_requested_at": "2026-07-01T08:16:30Z",
  "stopped_at": null,
  "metadata": {}
}
```

## 响应字段

返回更新后的 [Work item 对象](/zh/cloud-agents/api/environments/work/schemas#work-item-对象)。

## 错误码

| HTTP | type                    | 触发条件                         |
| ---- | ----------------------- | ---------------------------- |
| 400  | `invalid_request_error` | 请求体不是合法 JSON                 |
| 400  | `invalid_request_error` | Environment 不是 `self_hosted` |
| 401  | `authentication_error`  | PAT 无效或过期                    |
| 403  | `permission_error`      | 无权限执行此操作                     |
| 404  | `not_found_error`       | Environment 或 work item 不存在  |

完整错误信封格式见 [错误参考](/zh/cloud-agents/api/conventions/errors)。

## 状态变化

`force=false` 表示空 body、`{}` 或 `{"force": false}`。`force=true` 表示 `{"force": true}`。

| `force` | 当前状态                  | 结果                  |
| ------- | --------------------- | ------------------- |
| `false` | `queued`              | `stopped`           |
| `false` | `starting` 或 `active` | `stopping`          |
| `false` | `stopping`            | `stopped`           |
| `false` | `stopped`             | 返回现有 `stopped` item |
| `true`  | 任意 live 状态            | `stopped`           |

## 相关

<CardGroup cols={2}>
  <Card title="云端环境" icon="server" href="/zh/cloud-agents/environments">
    选择 Agent 运行的容器、网络与依赖。
  </Card>
</CardGroup>
