openapi: 3.1.0
info:
  title: MinerU Gateway Compatible API
  version: 0.1.0
  description: |
    MinerU precise-parsing compatible surface. Gateway API Keys replace
    upstream MinerU Tokens. Callback fields are rejected by the MVP.
    For quota-enabled accounts, creation reserves page quota atomically from both
    the owner account and key. Default reservation is 200 pages per file, bounded
    by page_ranges when present. HTTP 402 / code -70012 means insufficient quota.
    Queries never reserve or charge quota. Successful files settle using confirmed
    document pages; failed files release their hold. Missing page counts or unknown
    submissions retain a reservation for audited reconciliation. Rule: pages-v1,
    1 page = 1 quota unit; this is a Gateway allocation policy, not monetary pricing.
servers:
  - url: /
security:
  - gatewayBearer: []
paths:
  /gateway/v1/batches/{batch_id}/files/{ordinal}:
    put:
      operationId: uploadBatchPDF
      x-implementation-status: implemented
      description: |
        Gateway extension for browser PDF uploads without storage CORS support.
        Requires the exact Gateway Key that created the file batch. Streams to
        the encrypted stored HTTPS upload URL; callers cannot supply a target.
        Requires waiting_file state, an unexpired upload URL and a PDF header.
        Maximum 200 MiB, known Content-Length, 90-second upload deadline.
        No automatic upload retry. On uncertain errors query the same batch.
      parameters:
        - name: batch_id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - name: ordinal
          in: path
          required: true
          schema: { type: integer, minimum: 0 }
      requestBody:
        required: true
        content:
          application/pdf:
            schema: { type: string, format: binary }
      responses:
        '200':
          description: Storage confirmed the upload; parsing continues asynchronously.
          content:
            application/json:
              schema:
                type: object
                properties:
                  code: { type: integer, const: 0 }
                  msg: { type: string }
                  trace_id: { type: string }
                  data:
                    type: object
                    properties:
                      batch_id: { type: string, format: uuid }
                      uploaded_bytes: { type: integer }
        '400':
          $ref: '#/components/responses/GatewayError'
        '401':
          $ref: '#/components/responses/GatewayError'
        '404':
          $ref: '#/components/responses/GatewayError'
        '409':
          $ref: '#/components/responses/GatewayError'
        '410':
          $ref: '#/components/responses/GatewayError'
        '411':
          $ref: '#/components/responses/GatewayError'
        '413':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '503':
          $ref: '#/components/responses/GatewayError'
  /api/v4/extract/task:
    post:
      operationId: createExtractTask
      x-implementation-status: implemented
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExtractTaskRequest'
      responses:
        '200':
          description: Task accepted and bound to an upstream channel.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateTaskResponse'
        '202':
          $ref: '#/components/responses/SubmissionUnknown'
        '400':
          $ref: '#/components/responses/GatewayError'
        '402':
          description: Insufficient account or key quota; code -70012. No task submitted.
        '401':
          $ref: '#/components/responses/GatewayError'
        '409':
          $ref: '#/components/responses/GatewayError'
        '422':
          $ref: '#/components/responses/GatewayError'
        '415':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '503':
          $ref: '#/components/responses/GatewayError'
        '500':
          $ref: '#/components/responses/GatewayError'
  /api/v4/extract/task/{task_id}:
    get:
      operationId: getExtractTask
      x-implementation-status: implemented
      parameters:
        - $ref: '#/components/parameters/TaskID'
      responses:
        '200':
          description: Current task state from the bound channel.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TaskResponse'
        '202':
          $ref: '#/components/responses/SubmissionUnknown'
        '401':
          $ref: '#/components/responses/GatewayError'
        '404':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '500':
          $ref: '#/components/responses/GatewayError'
  /api/v4/file-urls/batch:
    post:
      operationId: createUploadBatch
      x-implementation-status: implemented
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UploadBatchRequest'
      responses:
        '200':
          description: Upload URLs allocated and batch bound to a channel.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateUploadBatchResponse'
        '202':
          $ref: '#/components/responses/SubmissionUnknown'
        '400':
          $ref: '#/components/responses/GatewayError'
        '402':
          description: Insufficient account or key quota; code -70012. No task submitted.
        '401':
          $ref: '#/components/responses/GatewayError'
        '409':
          $ref: '#/components/responses/GatewayError'
        '410':
          description: A matching idempotent batch exists, but its upload URLs have expired.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '415':
          $ref: '#/components/responses/GatewayError'
        '422':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '503':
          $ref: '#/components/responses/GatewayError'
        '500':
          $ref: '#/components/responses/GatewayError'
  /api/v4/extract/task/batch:
    post:
      operationId: createURLBatch
      x-implementation-status: implemented
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/URLBatchRequest'
      responses:
        '200':
          description: URL batch accepted and bound to a channel.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateBatchResponse'
        '202':
          $ref: '#/components/responses/SubmissionUnknown'
        '400':
          $ref: '#/components/responses/GatewayError'
        '402':
          description: Insufficient account or key quota; code -70012. No task submitted.
        '401':
          $ref: '#/components/responses/GatewayError'
        '409':
          $ref: '#/components/responses/GatewayError'
        '415':
          $ref: '#/components/responses/GatewayError'
        '422':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '503':
          $ref: '#/components/responses/GatewayError'
        '500':
          $ref: '#/components/responses/GatewayError'
  /api/v4/extract-results/batch/{batch_id}:
    get:
      operationId: getBatchResults
      x-implementation-status: implemented
      parameters:
        - $ref: '#/components/parameters/BatchID'
      responses:
        '200':
          description: Current batch state from the bound channel.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BatchResponse'
        '202':
          $ref: '#/components/responses/SubmissionUnknown'
        '401':
          $ref: '#/components/responses/GatewayError'
        '404':
          $ref: '#/components/responses/GatewayError'
        '502':
          $ref: '#/components/responses/GatewayError'
        '429':
          $ref: '#/components/responses/GatewayError'
        '500':
          $ref: '#/components/responses/GatewayError'
  /gateway/v1/health/live:
    get:
      operationId: getLiveness
      security: []
      responses:
        '200':
          description: Process is alive.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HealthResponse'
  /gateway/v1/health/ready:
    get:
      operationId: getReadiness
      security: []
      responses:
        '200':
          description: Required dependencies are available.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HealthResponse'
        '503':
          description: A required dependency is unavailable.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HealthResponse'
