openapi: 3.1.0
info:
  title: OpenMusicMetadata API
  version: 1.0.0
  description: |
    Canonical metadata for songs, albums, and artists.

    Requests are rate limited per client IP with a burst of 20 requests, refilling one request every 50 ms.
  license:
    name: AGPL-3.0

servers:
  - url: https://api.vleer.app/metadata/v1
    description: Production server

tags:
  - name: Catalog
  - name: Identify
  - name: Search

paths:
  /catalog:
    get:
      tags: [Catalog]
      summary: Get catalog resources
      operationId: getCatalogResources
      description: |
        Returns matching resources with all available attributes and relationships. Use exactly one of ids, isrc, or upc.

        - ids returns resources in request order. Duplicate ids are returned once per occurrence. Invalid and missing ids are omitted from data.
        - isrc and upc return resources ordered by id. Unknown values are omitted from data. An isrc matching several songs returns only the 5 most popular.
        - Songs without an artist or album, and albums without an artist, are treated as missing.
      parameters:
        - $ref: "#/components/parameters/CatalogIds"
        - $ref: "#/components/parameters/CatalogIsrc"
        - $ref: "#/components/parameters/CatalogUpc"
      responses:
        "200":
          description: Resource list
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceList"
        "400":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/TooManyRequests"
        "500":
          $ref: "#/components/responses/Error"
        "503":
          $ref: "#/components/responses/Error"
        "504":
          $ref: "#/components/responses/Error"

  /catalog/{id}:
    get:
      tags: [Catalog]
      summary: Get one catalog resource
      operationId: getCatalogResource
      description: Returns one resource with all available attributes and relationships.
      parameters:
        - $ref: "#/components/parameters/ResourceId"
      responses:
        "200":
          description: Resource
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceSingle"
        "400":
          $ref: "#/components/responses/Error"
        "404":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/TooManyRequests"
        "500":
          $ref: "#/components/responses/Error"
        "503":
          $ref: "#/components/responses/Error"
        "504":
          $ref: "#/components/responses/Error"

  /identify/{type}:
    get:
      tags: [Identify]
      summary: Identify one metadata resource
      operationId: identifyResource
      description: |
        Finds the best matching OpenMusicMetadata resource and returns it with all available attributes and relationships.

        Songs use name, artist, album, and duration. Albums use name and artist. Artists use name. Parameters not used by the type are ignored.

        Matching can be narrowed top-down with ids of already matched resources: artist_id for songs and albums, album_id for songs. Only the given scope is searched and the matching name parameter is not used for ranking. No match inside the scope is a 404. Ids never narrow upward, so a wrong song match cannot block matching its album or artist.

        Candidates are ranked by fuzzy name, artist, and album similarity plus popularity. For songs, a duration within 50 ms of a candidate adds a ranking bonus that grows as the difference shrinks. Candidates outside that window are not penalized.
      parameters:
        - name: type
          in: path
          required: true
          schema:
            $ref: "#/components/schemas/ResourceType"
        - name: name
          in: query
          required: true
          description: Resource name.
          schema:
            type: string
            minLength: 1
            maxLength: 256
        - name: album
          in: query
          required: false
          description: Album name. Used to identify songs.
          schema:
            type: string
            maxLength: 256
        - name: artist
          in: query
          required: false
          description: Artist name. Used to identify songs and albums.
          schema:
            type: string
            maxLength: 256
        - name: artist_id
          in: query
          required: false
          description: OpenMusicMetadata id of an already matched artist, in the form omm:artist:ID. Restricts matching to songs and albums by that artist instead of the whole catalog, which is more accurate. Takes precedence over artist. Ignored for artists. A malformed value is rejected with 400.
          schema:
            type: string
            pattern: "^omm:artist:[0-9]+$"
            example: omm:artist:1005712044
        - name: album_id
          in: query
          required: false
          description: OpenMusicMetadata id of an already matched album, in the form omm:album:ID. Restricts matching to songs on that album instead of the whole catalog, which is more accurate. Takes precedence over album. Ignored for albums and artists. A malformed value is rejected with 400.
          schema:
            type: string
            pattern: "^omm:album:[0-9]+$"
            example: omm:album:1440857781
        - name: duration
          in: query
          required: false
          description: Song duration in milliseconds. Used to identify songs. Values of 0 or less are ignored. A non-integer value is rejected with 400.
          schema:
            type: integer
            format: int32
            minimum: 1
      responses:
        "200":
          description: Identified resource
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceSingle"
        "400":
          $ref: "#/components/responses/Error"
        "404":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/TooManyRequests"
        "500":
          $ref: "#/components/responses/Error"
        "503":
          $ref: "#/components/responses/Error"
        "504":
          $ref: "#/components/responses/Error"

  /search/{type}:
    get:
      tags: [Search]
      summary: Search metadata resources
      operationId: searchResources
      description: |
        Returns the best matching OpenMusicMetadata resources of one type, ranked best first, so a client can offer alternatives when /identify picked the wrong one.

        Takes the same query parameters and uses the same ranking as /identify. Parameters not used by the type are ignored. The first result is normally the resource /identify would return. /identify answers 404 when its pick cannot be returned, while this skips such resources.

        artist_id and album_id narrow results the same way as in /identify.

        No match is a 200 with an empty list, not a 404.
      parameters:
        - name: type
          in: path
          required: true
          schema:
            $ref: "#/components/schemas/ResourceType"
        - name: name
          in: query
          required: true
          description: Resource name.
          schema:
            type: string
            minLength: 1
            maxLength: 256
        - name: album
          in: query
          required: false
          description: Album name. Used for songs.
          schema:
            type: string
            maxLength: 256
        - name: artist
          in: query
          required: false
          description: Artist name. Used for songs and albums.
          schema:
            type: string
            maxLength: 256
        - name: artist_id
          in: query
          required: false
          description: OpenMusicMetadata id of an already matched artist, in the form omm:artist:ID. Restricts matching to songs and albums by that artist instead of the whole catalog, which is more accurate. Takes precedence over artist. Ignored for artists. A malformed value is rejected with 400.
          schema:
            type: string
            pattern: "^omm:artist:[0-9]+$"
            example: omm:artist:1005712044
        - name: album_id
          in: query
          required: false
          description: OpenMusicMetadata id of an already matched album, in the form omm:album:ID. Restricts matching to songs on that album instead of the whole catalog, which is more accurate. Takes precedence over album. Ignored for albums and artists. A malformed value is rejected with 400.
          schema:
            type: string
            pattern: "^omm:album:[0-9]+$"
            example: omm:album:1440857781
        - name: duration
          in: query
          required: false
          description: Song duration in milliseconds. Used for songs. Values of 0 or less are ignored. A non-integer value is rejected with 400.
          schema:
            type: integer
            format: int32
            minimum: 1
        - name: limit
          in: query
          required: false
          description: Maximum number of results.
          schema:
            type: integer
            minimum: 1
            maximum: 10
            default: 10
      responses:
        "200":
          description: Ranked resources, best match first
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceList"
        "400":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/TooManyRequests"
        "500":
          $ref: "#/components/responses/Error"
        "503":
          $ref: "#/components/responses/Error"
        "504":
          $ref: "#/components/responses/Error"

components:
  responses:
    Error:
      description: Error response
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"

    TooManyRequests:
      description: Rate limit exceeded
      headers:
        retry-after:
          description: Seconds to wait before retrying.
          schema:
            type: integer
        x-ratelimit-after:
          description: Seconds to wait before retrying.
          schema:
            type: integer
      content:
        text/plain:
          schema:
            type: string
          example: Too Many Requests! Wait for 1s

  parameters:
    ResourceId:
      name: id
      in: path
      required: true
      description: OpenMusicMetadata id.
      schema:
        $ref: "#/components/schemas/Id"

    CatalogIds:
      name: ids
      in: query
      required: false
      description: Comma-separated OpenMusicMetadata ids. Maximum 100 values and 10000 characters. Cannot be combined with isrc or upc.
      schema:
        type: string
        maxLength: 10000
      example: omm:song:1482910533,omm:album:1193847211

    CatalogIsrc:
      name: isrc
      in: query
      required: false
      description: Comma-separated song ISRC values, matched case-insensitively. Maximum 100 values and 10000 characters. Cannot be combined with ids or upc.
      schema:
        type: string
        maxLength: 10000
      example: USRC17607839

    CatalogUpc:
      name: upc
      in: query
      required: false
      description: Comma-separated album UPC values. Maximum 100 values and 10000 characters. Cannot be combined with ids or isrc.
      schema:
        type: string
        maxLength: 10000
      example: "602455878540"

  schemas:
    ResourceType:
      type: string
      enum: [song, album, artist]

    Id:
      type: string
      description: OpenMusicMetadata id in the form omm:TYPE:ID, where ID is a positive integer without leading zeros, currently 10 digits and at most 18. Treat it as an opaque string, do not parse it and compare ids as text.
      pattern: "^omm:(song|album|artist):[1-9][0-9]{0,17}$"
      examples:
        - omm:song:1482910533
        - omm:album:1193847211
        - omm:artist:1005712044

    ArtworkUrl:
      type: string
      description: Artwork URL. It may contain {w} and {h} placeholders for client-selected width and height.

    ReleaseDate:
      type: string
      description: Date string in YYYY, YYYY-MM, or YYYY-MM-DD form.

    Genres:
      type: array
      minItems: 1
      items:
        type: string

    Song:
      type: object
      required: [id, type, attributes, relationships]
      properties:
        id:
          $ref: "#/components/schemas/Id"
        type:
          const: song
        attributes:
          type: object
          required: [name, artistNames]
          properties:
            name:
              type: string
            albumName:
              type: string
              description: Name of the first album.
            artistNames:
              type: array
              description: Artist names, ordered by name. Full artists are in relationships.
              items:
                type: string
            isrc:
              type: string
            artworkUrl:
              $ref: "#/components/schemas/ArtworkUrl"
            trackNumber:
              type: integer
              minimum: 1
            discNumber:
              type: integer
              minimum: 1
            genres:
              $ref: "#/components/schemas/Genres"
            releaseDate:
              $ref: "#/components/schemas/ReleaseDate"
            durationMs:
              type: integer
              minimum: 1
        relationships:
          type: object
          description: Related resources. Albums nested here never have relationships.
          required: [albums, artists]
          properties:
            albums:
              $ref: "#/components/schemas/AlbumList"
            artists:
              $ref: "#/components/schemas/ArtistList"

    Album:
      type: object
      required: [id, type, attributes]
      properties:
        id:
          $ref: "#/components/schemas/Id"
        type:
          const: album
        attributes:
          type: object
          required: [name, trackCount, artistNames]
          properties:
            name:
              type: string
            trackCount:
              type: integer
              minimum: 0
            artistNames:
              type: array
              description: Artist names, ordered by name. Full artists are in relationships.
              items:
                type: string
            artworkUrl:
              $ref: "#/components/schemas/ArtworkUrl"
            upc:
              type: string
            label:
              type: string
            genres:
              $ref: "#/components/schemas/Genres"
            releaseDate:
              $ref: "#/components/schemas/ReleaseDate"
        relationships:
          type: object
          description: Present except when the album is nested in a song's relationships.
          required: [artists]
          properties:
            artists:
              $ref: "#/components/schemas/ArtistList"

    Artist:
      type: object
      required: [id, type, attributes]
      properties:
        id:
          $ref: "#/components/schemas/Id"
        type:
          const: artist
        attributes:
          type: object
          required: [name]
          properties:
            name:
              type: string
            artworkUrl:
              $ref: "#/components/schemas/ArtworkUrl"
            genres:
              $ref: "#/components/schemas/Genres"

    Resource:
      oneOf:
        - $ref: "#/components/schemas/Song"
        - $ref: "#/components/schemas/Album"
        - $ref: "#/components/schemas/Artist"
      discriminator:
        propertyName: type
        mapping:
          song: "#/components/schemas/Song"
          album: "#/components/schemas/Album"
          artist: "#/components/schemas/Artist"

    ResourceSingle:
      type: object
      required: [data]
      properties:
        data:
          $ref: "#/components/schemas/Resource"

    ResourceList:
      type: object
      required: [data]
      properties:
        data:
          type: array
          items:
            $ref: "#/components/schemas/Resource"

    AlbumList:
      type: object
      required: [data]
      properties:
        data:
          type: array
          items:
            $ref: "#/components/schemas/Album"

    ArtistList:
      type: object
      required: [data]
      properties:
        data:
          type: array
          items:
            $ref: "#/components/schemas/Artist"

    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [status, message]
          properties:
            status:
              type: integer
              minimum: 400
              maximum: 599
            message:
              type: string
      example:
        error:
          status: 400
          message: Provide exactly one of ids, isrc, or upc
