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

# 设置数据集自动充值

> 使用 Bright Data 账号管理 API 的 PUT /billing/autorecharge/dataset 开启或关闭数据集自动充值，并选择扣款的支付方式。

`PUT /billing/autorecharge/dataset` 端点开启或关闭 Bright Data 账户的数据集自动充值，并设置扣款的支付方式。常规自动充值设置不会改变。

> [!TIP]
>
> 将您的 API key 粘贴到授权字段中。要获取 API key，请[创建账号](https://www.bright.cn/?hs_signup=1\&utm_source=docs\&utm_campaign=playground)，并查看[如何生成新的 API key？](https://docs.brightdata.com/cn/api-reference/authentication.md#如何生成新的-api-key？)

## 此端点需要哪种 API key 权限？

`PUT /billing/autorecharge/dataset` 需要 `billing.write.pay.settings` 权限。具有**管理员**或**财务**[权限级别](https://docs.brightdata.com/cn/api-reference/authentication.md#api-key-拥有哪些权限级别？)的 API key 拥有该权限。缺少该权限时，端点返回 HTTP 403 和纯文本消息。请在[账户设置](https://www.bright.cn/cp/setting/users)中管理 API key 权限。

## 如何开启数据集自动充值？

发送 `enabled: true` 并附带 `pmethod_id`。数据集自动充值不接受 `amount`：

1. 从 [`GET /billing/pmethods`](https://docs.brightdata.com/cn/api-reference/account-management-api/Get_payment_methods) 获取 `autorecharge_supported` 为 `true` 的支付方式 ID。
2. 发送请求：

```bash theme={null}
curl -X PUT "https://api.brightdata.com/billing/autorecharge/dataset" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"enabled": true, "pmethod_id": "64f0c2a1e4b0a1b2c3d4e5f6"}'
```

响应返回新的数据集自动充值设置：`available`、`enabled` 和 `pmethod_id`。当可使用常规自动充值且账户拥有数据集 zone 时，数据集自动充值可用。请在 [`GET /billing/autorecharge`](https://docs.brightdata.com/cn/api-reference/account-management-api/Get_auto_recharge_settings) 中查看 `dataset.available`。

## 数据集自动充值扣款多少？

仅当数据集快照构建开始且余额不足以支付快照费用时，数据集自动充值才会扣款。扣款在快照构建开始时进行，金额为 \$50 与该快照所差余额两者中的较大值：

| 快照所差余额 | 扣款 |
| - | - |
| \$120 | \$120 |
| \$20 | \$50 |

## 如何关闭数据集自动充值？

只发送 `enabled: false`，不附带其他字段：

```bash theme={null}
curl -X PUT "https://api.brightdata.com/billing/autorecharge/dataset" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"enabled": false}'
```

关闭数据集自动充值始终允许，即使 `available` 为 `false`。关闭数据集自动充值不会关闭常规自动充值。在 `enabled: false` 时发送 `pmethod_id` 会返回 `invalid_param`。

## PUT 如何更新设置？

请求体包含数据集部分的全部设置，请求是幂等的。重复发送相同的请求体，设置保持不变。

## 此端点可能返回哪些错误？

HTTP 400 和 `not_eligible` 的 HTTP 403 返回包含 `error` 和 `error_code` 的 JSON：

| HTTP 状态码 | `error_code` | 原因 |
| - | - | - |
| 400 | `invalid_param` | 类型错误、未知字段、`enabled: true` 时缺少 `pmethod_id`，或 `enabled: false` 时发送了该字段 |
| 400 | `pmethod_not_found` | 账户中没有此 ID 的已激活支付方式 |
| 400 | `pmethod_not_supported` | 该支付方式不支持自动充值或尚未确认 |
| 400 | `payment_required` | 账户没有已完成的付款 |
| 403 | `not_eligible` | 账户不可使用自动充值（`available: false`），而请求中包含 `enabled: true` |

HTTP 401、权限不足导致的 HTTP 403 和 HTTP 429 返回纯文本。账单端点允许每分钟 1,000 个请求、每小时 5,000 个请求。

## 相关端点

* [获取自动充值设置](https://docs.brightdata.com/cn/api-reference/account-management-api/Get_auto_recharge_settings.md)。当前的常规自动充值和数据集自动充值设置。
* [设置常规自动充值](https://docs.brightdata.com/cn/api-reference/account-management-api/Set_regular_auto_recharge.md)。开启或关闭常规自动充值，或修改充值金额。
* [获取支付方式](https://docs.brightdata.com/cn/api-reference/account-management-api/Get_payment_methods.md)。支付方式 ID 以及是否支持自动充值。


## OpenAPI

````yaml cn-openapi PUT /billing/autorecharge/dataset
openapi: 3.0.1
info:
  title: Bright Data API
  description: 将 Bright Data API 集成到您的流程中，以实现高端的爬取精度
  version: 1.0.0
servers:
  - url: https://api.brightdata.com
security:
  - bearerAuth: []
paths:
  /billing/autorecharge/dataset:
    put:
      description: >-
        启用或停用数据集自动充值。需要 `billing.write.pay.settings` 权限。


        请求体包含数据集部分的全部设置，PUT 是幂等的。常规部分不会改变。`{"enabled": false}` 始终允许，即使
        `available` 为 `false`。


        仅当数据集快照构建开始且余额不足以支付快照费用时，数据集自动充值才会扣款。扣款金额为 $50
        与该快照所差余额两者中的较大值。常规自动充值和数据集自动充值分别设置。
      operationId: setDatasetAutoRecharge
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/BillingDatasetEnable'
                - $ref: '#/components/schemas/BillingDisable'
            examples:
              enable:
                summary: 启用
                value:
                  enabled: true
                  pmethod_id: 64f0c2a1e4b0a1b2c3d4e5f6
              disable:
                summary: 停用
                value:
                  enabled: false
      responses:
        '200':
          description: 新的数据集自动充值设置
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BillingAutoRechargeDataset'
        '400':
          description: >-
            请求错误。`error_code` 取值：


            - `invalid_param`：类型错误、未知字段、`enabled: true` 时缺少 `pmethod_id`，或
            `enabled: false` 时发送了 `pmethod_id`。

            - `pmethod_not_found`：账户中没有此 ID 的已激活支付方式。

            - `pmethod_not_supported`：该支付方式不支持自动充值或尚未确认。

            - `payment_required`：账户没有已完成的付款。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BillingError'
        '401':
          description: 身份验证失败
          content:
            text/plain:
              schema:
                type: string
              example: User authentication is required
        '403':
          description: >-
            `text/plain`：API key 缺少 `billing.write.pay.settings`
            权限。`application/json` 且 `error_code` 为
            `not_eligible`：账户不可使用自动充值（`available: false`），而请求中包含 `enabled:
            true`。
          content:
            text/plain:
              schema:
                type: string
              example: >-
                Your API key lacks the required permissions for this action. You
                can change your token permissions at
                https://brightdata.com/cp/setting/users
            application/json:
              schema:
                $ref: '#/components/schemas/BillingError'
              example:
                error: Autocharge is not available for this account
                error_code: not_eligible
        '429':
          description: 超出速率限制（每分钟 1,000 个请求，每小时 5,000 个请求）
          content:
            text/plain:
              schema:
                type: string
components:
  schemas:
    BillingDatasetEnable:
      title: 启用
      type: object
      required:
        - enabled
        - pmethod_id
      additionalProperties: false
      properties:
        enabled:
          type: boolean
          enum:
            - true
        pmethod_id:
          type: string
          description: 来自 `GET /billing/pmethods` 的支付方式 ID。
          example: 64f0c2a1e4b0a1b2c3d4e5f6
    BillingDisable:
      title: 停用
      type: object
      required:
        - enabled
      additionalProperties: false
      properties:
        enabled:
          type: boolean
          enum:
            - false
    BillingAutoRechargeDataset:
      type: object
      required:
        - available
        - enabled
        - pmethod_id
      properties:
        available:
          type: boolean
          description: 可使用常规自动充值且账户拥有数据集 zone 时为 `true`。
        enabled:
          type: boolean
          description: 此部分已启用自动充值时为 `true`。
        pmethod_id:
          type: string
          nullable: true
          description: 用于自动充值的支付方式。停用时为 `null`。
    BillingError:
      type: object
      required:
        - error
        - error_code
      properties:
        error:
          type: string
          description: 可读的错误消息
        error_code:
          type: string
          enum:
            - invalid_param
            - amount_too_low
            - amount_too_high
            - amount_not_whole
            - pmethod_not_found
            - pmethod_not_supported
            - payment_required
            - not_eligible
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        在 Authorization 头中使用您的 Bright Data API Key 作为 Bearer token。


        **认证方法:**

        1. 从 Bright Data 账户设置获取您的 API Key:
        https://brightdata.com/cp/setting/users

        2. 在请求的 Authorization 头中包含 API Key

        3. 格式: `Authorization: Bearer YOUR_API_KEY`


        **示例:**

        ```

        Authorization: Bearer
        b5648e1096c6442f60a6c4bbbe73f8d2234d3d8324554bd6a7ec8f3f251f07df

        ```


        了解如何获取 Bright Data API Key:
        https://docs.brightdata.com/cn/api-reference/authentication#如何生成新的-api-key？
      bearerFormat: API Key

````