components:
  securitySchemes:
    gatewayBearer:
      type: http
      scheme: bearer
      bearerFormat: Gateway API Key
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      schema:
        type: string
        minLength: 1
        maxLength: 128
        pattern: '^[\x21-\x7E]+$'
    TaskID:
      name: task_id
      in: path
      required: true
      schema:
        type: string
        format: uuid
    BatchID:
      name: batch_id
      in: path
      required: true
      schema:
        type: string
        format: uuid
  responses:
    GatewayError:
      description: Gateway or upstream error.
      headers:
        X-Gateway-Trace-Id:
          $ref: '#/components/headers/GatewayTraceID'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    SubmissionUnknown:
      description: Upstream acceptance is unknown; poll the returned Gateway ID.
      headers:
        X-Gateway-Trace-Id:
          $ref: '#/components/headers/GatewayTraceID'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SubmissionUnknownResponse'
  headers:
    GatewayTraceID:
      description: Gateway request correlation ID.
      schema:
        type: string
        pattern: '^gw_'
  schemas:
    HealthResponse:
      type: object
      required: [status]
      properties:
        status:
          type: string
          enum: [ok, not_ready]
      additionalProperties: false
    BaseEnvelope:
      type: object
      required: [code, msg, trace_id, data]
      properties:
        code:
          oneOf:
            - type: integer
            - type: string
        msg:
          type: string
        trace_id:
          type: string
        data: {}
      additionalProperties: true
    ErrorResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            data:
              type: [object, 'null']
    CreateTaskResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            code:
              const: 0
            data:
              type: object
              required: [task_id]
              properties:
                task_id:
                  type: string
                  format: uuid
    CreateBatchResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            code:
              const: 0
            data:
              type: object
              required: [batch_id]
              properties:
                batch_id:
                  type: string
                  format: uuid
    CreateUploadBatchResponse:
      allOf:
        - $ref: '#/components/schemas/CreateBatchResponse'
        - type: object
          properties:
            data:
              type: object
              required: [batch_id, file_urls]
              properties:
                batch_id:
                  type: string
                  format: uuid
                file_urls:
                  type: array
                  items:
                    type: string
                    format: uri
    SubmissionUnknownResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            code:
              const: -70008
            data:
              type: object
              required: [state]
              properties:
                state:
                  const: submission_unknown
                task_id:
                  type: string
                  format: uuid
                batch_id:
                  type: string
                  format: uuid
              oneOf:
                - required: [task_id]
                - required: [batch_id]
    ExtractTaskRequest:
      type: object
      required: [url]
      properties:
        url:
          type: string
          format: uri
        model_version:
          type: string
          enum: [pipeline, vlm, MinerU-HTML]
          default: pipeline
        is_ocr:
          type: boolean
          default: false
        enable_formula:
          type: boolean
          default: true
        enable_table:
          type: boolean
          default: true
        language:
          type: string
          default: ch
        data_id:
          $ref: '#/components/schemas/DataID'
        extra_formats:
          type: array
          uniqueItems: true
          items:
            type: string
            enum: [docx, html, latex]
        page_ranges:
          type: string
        no_cache:
          type: boolean
          default: false
        cache_tolerance:
          type: integer
          minimum: 0
          default: 900
        callback:
          type: string
          description: Rejected by the Gateway MVP when non-empty.
        seed:
          type: string
          description: Rejected by the Gateway MVP when non-empty.
      additionalProperties: true
    UploadBatchRequest:
      type: object
      required: [files]
      properties:
        model_version:
          type: string
          enum: [pipeline, vlm, MinerU-HTML]
        files:
          type: array
          minItems: 1
          maxItems: 50
          items:
            type: object
            required: [name]
            properties:
              name:
                type: string
                minLength: 1
                maxLength: 255
                pattern: '^[^/\\\r\n]+\.[^./\\\r\n]+$'
              data_id:
                $ref: '#/components/schemas/DataID'
            additionalProperties: true
      additionalProperties: true
    URLBatchRequest:
      type: object
      required: [files]
      properties:
        model_version:
          type: string
          enum: [pipeline, vlm, MinerU-HTML]
        files:
          type: array
          minItems: 1
          maxItems: 50
          items:
            type: object
            required: [url]
            properties:
              url:
                type: string
                format: uri
              data_id:
                $ref: '#/components/schemas/DataID'
              is_ocr:
                type: boolean
              page_ranges:
                type: string
                minLength: 1
                maxLength: 128
            additionalProperties: true
      additionalProperties: true
    DataID:
      type: string
      maxLength: 128
      pattern: '^[A-Za-z0-9_.-]+$'
    TaskResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            data:
              type: object
              required: [task_id, state]
              properties:
                task_id:
                  type: string
                  format: uuid
                state:
                  $ref: '#/components/schemas/TaskState'
                extract_progress:
                  type: object
                  additionalProperties: true
                full_zip_url:
                  type: string
                  format: uri
                err_msg:
                  type: string
              additionalProperties: true
    BatchResponse:
      allOf:
        - $ref: '#/components/schemas/BaseEnvelope'
        - type: object
          properties:
            data:
              type: object
              required: [batch_id, extract_result]
              properties:
                batch_id:
                  type: string
                  format: uuid
                extract_result:
                  type: array
                  items:
                    type: object
                    required: [state]
                    properties:
                      state:
                        $ref: '#/components/schemas/BatchItemState'
                    additionalProperties: true
              additionalProperties: true
    TaskState:
      type: string
      enum: [submitting, submission_unknown, pending, running, converting, done, failed]
    BatchItemState:
      type: string
      enum: [waiting-file, pending, running, converting, done, failed]
