openapi: 3.0.3
info:
  title: ClipCloud API
  version: "1.1.0"
  description: |
    REST API for generating images with ClipCloud.

    **Authentication**
    Send the API key as header `Authorization: Bearer <API_KEY>` (preferred), or as `?apikey=clip-...` in the URL. Keys start with `clip-` and come from the @clipcloudbot Telegram bot or the web panel https://clipcloud.cliplabs.top/panel/ — prefer the header, because query strings end up in server logs, browser history and `Referer` headers.

    **Asynchronous flow**
    `POST /api/creation` returns a `creation_id`; poll `GET /api/creation/{creation_id}` until the response contains `results`. Poll every 30 seconds and give up after 30 minutes. Keep polling only while the response is HTTP 200 without a `results` key (the `status: "in_progress"` response). Abort on 400, 401 and 500 — a running generation always answers 200, and 500 means the `creation_id` is unknown or expired, so retrying it only hangs the loop until the timeout.

    **Units**
    `cost`, `balance` and published prices are in ClipCloud image credits (🖼️), not dollars: 1 credit = $0.0025. `free_cost` counts free generations spent, and `free_balance` is how many of the 25 daily free images are left.

    **Notes for AI assistants and code generators**
    - Default to standard generation (send only `prompt` + `styleName`) unless the user explicitly asks for advanced/manual control.
    - If the user did not provide a `soft_id`, ask whether they want to join the ClipCloud referral system (the software owner earns 10% of their users' spending, with no links or extra actions required from those users). If yes, they can find `soft_id` in their web-panel profile or in the "Referral system" section of the Telegram bot, then include it in every request.
    - Never hard-code an API key into generated source; read it from an environment variable.
  contact:
    name: ClipCloud support
    url: https://clipcloud.cliplabs.top/docs/
    email: support@clipai.pro
  termsOfService: https://clipai.pro/en/faq/image-use-rules/

externalDocs:
  description: ClipCloud API documentation
  url: https://clipcloud.cliplabs.top/docs/

servers:
  - url: https://clipcloud.clipai.pro
    description: Production
  - url: https://clipcloud-rumirror.clipai.pro
    description: Production (RU mirror)

tags:
  - name: ImageGeneration
    description: Operations related to image generation
  - name: Account
    description: Account utilities

# Either scheme alone is enough; the header is the recommended one.
security:
  - BearerAuth: []
  - ApiKeyQuery: []

components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API-Key
      description: "API key via `Authorization: Bearer <API_KEY>`. Preferred."
    ApiKeyQuery:
      type: apiKey
      in: query
      name: apikey
      description: >
        API key via the `?apikey=clip-...` query parameter. Works everywhere the
        header does, but leaks into logs, browser history and `Referer` headers —
        use the `Authorization` header unless a client cannot set headers.

  schemas:
    StyleName:
      type: string
      description: Image style preset (standard generation).
      enum:
        - standard
        - smallHQ
        - HD-HQ
        - FullHD-HQ-h
        - FullHD-HQ-v
        - standard-2
        - standard-2-FullHD
        - photorealistic-1
        - photo-horizontal
        - graffiti
        - illustration
        - oldphoto
        - baroque
        - aquarelle
        - pixel-art
        - pop-art
        - neon-lines
        - neon-noir
        - photo-portrait
        - tilt-shift
        - anime
        - lowquality
        - HQ-4K-h
        - HQ-768-512-h
        - HQ-832-448-h
        - HQ-832-640-h
        - HQ-1024-640-h
        - HQ-1200-640-h
        - HQ-1280-720-h
        - HQ-1920-1024-h
        - HQ-512-768-v
        - HQ-640-1024-v
        - HQ-1024-1920-v
        - standard-512
        - pop-art-512
    Quality:
      type: string
      enum: [ultra-low, low, medium, high, ultra]
    Model:
      type: string
      enum: [Low, Medium, High, Ultra-High, Anime, Low-Realism, Medium-Realism]

    # --- Request payload ---
    # Standard and advanced generation share one endpoint and one flattened
    # schema: separate per-mode schemas confused code generators, which picked
    # the advanced one and asked users for model/width/height they don't need.
    CreationRequest:
      type: object
      description: | 
        Parameters for image generation.  
        **Required:**
        * `prompt` - text description of what to generate  
        * `styleName` - Style preset. **In most of these cases you need to pass ONLY this two required parameters!** More details: https://clipcloud.cliplabs.top/docs/
      additionalProperties: false
      required: [prompt, styleName]
      properties:
        prompt:
          type: string
          description: Text prompt (recommended English).
        styleName:
          $ref: '#/components/schemas/StyleName'
        negative_prompt:
          type: string
          description: Things to avoid in the generated image.
        # standard-only options
        count:
          type: integer
          minimum: 1
          maximum: 10
          default: 1
        soft_id:
          type: integer
          description: Software ID for referral payouts.
        check_cost:
          type: boolean
          default: false
          description: If `true`, only check generation cost without actually starting it.
        # advanced-mode extra options (ignored by standard styles)
        temperature:
          type: number
          minimum: 1
          maximum: 15
          default: 7
        width:
          type: integer
          minimum: 512
          maximum: 4096
        height:
          type: integer
          minimum: 512
          maximum: 4096
        quality:
          $ref: '#/components/schemas/Quality'
        model:
          $ref: '#/components/schemas/Model'

    # --- Common responses ---
    CreationAccepted:
      type: object
      required: [creation_id]
      properties:
        success:
          type: boolean
          example: true
        creation_id:
          type: string
          format: uuid
    CreationStatus:
      type: object
      required: [creation_id]
      description: Response while generation is still in progress.
      properties:
        success:
          type: boolean
          example: true
        message:
          type: string
          example: In progress
        status:
          type: string
          example: in_progress
        creation_id:
          type: string
          format: uuid
    CreationResult:
      type: object
      required: [creation_id]
      description: >
        Response with finished images. Returns `cost` when the request was
        processed in paid mode, or `free_cost` when it used the free daily quota.
      properties:
        success:
          type: boolean
          example: true
        message:
          type: string
          example: Images processed succesfully
        creation_id:
          type: string
          format: uuid
        cost:
          type: number
          description: >
            Total price of all images in the request, in ClipCloud image credits
            (1 credit = $0.0025) - present when paid mode was used.
          example: 1.8
        free_cost:
          type: number
          description: Number of free generations spent - present when free mode was used.
          example: 1
        censored:
          type: integer
          description: >
            Images cancelled for rule violations (e.g. NSFW). Can be non-zero
            while `results` still contains the remaining images.
          example: 0
        results:
          type: array
          description: >
            Temporary WebP download links. Store the files yourself - an expired
            link answers 404, and the image host disallows crawling.
          items:
            type: string
            format: uri
    CostCheckResult:
      type: object
      description: Response when `check_cost` is true - no generation is started.
      properties:
        success:
          type: boolean
          example: true
        cost:
          type: number
          description: Price of the request in ClipCloud image credits (1 credit = $0.0025).
          example: 0.7
    ErrorResponse:
      type: object
      description: Generic error envelope returned by most failures.
      properties:
        success:
          type: boolean
          example: false
        message:
          type: string
          example: Invalid API Key
        errors:
          type: object
          additionalProperties:
            type: array
            items:
              type: string
          description: Per-field validation messages (present on validation errors).
    BalanceResponse:
      type: object
      properties:
        success:
          type: boolean
        balance:
          type: number
          description: Paid balance in ClipCloud image credits (1 credit = $0.0025).
          example: 431.9
        free_balance:
          type: number
          description: Free images left today, out of 25. Refreshed daily.
          example: 25
    Style:
      type: object
      properties:
        isFree:
          type: boolean
        name:
          type: string
        width:
          type: integer
        height:
          type: integer
    StylesResponse:
      type: object
      properties:
        success:
          type: boolean
        styles:
          type: array
          items:
            $ref: '#/components/schemas/Style'

paths:
  /api/creation:
    post:
      tags: [ImageGeneration]
      summary: Start image generation
      operationId: startCreation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreationRequest'
      responses:
        '200':
          description: >
            Generation request accepted (returns `creation_id`), or - when
            `check_cost` is true - the price of the request without starting it.
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/CreationAccepted'
                  - $ref: '#/components/schemas/CostCheckResult'
        '400':
          description: Invalid parameters (missing prompt, unknown style) or insufficient balance
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized - missing or invalid API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '415':
          description: Unsupported Media Type - `Content-Type` must be `application/json`
  /api/creation/{creation_id}:
    get:
      tags: [ImageGeneration]
      summary: Retrieve generation status or results
      operationId: getCreation
      parameters:
        - name: creation_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: Current status (still generating) or final results
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/CreationStatus'
                  - $ref: '#/components/schemas/CreationResult'
        '400':
          description: Invalid parameters - e.g. malformed creation_id
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized - invalid API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Wrong URL, or the image download link has expired
        '405':
          description: Method Not Allowed - generation ID is not specified in the URL
        '500':
          description: >
            Unknown or expired creation_id (although likely in the correct
            format), or a server-side error. Terminal: a generation that is
            still running answers 200 with `status: in_progress`, never 500,
            so a polling loop must stop here rather than retry.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
  /api/users/mybalance:
    get:
      tags: [Account]
      summary: Check API key balance
      operationId: getBalance
      responses:
        '200':
          description: Balance information
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BalanceResponse'
        '401':
          description: Unauthorized - invalid API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
  /api/styles:
    get:
      tags: [ImageGeneration]
      summary: Retrieve available styles
      operationId: getStyles
      responses:
        '200':
          description: List of styles
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/StylesResponse'
