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.
One command and one event
Section titled “One command and one event”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
How to read this
Section titled “How to read this”Time runs from top to bottom. Solid arrows are calls or messages, dashed arrows are answers.
- Connect. A client opens a WebSocket to
/wson the webserver (port 8095 by default). The server answers right away with aServerInfoMessage: its id, version and API schema version. - Authenticate. The client sends the
authcommand with a token. A token comes from a login (auth/loginorPOST /auth/login). Each user has a role, and the role decides which commands they may run. - Command. A
CommandMessagecarries amessage_id, the command name and its arguments. The server finds the handler inmass.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 markedpartial. AMusicAssistantErrorcomes back as anErrorResultMessagewith an error code and a translated message. - Event. When state changes, the owner calls
mass.signal_eventwith anEventType, 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.
How a command finds its handler
Section titled “How a command finds its handler”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).
Other ways in
Section titled “Other ways in”- HTTP.
POST /apitakes the same command message and returns the result, with the token in anAuthorization: Bearerheader. 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 schema version
Section titled “The schema version”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_VERSIONinconstants.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.
Read deeper
Section titled “Read deeper”- webserver README: authentication, login flows, remote access and request flows.
- helpers/api.py:
api_commandand argument parsing. - constants.py: the schema version rules.
- Build on the API and The listing contract
- Inside the server and The repositories
Music Assistant is a project of the Open Home Foundation.