> ## 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 的 lease。

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

为 work item 发送 heartbeat，并延长 worker lease。第一次成功 heartbeat 会将 `starting` item 推进到 `active`。

建议使用 `expected_last_heartbeat` 做乐观 lease 所有权校验。如果该 item 已被其他 worker 接管，接口会返回 412。

## 路径参数

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

## 请求头

| 头部              | 必选 | 说明                  |
| --------------- | -- | ------------------- |
| `Authorization` | 是  | `Bearer $QODER_PAT` |

## 查询参数

| 参数                        | 类型      | 必选 | 说明                                                 |
| ------------------------- | ------- | -- | -------------------------------------------------- |
| `expected_last_heartbeat` | string  | 否  | `NO_HEARTBEAT` 或服务端上一次返回的 heartbeat 时间戳。省略时表示无条件更新 |
| `desired_ttl_seconds`     | integer | 否  | 期望的 lease TTL。必须为正整数；服务端会将接受值限制在 10 到 600 秒范围内     |

## 示例请求

```bash theme={null}
curl -X POST "https://api.qoder.com/api/v1/cloud/environments/env_019e64e01a137caf953ac2ac7b42ec5c/work/work_019f3be4fd2475d9a784bf2c739e1194/heartbeat?expected_last_heartbeat=NO_HEARTBEAT&desired_ttl_seconds=60" \
  -H "Authorization: Bearer $QODER_PAT"
```

## 示例响应

**HTTP 200 OK**

```json theme={null}
{
  "type": "work_heartbeat",
  "last_heartbeat": "2026-07-01T08:15:06.120394Z",
  "lease_extended": true,
  "state": "active",
  "ttl_seconds": 60
}
```

## 响应字段

返回 [Work heartbeat 对象](/zh/cloud-agents/api/environments/work/schemas#work-heartbeat-对象)。

## 错误码

| HTTP | type                        | 触发条件                                                          |
| ---- | --------------------------- | ------------------------------------------------------------- |
| 400  | `invalid_request_error`     | `desired_ttl_seconds` 不是正整数                                   |
| 400  | `invalid_request_error`     | `expected_last_heartbeat` 既不是 `NO_HEARTBEAT`，也不是 RFC 3339 时间戳 |
| 400  | `invalid_request_error`     | Environment 不是 `self_hosted`                                  |
| 401  | `authentication_error`      | PAT 无效或过期                                                     |
| 403  | `permission_error`          | 无权限执行此操作                                                      |
| 404  | `not_found_error`           | Environment 或 work item 不存在                                   |
| 409  | `invalid_request_error`     | Work item 为 `queued` 或 `stopped`，不能 heartbeat                 |
| 412  | `precondition_failed_error` | `expected_last_heartbeat` 与当前 lease owner 不匹配                 |

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

## 相关

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