# 租用

Source: https://bflops.com/docs/api/rentals

启动已报价的机器或集群，查看、延长和停止它。



## 启动租用 [#start-a-rental]

<Endpoint method="POST" path="/api/v1/rentals" />

启动报价的机器或集群。需要设有 **GPU 每日限额**的密钥。

| 字段          | 类型      | 必填 | 说明                                                                                 |
| ----------- | ------- | -- | ---------------------------------------------------------------------------------- |
| `quote_id`  | string  | 是  | 来自 [`POST /quotes`](/docs/api/quotes)，60 秒内有效                                      |
| `ssh_key`   | string  | 是  | 你的 SSH 公钥，一行。参见 [SSH 密钥](/docs/ssh-keys)                                           |
| `app`       | string  | 否  | `pytorch`（默认）、`jupyter`、`comfyui`、`ollama` 或 `stable-diffusion`。参见[应用](/docs/apps) |
| `ttl_hours` | number  | 否  | 多少小时后自动停止：1 到 168，默认 24                                                            |
| `data`      | boolean | 否  | 挂载 [/data](/docs/storage)。仅当报价的 `data_allowed` 为 `true` 时可用                        |

发送 `Idempotency-Key` 头，重试时就不会启动第二台机器。参见[安全重试](/docs/api#safe-retries)。

```bash
curl https://bflops.com/api/v1/rentals \
  -H "Authorization: Bearer $BFLOPS_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: train-2026-11-27" \
  -d '{"quote_id": "q_9d2c4e1f0a7b4c8e9f1a2b3c4d5e6f70", "ssh_key": "ssh-ed25519 AAAA... you@laptop", "ttl_hours": 4}'
```

它会立即返回 `201`，租用状态为 `starting`。同样的请求再发一次，会返回 `200` 和同一次租用。

## 租用对象 [#the-rental]

```json title="201"
{
  "rental": {
    "id": 1042,
    "status": "running",
    "kind": "on_demand",
    "gpu_model": "H100 SXM",
    "gpus": 8,
    "nodes": 1,
    "cluster": false,
    "data": false,
    "app": "pytorch",
    "per_hour_usd": "17.52",
    "cost_usd": "2.92",
    "billed_seconds": 600,
    "ssh": "ssh -p 40022 root@203.0.113.7",
    "tunnel": {
      "command": "ssh -N -L 8888:localhost:8888 -p 40022 root@203.0.113.7",
      "url": "http://localhost:8888"
    },
    "node_access": [],
    "requested_at": "2026-11-27T09:30:12.000Z",
    "started_at": "2026-11-27T09:31:40.000Z",
    "ends_at": "2026-11-27T13:31:40.000Z",
    "ended_at": null,
    "end_reason": null,
    "price_rise": null,
    "can_stop": true,
    "can_extend": true
  }
}
```

| 字段                          | 含义                                                                                    |
| --------------------------- | ------------------------------------------------------------------------------------- |
| `status`                    | `starting`、`running`、`paused`（计费已暂停：10 分钟内没有通过检查）、`stopping`、`ended` 或 `failed`（从不计费） |
| `ssh`、`tunnel`              | 进入 `running` 后的连接方式。之前为 `null`                                                        |
| `node_access`               | 集群：每个节点的 `rank`、`ssh` 和 `private_ip`                                                  |
| `cost_usd`、`billed_seconds` | 到目前为止的费用和计费秒数                                                                         |
| `ends_at`                   | 自动停止的时间；在网站上启动的为 `null`                                                               |
| `end_reason`                | 结束原因。见下文                                                                              |
| `price_rise`                | 即将到来的价格变化：新的 `per_hour_usd` 和生效时间 `at`                                                |

### 租用为何结束 [#why-a-rental-ended]

| `end_reason`      | 含义                    |
| ----------------- | --------------------- |
| `renter_stop`     | 你或你的密钥停止了它            |
| `balance_empty`   | 你的余额用完了               |
| `spend_cap`       | 达到了每日上限               |
| `ended_on_time`   | `ttl_hours` 到了        |
| `preempted`       | 可中断机器被收回              |
| `stopped_by_host` | 机器在出租方那边停止了           |
| `key_revoked`     | 启动它的密钥已被撤销            |
| `paused`          | 出于安全原因，新机器已暂停         |
| `policy`          | 它违反了我们的[服务条款](/terms) |
| `canceled`        | 它在启动前被取消              |
| `start_failed`    | 它启动失败。从不计费            |

## 列出租用 [#list-rentals]

<Endpoint method="GET" path="/api/v1/rentals" />

你的租用，最新的在前，放在 `data` 中。加上 `?status=active` 只看仍在进行的，`limit` 为 1 到 200，默认 50。

## 一次租用 [#one-rental]

<Endpoint method="GET" path="/api/v1/rentals/{id}" />

返回 `{ "rental": { ... }, "events": [ ... ] }`。每条记录都有 `kind` 和 `at` 时间，适用时还有 `reason` 等详情。启动期间，大约每 10 秒读取一次。

## 停止租用 [#stop-a-rental]

<Endpoint method="POST" path="/api/v1/rentals/{id}/stop" />

计费立即结束。机器连同 /data 以外的所有内容一起被删除，`status` 很快变为 `ended`。再次停止不会有任何变化。没有请求体。

## 延长租用 [#extend-a-rental]

<Endpoint method="POST" path="/api/v1/rentals/{id}/extend" />

为密钥启动的机器在 `ends_at` 上增加时长，仅在 `starting` 或 `running` 时可用。从现在起永远不会超过 7 天。接受 `Idempotency-Key`。

```json title="请求体"
{ "hours": 12 }
```

`hours` 为 1 到 168。在网站上启动的租用不能延长：它会一直运行，直到你停止它。这样的调用会得到 `409` `not_extendable`。

## 你可能遇到的错误 [#errors-you-may-get]

| 错误码                                  | 状态码 | 怎么办                              |
| ------------------------------------ | --- | -------------------------------- |
| `read_only_key`                      | 403 | 在 [API 密钥](/keys)中为密钥设定 GPU 每日限额 |
| `expired`、`price_moved`、`quote_used` | 409 | 获取新的报价，再次启动                      |
| `balance_short`                      | 402 | [充值](/docs/top-up)，然后再次启动        |
| `machine_unavailable`                | 409 | 它刚被租走。重新列出                       |
| `agent_cap`                          | 429 | 这个密钥已在运行 4 台机器或 1 个集群。先停止一个      |
| `key_daily_limit`                    | 429 | 等到下一个 UTC 日，或提高限额                |
| `data_unavailable`、`data_in_use`     | 409 | 参见[用 /data 保留文件](/docs/storage)  |

所有错误码：[错误](/docs/api/errors)。
