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

# 创建子账户

> 创建一个拥有独立余额的子账户，其余额与主账户余额完全独立。

**所需权限：** `SUBACCOUNT:WRITE` 或 `FULL_ACCESS`

> [!NOTE]
>
> **`reference` 不可更改。** 它是您在 `X-Sub-Account` 请求头中发送、用于在子账户上操作的标识符，也是 webhook 中 `subAccount` 字段收到的值——请谨慎选择，因为创建后无法修改。
>
> 格式：`^[a-z0-9][a-z0-9_-]{1,31}$`——2 到 32 个字符，仅限小写字母、数字、`-` 和 `_`，并以小写字母或数字开头。`reference` 在账户内必须唯一：重复使用已存在的值将返回 `409 SUB_ACCOUNT_REFERENCE_TAKEN`。
>
> 子账户创建时拥有独立余额，初始为零，与主账户余额完全独立。关于 `X-Sub-Account` 请求头的行为和完整的错误表，参见[子账户](https://docs.brazilpix.com/cn.md#子账户)。


## OpenAPI

````yaml openapi.cn.json POST /sub-accounts
openapi: 3.0.0
info:
  title: Brazil Pix API
  description: Brazil Pix Payment API documentation
  version: '1.0'
  contact: {}
servers:
  - url: https://api.brazilpix.com/v2
security: []
paths:
  /sub-accounts:
    post:
      tags:
        - External API - Sub-accounts (Subcontas)
      summary: 创建子账户
      description: |-
        创建一个拥有独立余额的子账户，其余额与主账户余额完全独立。

        **所需权限：** `SUBACCOUNT:WRITE` 或 `FULL_ACCESS`
      operationId: ExternalSubAccountsController_createSubAccount
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateSubAccountInput'
      responses:
        '201':
          description: 子账户创建成功
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateSubAccountOutput'
        '400':
          description: Bad Request - Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BadRequestErrorResponse'
        '401':
          description: Unauthorized - Authentication required
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UnauthorizedErrorResponse'
        '403':
          description: Forbidden - Permission denied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ForbiddenErrorResponse'
        '409':
          description: >-
            Conflict - `reference`
            已在本账户中使用（`SUB_ACCOUNT_REFERENCE_TAKEN`），或已达到子账户数量上限（`SUB_ACCOUNT_QUOTA_EXCEEDED`）
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConflictErrorResponse'
        '500':
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InternalServerErrorResponse'
      security:
        - bearerAuth: []
components:
  schemas:
    CreateSubAccountInput:
      type: object
      properties:
        reference:
          type: string
          description: >-
            由您定义的子账户标识符。在账户内唯一，创建后不可更改。`principal` 是主账户的保留字，不可使用：将返回 `400
            SUB_ACCOUNT_INVALID_REFERENCE`。
          pattern: ^[a-z0-9][a-z0-9_-]{1,31}$
          example: loja-centro
        name:
          type: string
          description: 子账户显示名称
          maxLength: 255
          example: Loja Centro
      required:
        - reference
        - name
    CreateSubAccountOutput:
      type: object
      properties:
        id:
          type: string
          description: 子账户 ID
          format: uuid
        reference:
          type: string
          description: 子账户的 reference
          example: loja-centro
        name:
          type: string
          description: 子账户名称
          example: Loja Centro
        status:
          type: string
          description: 子账户状态
          example: approved
        createdAt:
          type: string
          description: 创建时间
          format: date-time
      required:
        - id
        - reference
        - name
        - status
        - createdAt
    BadRequestErrorResponse:
      type: object
      properties:
        statusCode:
          type: number
          example: 400
        error:
          type: string
          example: BadRequest
        message:
          type: string
          example: name must be longer than or equal to 2 characters
        details:
          type: object
        timestamp:
          type: string
          example: '2025-12-15T22:00:00.000Z'
        traceId:
          type: string
      required:
        - statusCode
        - error
        - message
        - timestamp
    UnauthorizedErrorResponse:
      type: object
      properties:
        statusCode:
          type: number
          example: 401
        error:
          type: string
          example: Unauthorized
        message:
          type: string
          example: Authentication token is required
        details:
          type: object
        timestamp:
          type: string
          example: '2025-12-15T22:00:00.000Z'
        traceId:
          type: string
      required:
        - statusCode
        - error
        - message
        - timestamp
    ForbiddenErrorResponse:
      type: object
      properties:
        statusCode:
          type: number
          example: 403
        error:
          type: string
          example: Forbidden
        message:
          type: string
          example: You do not have permission to access this resource
        details:
          type: object
        timestamp:
          type: string
          example: '2025-12-15T22:00:00.000Z'
        traceId:
          type: string
      required:
        - statusCode
        - error
        - message
        - timestamp
    ConflictErrorResponse:
      type: object
      properties:
        statusCode:
          type: number
          example: 409
        error:
          type: string
          example: Conflict
        message:
          type: string
          example: Resource with name 'Example' already exists
        details:
          type: object
          example:
            resource: Example
            field: name
            value: Example
        timestamp:
          type: string
          example: '2025-12-15T22:00:00.000Z'
        traceId:
          type: string
      required:
        - statusCode
        - error
        - message
        - timestamp
    InternalServerErrorResponse:
      type: object
      properties:
        statusCode:
          type: number
          example: 500
        error:
          type: string
          example: InternalError
        message:
          type: string
          example: An unexpected error occurred
        details:
          type: object
        timestamp:
          type: string
          example: '2025-12-15T22:00:00.000Z'
        traceId:
          type: string
          example: 2771463d58840c8e116dc6dda1e1e5d0
      required:
        - statusCode
        - error
        - message
        - timestamp
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: 通过 POST /auth（client_id + client_secret）获取的访问令牌。有效期 300 秒。

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.