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

# Generate Batch Child Codes

> Generate child codes for a batch template. Template must be in active status. Supports initial generation, retry on failure, and incremental expansion. Either templateId or templateCode must be provided. If quantity > 1000, isAsync must be set to true. Default is synchronous execution.

## Endpoint Overview

**POST** `https://api.unibee.dev/merchant/discount/batch/children/generate`

Generate child codes for a batch template. Template must be in active status. Supports initial generation, retry on failure, and incremental expansion. Either templateId or templateCode must be provided. If quantity > 1000, isAsync must be set to true. Default is synchronous execution.

***

## Authorization

All UniBee Merchant API requests require authentication via API key.

| Header          | Required | Description                           |
| --------------- | -------- | ------------------------------------- |
| `Authorization` | Yes      | Bearer `<your_api_key>`               |
| `Content-Type`  | Yes      | `application/json` (for request body) |

***

## Parameters

Parameters for this endpoint are listed below. The schema is also shown in the Try it panel.

### Request body

| Name           | Type    | Required | Description                                                                                                                                                                                             |
| -------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `codeLength`   | integer | No       | Optional. Total length of generated child code (prefix + random part). Must be > len(templateCode) and \< 190. The random part length must be sufficient to support the template's target quantity.     |
| `isAsync`      | boolean | No       | Force async execution. Default is false (synchronous). If quantity > 1000, this must be set to true.                                                                                                    |
| `quantity`     | integer | No       | Number of child codes to generate. If not provided, generates up to the template's quantity limit minus existing count. Must not exceed the remaining available quantity.                               |
| `randomStyle`  | string  | No       | Optional. Random part style: auto\|numeric\|lower\|upper. Default is auto, which infers style from templateCode: all digits -> numeric, all lowercase (with optional digits) -> lower, otherwise upper. |
| `templateCode` | string  | No       | The template's code prefix (alternative to templateId)                                                                                                                                                  |
| `templateId`   | integer | No       | The template's Id (alternative to templateCode)                                                                                                                                                         |

***

## Request examples

### cURL

```bash theme={null}
curl -X POST "https://api.unibee.dev/merchant/discount/batch/children/generate" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "codeLength": 0,
  "isAsync": false,
  "quantity": 0,
  "randomStyle": "",
  "templateCode": "",
  "templateId": 0
}'
```

### Sandbox

```bash theme={null}
curl -X POST "https://api-sandbox.unibee.top/merchant/discount/batch/children/generate" \
  -H "Authorization: Bearer YOUR_SANDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "codeLength": 0,
  "isAsync": false,
  "quantity": 0,
  "randomStyle": "",
  "templateCode": "",
  "templateId": 0
}'
```

***

## Response

Success responses return a JSON envelope with `code`, `data`, `message`, `redirect`, and `requestId`. `code` `0` indicates success.

| Field                 | Type    | Description                                                                    |
| --------------------- | ------- | ------------------------------------------------------------------------------ |
| `code`                | integer | Response code. `0` = success                                                   |
| `data`                | object  | Response payload                                                               |
| `data.existingCount`  | integer | Already generated count before this operation                                  |
| `data.failedCount`    | integer | Failed count in this operation (only for synchronous execution)                |
| `data.generatedCount` | integer | Newly generated count in this operation (only for synchronous execution)       |
| `data.isAsync`        | boolean | Whether the generation is running asynchronously                               |
| `data.successCount`   | integer | Successfully inserted count in this operation (only for synchronous execution) |
| `data.templateCode`   | string  | Template code prefix                                                           |
| `data.templateId`     | integer | Template ID                                                                    |
| `data.totalTarget`    | integer | Target total quantity                                                          |
| `message`             | string  | Human-readable message                                                         |
| `requestId`           | string  | Request ID for support                                                         |

***

## Error handling

| HTTP status | Meaning                                                                   |
| ----------- | ------------------------------------------------------------------------- |
| 400         | Bad request — invalid or missing parameters. Check `message` in the body. |
| 401         | Unauthorized — missing or invalid API key.                                |
| 404         | Not found — invalid path or resource.                                     |
| 500         | Server error — retry with backoff.                                        |

When `code` in the response body is non-zero, check `message` for details. Use `requestId` when contacting support.


## OpenAPI

````yaml post /merchant/discount/batch/children/generate
openapi: 3.0.0
info:
  description: UniBee Api Server
  license:
    name: Apache-2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
  title: OpenAPI UniBee
  version: daily,buildtime:202603161017
servers:
  - url: https://api.unibee.dev
  - url: https://api-sandbox.unibee.top
security:
  - Authorization: []
paths:
  /merchant/discount/batch/children/generate:
    post:
      tags:
        - Batch Discount
      summary: Generate Batch Child Codes
      description: >-
        Generate child codes for a batch template. Template must be in active
        status. Supports initial generation, retry on failure, and incremental
        expansion. Either templateId or templateCode must be provided. If
        quantity > 1000, isAsync must be set to true. Default is synchronous
        execution.
      requestBody:
        content:
          application/json:
            schema:
              $ref: >-
                #/components/schemas/unibee.api.merchant.discount.BatchChildrenGenerateReq
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  code:
                    format: int
                    properties: {}
                    type: integer
                  data:
                    properties:
                      existingCount:
                        description: Already generated count before this operation
                        format: int64
                        properties: {}
                        type: integer
                      failedCount:
                        description: >-
                          Failed count in this operation (only for synchronous
                          execution)
                        format: int
                        properties: {}
                        type: integer
                      generatedCount:
                        description: >-
                          Newly generated count in this operation (only for
                          synchronous execution)
                        format: int
                        properties: {}
                        type: integer
                      isAsync:
                        description: Whether the generation is running asynchronously
                        format: bool
                        properties: {}
                        type: boolean
                      successCount:
                        description: >-
                          Successfully inserted count in this operation (only
                          for synchronous execution)
                        format: int
                        properties: {}
                        type: integer
                      templateCode:
                        description: Template code prefix
                        format: string
                        properties: {}
                        type: string
                      templateId:
                        description: Template ID
                        format: int64
                        properties: {}
                        type: integer
                      totalTarget:
                        description: Target total quantity
                        format: int64
                        properties: {}
                        type: integer
                    type: object
                  merchantId:
                    format: int64
                    properties: {}
                    type: integer
                  message:
                    format: string
                    properties: {}
                    type: string
                  redirect:
                    format: string
                    properties: {}
                    type: string
                  requestId:
                    format: string
                    properties: {}
                    type: string
                type: object
          description: ''
components:
  schemas:
    unibee.api.merchant.discount.BatchChildrenGenerateReq:
      description: >-
        Generate child codes for a batch template. Template must be in active
        status. Supports initial generation, retry on failure, and incremental
        expansion. Either templateId or templateCode must be provided. If
        quantity > 1000, isAsync must be set to true. Default is synchronous
        execution.
      properties:
        codeLength:
          description: >-
            Optional. Total length of generated child code (prefix + random
            part). Must be > len(templateCode) and <= 190. The random part
            length must be sufficient to support the template's target quantity.
          format: '*int'
          properties: {}
          type: integer
        isAsync:
          description: >-
            Force async execution. Default is false (synchronous). If quantity >
            1000, this must be set to true.
          format: '*bool'
          properties: {}
          type: boolean
        quantity:
          description: >-
            Number of child codes to generate. If not provided, generates up to
            the template's quantity limit minus existing count. Must not exceed
            the remaining available quantity.
          format: '*int64'
          properties: {}
          type: integer
        randomStyle:
          description: >-
            Optional. Random part style: auto|numeric|lower|upper. Default is
            auto, which infers style from templateCode: all digits -> numeric,
            all lowercase (with optional digits) -> lower, otherwise upper.
          format: '*string'
          properties: {}
          type: string
        templateCode:
          description: The template's code prefix (alternative to templateId)
          format: string
          properties: {}
          type: string
        templateId:
          description: The template's Id (alternative to templateCode)
          format: '*int64'
          properties: {}
          type: integer
      type: object
  securitySchemes:
    Authorization:
      bearerFormat: JWT
      scheme: bearer
      type: http

````