# REST API

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

通过普通的 HTTP 和 JSON，用你自己的代码租用 GPU 机器。



REST API 能完成网站上所有关于机器的操作：列出、报价、启动、查看、延长和停止。每个请求都需要一个 [API 密钥](/docs/api-keys)。

## 基础地址 [#base-url]

```text
https://bflops.com/api/v1
```

## 身份验证 [#authentication]

在 `Authorization` 头中发送你的密钥：

```bash title="在你的电脑上"
curl https://bflops.com/api/v1/balance \
  -H "Authorization: Bearer bf_..."
```

`x-api-key: bf_...` 也可以。缺少密钥或密钥错误，会得到 `401` 和错误码 `unauthorized`。

## 四次调用租用一台机器 [#rent-a-machine-in-four-calls]

<Steps>
  <Step>
    ### 找到一台机器 [#find-a-machine]

    ```bash
    curl "https://bflops.com/api/v1/machines?model=RTX%204090&limit=1" \
      -H "Authorization: Bearer $BFLOPS_KEY"
    ```

    从 `data` 的第一项中取出 `id`。参见[机器与集群](/docs/api/machines)。
  </Step>

  <Step>
    ### 获取报价 [#get-a-quote]

    ```bash
    curl https://bflops.com/api/v1/quotes \
      -H "Authorization: Bearer $BFLOPS_KEY" \
      -H "Content-Type: application/json" \
      -d '{"offer_id": "s4821"}'
    ```

    取出 `quote.id`。它在 60 秒内有效。参见[报价](/docs/api/quotes)。
  </Step>

  <Step>
    ### 启动它 [#start-it]

    ```bash
    curl https://bflops.com/api/v1/rentals \
      -H "Authorization: Bearer $BFLOPS_KEY" \
      -H "Content-Type: application/json" \
      -H "Idempotency-Key: first-run-001" \
      -d "{\"quote_id\": \"q_...\", \"ssh_key\": \"$(cat ~/.ssh/id_ed25519.pub)\", \"ttl_hours\": 4}"
    ```

    取出 `rental.id`。它的 `status` 是 `starting`。参见[租用](/docs/api/rentals)。
  </Step>

  <Step>
    ### 等待，然后连接 [#wait-then-connect]

    每 10 秒读取一次 `GET /rentals/{id}`，直到 `status` 变成 `running`，然后运行它的 `ssh` 命令。用 `POST /rentals/{id}/stop` 停止它。
  </Step>
</Steps>

## 约定 [#conventions]

* 输入和输出都是 **JSON**，字段名使用 `snake_case`。带请求体时发送 `Content-Type: application/json`；请求体最多 16 KiB。
* **金额**是美元字符串，例如 `"0.44"` 或 `"12.075031"`。按十进制数解析，不要用浮点数。
* **时间**是 UTC 的 ISO 8601 格式，例如 `"2026-11-27T09:30:00.000Z"`。
* **价格**始终是你的价格，已包含我们的费用。
* 列表放在 `data` 中，最新或最便宜的排在前面。

## 速率限制 [#rate-limits]

每个密钥每分钟可以进行 120 次读取和 30 次更改。报价、启动、停止和延长都算更改。超过后，你会得到 `429` 和以秒为单位的 `Retry-After` 头：等待相应时间，然后重试。

## 安全重试 [#safe-retries]

`POST /rentals` 和 `POST /rentals/{id}/extend` 接受 `Idempotency-Key` 头：8 到 64 个字母、数字、`_` 或 `-`。24 小时内用同一个键再次发送，你会得到第一次的回复，带有 `Idempotent-Replayed: true` 头，不会重复启动。每次新的启动都用一个新的键。

## 接口 [#endpoints]

| 方法     | 路径                                                          | 作用           |
| ------ | ----------------------------------------------------------- | ------------ |
| `GET`  | [`/machines`](/docs/api/machines#list-machines)             | 列表中的机器，可筛选   |
| `GET`  | [`/machines/{id}`](/docs/api/machines#one-machine)          | 一台机器         |
| `GET`  | [`/clusters`](/docs/api/machines#list-clusters)             | 列表中的集群       |
| `GET`  | [`/clusters/{id}`](/docs/api/machines#one-cluster)          | 一个集群         |
| `POST` | [`/quotes`](/docs/api/quotes)                               | 你的价格，保留 60 秒 |
| `POST` | [`/rentals`](/docs/api/rentals#start-a-rental)              | 启动报价的机器或集群   |
| `GET`  | [`/rentals`](/docs/api/rentals#list-rentals)                | 你的租用         |
| `GET`  | [`/rentals/{id}`](/docs/api/rentals#one-rental)             | 一次租用及其记录     |
| `POST` | [`/rentals/{id}/stop`](/docs/api/rentals#stop-a-rental)     | 停止它；计费立即结束   |
| `POST` | [`/rentals/{id}/extend`](/docs/api/rentals#extend-a-rental) | 在自动停止前增加时长   |
| `GET`  | [`/balance`](/docs/api/balance)                             | 你的余额和充值方式    |

错误列在[错误](/docs/api/errors)中。整个 API 都是英文。
