openapi: 3.1.0
info:
  title: Cloud Music API
  version: 0.2.0
  description: Separate tracks and generate music. Jobs and credits belong to the authenticated Music account.
servers:
  - url: https://api.soniqo.audio/v1/music
security:
  - musicBearer: []
paths:
  /credits:
    get:
      operationId: getMusicCredits
      summary: Read the current monthly credit balance
      responses:
        '200':
          description: Current allowance, charges, reservations and available units
          content:
            application/json:
              schema: { $ref: '#/components/schemas/CreditBalance' }
        default: { $ref: '#/components/responses/Error' }
  /uploads:
    post:
      operationId: createMusicUpload
      summary: Create an account-owned audio upload
      description: PUT WAV, MP3 or M4A bytes to the temporary URL with the required headers. Use the upload ID when requesting a quote.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/UploadRequest' }
      responses:
        '201':
          description: Upload slot and signed PUT URL
          content:
            application/json:
              schema: { $ref: '#/components/schemas/UploadSlot' }
        default: { $ref: '#/components/responses/Error' }
  /uploads/{upload_id}:
    parameters:
      - name: upload_id
        in: path
        required: true
        schema: { type: string }
    get:
      operationId: getMusicUpload
      summary: Check audio validation and decoded duration
      responses:
        '200':
          description: Current upload status
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Upload' }
        '404': { $ref: '#/components/responses/NotFound' }
        default: { $ref: '#/components/responses/Error' }
  /quotes:
    post:
      operationId: createMusicQuote
      summary: Quote a separation or generation job
      description: >
        Validate any upload used by the request and bind the quote to those exact bytes.
        Quotes expire and belong to one account. Standard separation costs one credit per decoded minute,
        measured in whole seconds with fractional seconds rounded up. The quote shows the cost for the
        separation job or for both generation takes. Generation costs one unit per target second per take
        through 60 seconds and 2.25 units per additional second per take. The total rounds up to a whole unit.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - { $ref: '#/components/schemas/SeparationRequest' }
                - { $ref: '#/components/schemas/GenerationRequest' }
            examples:
              separate:
                value: { operation: separate_tracks, upload_id: abc123abc123abc123abc123abc123ab, stem_count: 4 }
              generate:
                value:
                  operation: generate_music
                  mode: vocals
                  prompt: Upbeat pop with warm synths and an English vocal about starting over.
                  duration_seconds: 30
                  reference_upload_id: def456def456def456def456def456de
      responses:
        '201':
          description: Quote for the validated request
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Quote' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409':
          description: Upload bytes or validation are pending
          headers:
            Retry-After:
              description: Seconds to wait before repeating the quote request
              schema: { type: integer, minimum: 1 }
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '422':
          description: Invalid audio or unsupported job settings
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        default: { $ref: '#/components/responses/Error' }
  /jobs:
    get:
      operationId: listMusicJobs
      summary: Browse current and previous jobs
      parameters:
        - name: cursor
          in: query
          description: Cursor from the previous page
          schema: { type: string }
      responses:
        '200':
          description: Account-owned job history
          content:
            application/json:
              schema:
                type: object
                required: [jobs]
                properties:
                  jobs:
                    type: array
                    items: { $ref: '#/components/schemas/Job' }
                  next_cursor: { type: string }
        default: { $ref: '#/components/responses/Error' }
    post:
      operationId: createMusicJob
      summary: Reserve credits and submit a quoted job
      description: Each quote admits one job. Repeating an accepted request with the same key and quote returns the original job.
      parameters:
        - name: Idempotency-Key
          in: header
          required: true
          schema: { type: string, minLength: 1 }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [quote_id]
              properties:
                quote_id: { type: string, minLength: 1 }
            example: { quote_id: abc123def456abc123def456abc123de }
      responses:
        '200':
          description: Original job for an identical retry
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Job' }
        '202':
          description: Job accepted and credits reserved
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Job' }
        '402':
          description: Insufficient credits
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409':
          description: Quote expired or used, or key reused with a different request
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        default: { $ref: '#/components/responses/Error' }
  /jobs/{job_id}:
    parameters:
      - { $ref: '#/components/parameters/JobId' }
    get:
      operationId: getMusicJob
      summary: Read job status and download results
      description: Completed jobs return signed download URLs while the files are retained. Read the job again to refresh the URLs.
      responses:
        '200':
          description: Current job state and any available artifacts
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Job' }
        '404': { $ref: '#/components/responses/NotFound' }
        default: { $ref: '#/components/responses/Error' }
  /jobs/{job_id}/cancel:
    parameters:
      - { $ref: '#/components/parameters/JobId' }
    post:
      operationId: cancelMusicJob
      summary: Request cancellation
      description: Retry this request safely. Poll canceling jobs until they reach a terminal state. Already terminal jobs return their current state.
      responses:
        '200':
          description: Current job state after the request
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Job' }
        '404': { $ref: '#/components/responses/NotFound' }
        default: { $ref: '#/components/responses/Error' }
components:
  securitySchemes:
    musicBearer:
      type: http
      scheme: bearer
      description: Bearer access token for the Music account
  parameters:
    JobId:
      name: job_id
      in: path
      required: true
      schema: { type: string }
  responses:
    NotFound:
      description: No resource with that ID in this account
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    Error:
      description: Request rejected
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
  schemas:
    Units:
      type: integer
      minimum: 0
      description: Whole units of one-sixtieth of a credit
    CreditBalance:
      type: object
      required: [period_start, period_end, granted_units, reserved_units, charged_units, available_units]
      properties:
        period_start: { type: string, format: date-time }
        period_end: { type: string, format: date-time }
        granted_units: { $ref: '#/components/schemas/Units' }
        reserved_units: { $ref: '#/components/schemas/Units' }
        charged_units: { $ref: '#/components/schemas/Units' }
        available_units: { $ref: '#/components/schemas/Units' }
    Operation:
      type: string
      enum: [separate_tracks, generate_music]
    StemCount:
      type: integer
      const: 4
      description: Vocals, drums, bass and other instruments.
    UploadRequest:
      type: object
      additionalProperties: false
      required: [file_name, content_type, size_bytes]
      properties:
        file_name: { type: string, minLength: 1 }
        content_type: { type: string, minLength: 1 }
        size_bytes: { type: integer, minimum: 1, maximum: 67108864 }
    UploadSlot:
      type: object
      required: [upload_id, put_url, required_headers, url_expires_at]
      properties:
        upload_id: { type: string }
        put_url: { type: string, format: uri }
        required_headers:
          type: object
          additionalProperties: { type: string }
        url_expires_at: { type: string, format: date-time }
    Upload:
      type: object
      required: [upload_id, status, expires_at]
      properties:
        upload_id: { type: string }
        status: { type: string, enum: [awaiting_upload, validating, ready, rejected, expired] }
        duration_seconds: { type: number, exclusiveMinimum: 0 }
        expires_at: { type: string, format: date-time }
        error: { $ref: '#/components/schemas/Error' }
    SeparationRequest:
      type: object
      additionalProperties: false
      required: [operation, upload_id]
      properties:
        operation: { const: separate_tracks }
        upload_id: { type: string, minLength: 1 }
        stem_count:
          $ref: '#/components/schemas/StemCount'
          default: 4
    GenerationRequest:
      type: object
      additionalProperties: false
      required: [operation, mode, prompt, duration_seconds]
      properties:
        operation: { const: generate_music }
        mode: { type: string, enum: [vocals, instrumental] }
        prompt: { type: string, minLength: 1 }
        duration_seconds:
          type: integer
          minimum: 1
          maximum: 300
          description: Target duration in seconds. Actual take lengths can be shorter and are reported on the artifacts.
        lyrics: { type: string, minLength: 1 }
        reference_upload_id:
          type: string
          minLength: 1
          description: ID of uploaded audio that guides the composition in either output mode.
      if:
        properties:
          mode: { const: instrumental }
      then:
        not:
          required: [lyrics]
    Quote:
      type: object
      required: [quote_id, operation, cost_units, expires_at]
      properties:
        quote_id: { type: string }
        operation: { $ref: '#/components/schemas/Operation' }
        stem_count: { $ref: '#/components/schemas/StemCount' }
        cost_units: { $ref: '#/components/schemas/Units' }
        expires_at: { type: string, format: date-time }
    Job:
      type: object
      required: [job_id, operation, status, created_at, updated_at, reserved_units, charged_units]
      properties:
        job_id: { type: string }
        operation: { $ref: '#/components/schemas/Operation' }
        stem_count: { $ref: '#/components/schemas/StemCount' }
        status: { type: string, enum: [queued, running, canceling, completed, failed, canceled] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
        reserved_units: { $ref: '#/components/schemas/Units' }
        charged_units: { $ref: '#/components/schemas/Units' }
        queue_ms: { type: integer, minimum: 0 }
        processing_ms: { type: integer, minimum: 0 }
        artifacts_available_until: { type: string, format: date-time }
        artifacts:
          type: array
          description: Four aligned stems and a backing track for separation. Generation returns two numbered takes.
          items: { $ref: '#/components/schemas/Artifact' }
        error: { $ref: '#/components/schemas/Error' }
    Artifact:
      type: object
      required: [kind, file_name, content_type, duration_seconds, sample_rate_hz, channels, download_url, url_expires_at]
      properties:
        kind: { type: string, enum: [vocals, drums, bass, other, backing, take] }
        take_number: { type: integer, enum: [1, 2] }
        file_name: { type: string }
        content_type: { const: audio/wav }
        duration_seconds: { type: number, exclusiveMinimum: 0 }
        sample_rate_hz: { type: integer, minimum: 1 }
        channels: { const: 2 }
        download_url: { type: string, format: uri }
        url_expires_at: { type: string, format: date-time }
    Error:
      type: object
      required: [code, message]
      properties:
        code: { type: string }
        message: { type: string }
        retryable: { type: boolean }
