Skip to content

feat: Add Apple Music integration#3

Open
STop211650 wants to merge 15 commits intosteipete:mainfrom
STop211650:feature/apple-music-clean
Open

feat: Add Apple Music integration#3
STop211650 wants to merge 15 commits intosteipete:mainfrom
STop211650:feature/apple-music-clean

Conversation

@STop211650
Copy link
Copy Markdown

Summary

  • Add Apple Music search and playback support for Sonos speakers
  • Authentication via browser-extracted tokens (~6 month lifespan)
  • Enqueue content via Sonos SMAPI for seamless playback

New Commands

  • sonos auth applemusic login/status/logout - Token management
  • sonos search applemusic <query> - Search Apple Music catalog
  • sonos play applemusic <query> - Search and play on Sonos

Files Added

Path Description
internal/applemusic/ Apple Music API client, auth flow, token storage
internal/cli/auth_applemusic.go Auth CLI commands
internal/cli/search_applemusic.go Search CLI command
internal/cli/play_applemusic.go Play CLI command
internal/sonos/applemusic.go URL parsing, Sonos enqueue logic
docs/applemusic.md Detailed documentation

Authentication

Tokens are extracted from the Apple Music web player:

  1. Sign in at https://music.apple.com
  2. Open Developer Tools → Console
  3. Run MusicKit.getInstance().developerToken and MusicKit.getInstance().musicUserToken
  4. Paste tokens when prompted by sonos auth applemusic login

Prerequisites

  • Apple Music must be linked in the Sonos app
  • Active Apple Music subscription

🤖 Generated with Claude Code

Shyam Toprani and others added 15 commits December 20, 2025 19:20
- Add Apple Music client with authentication and token storage
- Add CLI commands for Apple Music auth, search, and playback
- Add project documentation (CLAUDE.md, AGENTS.md)
- Add beads workflow configuration
- Update existing CLI files for music service integration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Documents the Apple Music integration including authentication,
search, playback commands, architecture, and troubleshooting.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add GetTransportSettings and SetPlayMode to the Sonos client for
controlling playback modes. Includes new CLI command `sonos mode`
with options: get, shuffle, shuffle-norepeat, repeat, repeat-one, normal.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add references section with links to token extraction guides and
official Apple documentation for MusicKit integration.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Update mode command Use field to list all six modes
- Add input validation for PlayMode values in SetPlayMode

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add support for searching and playing Apple Music content on Sonos speakers:

- Search Apple Music catalog (songs, albums, playlists, artists)
- Play Apple Music content directly on Sonos speakers
- Authentication via browser-extracted tokens (~6 month lifespan)
- Enqueue via Sonos SMAPI for seamless playback

New commands:
- `sonos auth applemusic login/status/logout` - Token management
- `sonos search applemusic <query>` - Search Apple Music
- `sonos play applemusic <query>` - Search and play on Sonos

Requires Apple Music to be linked in the Sonos app.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use service 52231 for DIDL metadata descriptors (not 204)
- Add proper item ID prefixes (10032020 for tracks, 1004206c for albums)
- URL-encode colons in item IDs to match Sonos format

This fixes queue display showing "Q:0/1" instead of track titles.
The URI uses sid=204 for routing, but metadata needs service 52231.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use service 52231 for DIDL metadata descriptors (not 204)
- Add proper item ID prefixes (10032020 for tracks, 1004206c for albums)
- URL-encode colons in item IDs to match Sonos format

This fixes queue display showing "Q:0/1" instead of track titles.
The URI uses sid=204 for routing, but metadata needs service 52231.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ayushsdev
Copy link
Copy Markdown

Was looking for this support too, thanks!

@cezarderevlean
Copy link
Copy Markdown

cezarderevlean commented Feb 27, 2026

Tried to use this locally, but there are a few issues.

1. Tries to open Safari / Apple Music app, even though default browser is Chrome

Should probably just show the link and suggest to open it manually in the browser, because probably the system treats is as a deeplink otherwise.

Opening Apple Music in your browser...

2. Instructions are outdated

Outdated:

To extract your Apple Music token:

  1. Sign in to Apple Music at https://music.apple.com
  2. Open browser Developer Tools (F12 or Cmd+Option+I)
  3. Go to the "Application" tab (Chrome) or "Storage" tab (Firefox)
  4. Find "Local Storage" → "https://music.apple.com"
  5. Look for a key containing "music.user.token" or similar
  6. Copy the token value

Alternatively, in the Console tab, run:
localStorage.getItem('music.ampwebplay.media-user-token')

Correct:

After signing in, run in browser console:
MusicKit.getInstance().developerToken
MusicKit.getInstance().musicUserToken

Paste developer token (JWT, starts with eyJ...):

Paste user token (starts with Av...):

3. User token doesn't necessarily start with Av (mine seems to start with As)

The token is a long string starting with something like "Aw..." or similar.

Paste user token (starts with Av...):

4. Date when token expires is wrong:

Apple Music tokens saved successfully!
Token expires around: 0001-01-01T00:00:00Z

5. After successfully authenticating, sometimes I get an error.

Did Apple restricted the API?

╰─❯ sonos search applemusic papaoutai
Error: apple music search failed: apple music api error: API capacity exceeded (status 429)

Ran a security check with MiniMax M2.5 @ OpenCode:

Security Review: No Issues Found
Your implementation is safe. Here's what I verified:

  1. Tokens only go to Apple - The client sends tokens only to https://amp-api.music.apple.com (Apple's official API). No third-party servers.
  2. Local storage only - Tokens are stored at ~/.config/sonoscli/applemusic_token.json with secure permissions (600/700).
  3. No logging/telemetry - No token logging, no external analytics, no unexpected network calls.
  4. Minimal dependencies - Just cobra CLI framework + stdlib net/http.
    The flow is:
  • Browser opens to music.apple.com (Apple's site)
  • You extract tokens from your browser's localStorage
  • You paste them into the CLI
  • CLI uses them only for API calls directly to Apple
    You're not at risk. This is a standard pattern for personal CLI tools that want to use Apple Music's web API without going through Apple's official MusicKit SDK (which requires app registration).

@STop211650
Copy link
Copy Markdown
Author

STop211650 commented Feb 27, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants