Skip to content

The API and events

Every client of Music Assistant, the web frontend included, uses the same API. This page helps you understand how a command travels from a client to the code that handles it, and how changes reach clients as events. To build on it, go to Build on the API.

sequenceDiagram
  participant C as Client
  participant W as WebsocketClientHandler
  participant M as mass
  participant X as Controller or provider
  C->>W: connect to /ws
  W-->>C: ServerInfoMessage with schema_version
  C->>W: auth command with a token
  W-->>C: authenticated, subscribed to events
  C->>W: CommandMessage with message_id, command, args
  W->>M: look up command_handlers
  W->>X: call the handler in its own task
  X-->>W: result or MusicAssistantError
  W-->>C: SuccessResultMessage or ErrorResultMessage
  X->>M: signal_event
  M->>W: event to subscribers
  W-->>C: event, if this user may see it

Time runs from top to bottom. Solid arrows are calls or messages, dashed arrows are answers.

  • Connect. A client opens a WebSocket to /ws on the webserver (port 8095 by default). The server answers right away with a ServerInfoMessage: its id, version and API schema version.
  • Authenticate. The client sends the auth command with a token. A token comes from a login (auth/login or POST /auth/login). Each user has a role, and the role decides which commands they may run.
  • Command. A CommandMessage carries a message_id, the command name and its arguments. The server finds the handler in mass.command_handlers, checks that the user may run it, converts the arguments to the types the method expects, and runs it in its own task. So one slow command does not block the next.
  • Result. The answer carries the same message_id. A very long listing comes in parts, each marked partial. A MusicAssistantError comes back as an ErrorResultMessage with an error code and a translated message.
  • Event. When state changes, the owner calls mass.signal_event with an EventType, an object id and data. Each authenticated connection is subscribed to all events and filters them per user: a user only gets events for players they have access to, for example.

A command is a method marked with the @api_command decorator from music_assistant/helpers/api.py, for example @api_command("player_queues/play_media"). The decorator only marks the method. At startup, after the controllers are set up, mass._register_api_commands() collects the marked methods of mass and the controllers into mass.command_handlers. Some commands are registered later: providers register theirs in loaded_in_mass().

The decorator also says whether a command needs authentication and which scope it needs (required_scope).

  • HTTP. POST /api takes the same command message and returns the result, with the token in an Authorization: Bearer header. Useful for scripts.
  • API docs. Every server serves its own API documentation at /api-docs, generated from the registered commands, with a command reference, the schemas and an OpenAPI spec.
  • Remote access. A client outside the home network connects over WebRTC, through the signaling server, and the server bridges it to the same local WebSocket API.
  • Home Assistant. When the server runs as the Music Assistant app for Home Assistant, Ingress connections are authenticated from Home Assistant instead of with a token.

The frontend ships in lockstep with the server and never checks versions. Clients that are not bundled, such as the Home Assistant integration, the apps and third-party tools, read the version from ServerInfoMessage:

  • schema_version (API_SCHEMA_VERSION in constants.py) goes up when such a client gets something new to gate on: a new command, a new or changed parameter, a new field the server sends.
  • min_supported_schema_version (MIN_SCHEMA_VERSION) goes up only for a breaking change, which breaks every client in the field that has not been updated.

The comments next to the two constants in constants.py say exactly when to bump which.

Music Assistant is a project of the Open Home Foundation.