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

# Apply a music track to a rendered fragment

> Kicks off an async Cloud Run job.

<Note>
  A `200` here means the job *started*, not that it's done. Poll `GET` on this same path until `status` is `completed` or `failed`, then re-fetch `signed-video` for the updated URL.
</Note>


## OpenAPI

````yaml /openapi.json post /fragments/{fragmentId}/music
openapi: 3.1.0
info:
  title: Mathify API
  version: 1.0.0
  description: >-
    Public REST API for generating and retrieving Mathify math animations. See
    app/api/v1/README.md in the source repo for the underlying implementation.
servers:
  - url: https://mathify.dev/api/v1
security: []
tags:
  - name: Projects
  - name: Messages
  - name: Fragments
  - name: Music
  - name: Evals
paths:
  /fragments/{fragmentId}/music:
    post:
      tags:
        - Music
      summary: Apply a music track to a rendered fragment
      description: Kicks off an async Cloud Run job.
      operationId: applyMusic
      parameters:
        - in: path
          name: fragmentId
          schema:
            type: string
          required: true
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApplyMusicBody'
      responses:
        '200':
          description: Music job started
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApplyMusicResponse'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Missing or invalid API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '403':
          description: >-
            A valid API key is required, or the account has no credits balance
            (see https://mathify.dev/pricing)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '409':
          description: A music job is already running for this fragment
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '422':
          description: Fragment/track not ready or not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '502':
          description: Upstream render backend error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
      security:
        - apiKeyHeader: []
        - bearerAuth: []
components:
  schemas:
    ApplyMusicBody:
      description: 'Provide either trackSlug or random: true.'
      type: object
      properties:
        trackSlug:
          type: string
          minLength: 1
        random:
          type: boolean
        volume:
          default: 0.8
          type: number
          minimum: 0
          maximum: 1
        loopAudio:
          default: true
          type: boolean
    ApplyMusicResponse:
      type: object
      properties:
        ok:
          type: boolean
          const: true
        trackSlug:
          type: string
        track:
          type: object
          properties:
            title:
              type: string
            artist:
              type: string
            mood:
              type: string
          required:
            - title
            - artist
            - mood
          additionalProperties: false
      required:
        - ok
        - trackSlug
        - track
      additionalProperties: false
    ErrorResponse:
      type: object
      properties:
        error:
          example: Project not found
          type: string
      required:
        - error
      additionalProperties: false
  securitySchemes:
    apiKeyHeader:
      type: apiKey
      in: header
      name: x-api-key
      description: >-
        Your Mathify API key (`mk_live_<64 hex chars>`), generated at
        /settings/api-keys.
    bearerAuth:
      type: http
      scheme: bearer
      description: 'Same token as apiKeyHeader, passed as `Authorization: Bearer <token>`.'

````