Spotify service integration
Spotifast connects to Spotify through separate service paths for account data, playback, and catalogue data. Browser-based Web API sign-in supplies account access. A librespot playback session supplies audio playback and some data that Spotify does not expose through the Web API. A gateway selects the path that can serve each request while keeping all active grants tied to one Spotify account. See Authentication and credentials and Spotify API client and gateway for the child pages on these areas.
Service boundaries
The integration treats Spotify as a set of related services rather than one uniform API. This split matters because each service has different credentials, limits, and feature coverage.
Web API access
The Web API provides the public account, playback-control, library, playlist, and catalogue features. Spotifast sends these requests with a bearer token from a Web API grant and converts Spotify responses into the data used by the rest of the app. The main capabilities include:
- Account profile and plan information.
- Saved items, followed artists, top items, and recent listening.
- Playlist discovery and playlist changes.
- Artist, album, track, show, episode, search, and recommendation data where Spotify permits it.
- Playback state, device discovery, playback controls, and queue changes.
The public API does not cover every feature that Spotify clients expose. Newer personal apps can also have narrower access than older shared grants. The gateway therefore treats Web API access as one part of the integration, not as the source for every Spotify feature.
References: src/…/client.rs• L721-1327 src/auth.rs• L43-65 docs/…/what-spotify-allows.md• L16-56
Playback session
The playback session signs in as a Spotify client and supports local audio playback and Spotify Connect. It also provides private or session-backed data such as playlist folders, playlists that a personal app cannot read, related artists, and selected personalized shelves. These reads use the playback account and can fill gaps in the Web API.
Playback-session data is not a general replacement for the Web API. Some session reads are private Spotify behavior and can change without notice. Spotifast uses them for features they uniquely support and falls back to a public Web API path when a suitable fallback exists.
References: src/player.rs• L1-8 src/session_reads.rs• L1-4 docs/…/what-spotify-allows.md• L58-160
Partner and private reads
Some catalogue details come from private requests used by Spotify clients. These requests run through the playback session and an authenticated HTTP transport. They support data such as related artists, play counts, monthly listeners, upcoming releases, and ordered preview rows.
Private reads are supplementary. If Spotify rejects or changes one, Spotifast either uses a Web API fallback or leaves that part of the view unavailable. Private writes are limited to user actions such as changing a pre-save or hiding a song in a playlist.
References: src/…/client.rs• L461-507 src/session_reads.rs• L33-112 src/session_reads.rs• L393-469
Authentication model
Spotifast uses browser authorization so it does not handle the user's Spotify password. The browser returns a one-time code to a loopback listener. Spotifast exchanges that code for tokens, refreshes access tokens before expiry, and keeps durable grants in protected credential storage.
Web API grants
There are two Web API grant roles:
| Grant | Purpose | Lifetime and source |
|---|---|---|
| Personal | The listener's account, playback controls, user data, playlists, and catalogue requests | Created with the listener's Spotify app and renewed with its refresh token |
| Shared | Legacy access for features that a newer personal app may not serve | Restored when an older grant exists; no new shared sign-in is created |
The personal grant uses the Client ID entered by the listener. Spotify authorizes it with PKCE and the fixed loopback redirect URI http://127.0.0.1:8989/login. The playback grant is separate because the identity used for streaming is not suitable for normal Web API traffic.
References: src/auth.rs• L1-17 src/auth.rs• L77-113 src/backend.rs• L3328-3442
Playback grant
The playback grant gives librespot the reusable credential needed to start a Spotify session and stream audio. It is stored separately from Web API grants because its identity and protocol role are different. Playback requires the account to support Spotify Premium playback.
The playback account is also the identity used for session-backed reads. When Spotifast combines a Web API grant with playback data, the gateway checks that both grants belong to the same Spotify account.
References: src/auth.rs• L33-45 src/credentials.rs• L25-64 src/backend.rs• L3868-3925
Credential lifecycle
A successful Web API grant is first checked against Spotify's account endpoint. Spotifast then marks its session ready and may persist the grant. Refresh keeps the access token usable as it ages, but refresh cannot add permissions that the original browser approval did not grant.
Sign-out revokes the local grant, clears the active session, and invalidates work that belongs to the old authorization. This prevents an old request or delayed storage operation from restoring the previous account.
References: src/…/client.rs• L109-215 src/credentials.rs• L721-759 src/backend.rs• L3135-3306
Request routing
The API gateway owns the choice between shared Web API access, personal Web API access, and the playback session. Callers describe a capability rather than choosing credentials directly. The gateway uses account state and known playlist access to select a source.
This diagram shows how a user request moves from a product capability to the service path that can answer it.
flowchart LR
A[Spotifast capability] --> B[Gateway classifies operation]
B --> C{Source can serve it?}
C -->|Personal Web API| D[Authenticated Web API client]
C -->|Shared Web API| E[Legacy Web API client]
C -->|Playback session| F[librespot and private reads]
D --> G[Account, playback control, library, catalogue]
E --> G
F --> H[Playback and session-only data]
D -. fallback .-> F
F -. public fallback .-> E
Capability selection
Personal Web API access is preferred for account data, user data, playback control, playlist creation, and ordinary catalogue work when it is ready. Shared access remains important for legacy coverage that personal apps may not have, such as some Spotify-owned or external playlists.
The playback session serves playlist library views and selected catalogue reads when the Web API cannot provide them. If no shared grant exists, the personal grant handles the operations it can serve and the playback session covers the remaining session-backed reads.
References: src/…/gateway.rs• L103-143 src/backend.rs• L5088-5160 src/session_reads.rs• L816-887
Playlist access
Playlist access is learned from returned playlist metadata. Spotifast classifies a playlist as owned, collaborative, external, or unknown. That classification guides later metadata, item, and mutation requests because Spotify may allow a personal app to handle the account's own playlists but require the shared or playback path for other playlists.
Unknown access uses the broader path first. A later successful response updates the classification. A permission failure clears the classification so the next request can choose again instead of trusting stale information.
References: src/…/gateway.rs• L76-155 src/…/gateway.rs• L415-446 src/backend.rs• L5163-5223
Account pairing
The shared Web API grant, personal Web API grant, and playback session must describe the same Spotify account when Spotifast uses them together. A mismatch is rejected rather than silently mixing one user's private data with another user's playback state.
During reauthorization, the old session becomes unavailable before the new provider is installed. Waiting requests keep their authorization generation, so a sign-out or account change cannot release them with the wrong client.
References: src/…/gateway.rs• L223-301 src/…/gateway.rs• L368-407 src/backend.rs• L3252-3267
API communication
The Web API client provides the shared transport rules for authenticated calls. It adds the current bearer token, limits concurrent requests, refreshes a rejected token once, and turns Spotify responses into errors that the product can act on. It also sends authenticated partner catalogue queries through a separate service path with its own cooldown, so a throttle in one path does not block the other.
Request protection
The client protects Spotify and the user experience with a small set of common rules:
- Requests wait for a concurrency permit.
- Foreground and background work use separate request budgets.
- A
401can trigger one token refresh and retry. - Web API and partner requests keep separate cooldowns while honoring
Retry-After. - Development Mode quota exhaustion is reported separately from normal rate limiting.
- Safe
GETrequests can retry once after a server error. - Queue writes are serialized so repeated tracks keep their order and a rejected append can retry safely.
The client also reports network activity to the interface. Background work uses a separate budget so a slow catalogue refresh does not block foreground playback or account actions.
References: src/…/client.rs• L22-43 src/…/client.rs• L378-507 src/…/client.rs• L531-719
Response fallbacks
The backend chooses a fallback when a session-backed read cannot answer. A definitive Spotify result, such as a missing or forbidden playlist, is returned to the user. A transport or decode failure can cause a public Web API read when that path has equivalent coverage.
This keeps private service changes local to the feature that uses them. It also avoids spending shared Web API quota when the playback session already supplied a complete answer.
References: src/session_reads.rs• L160-195 src/backend.rs• L5231-5265 src/backend.rs• L5320-5379
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the requested Web API scopes aligned with every account, library, playlist, and playback operation that the gateway can route to a Web API client.
- Keep the personal redirect URI in the authorization flow and the Spotify app setup guidance identical.
- When adding a capability, update its gateway operation classification and define its fallback when the selected source is unavailable.
- Preserve the same-account check when a feature combines a playback session with a Web API token.
- Keep rate-limit handling separate for shared, personal, and private partner traffic so one source does not block another source.
- When a private response changes, check both its Web API fallback and the user-visible behavior when no fallback exists.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Personal app lacks a requested permission | The grant cannot serve that operation until the user authorizes again | Check granted scopes before restoring the session |
| Personal app is restricted by Spotify Development Mode | Some Spotify-owned or external catalogue and playlist requests fail | Route those operations to the shared grant or playback session when available |
| Shared Web API grant is expired or revoked | Shared-only coverage disappears | Forget the rejected grant and let personal or session-backed paths take over |
Web API or partner service returns 429 | New requests may fail or worsen the matching service cooldown | Honor Retry-After and keep bounded, source-specific cooldowns |
| Playback and Web API grants belong to different accounts | Private data and playback state could be mixed | Reject installation when account IDs differ |
| Private session response is unavailable or changes shape | The affected enrichment is missing or falls back to the Web API | Classify the failure as retryable and keep a public fallback where possible |
| Credential storage is locked or fails verification | The current run may work but the grant cannot be restored safely | Report the storage error and require sign-in after the grant is lost |
Key files
| File | Why to read it |
|---|---|
| src/auth.rs | Defines the browser authorization flows, Web API scopes, token refresh, and grant state. |
| src/credentials.rs | Defines protected persistence, revocation, and authorization generations for Spotify grants. |
| src/…/gateway.rs | Defines account pairing, capability routing, playlist access classification, and session state. |
| src/…/client.rs | Defines authenticated Web API and partner transport, rate limits, token refresh, and public account, playback, playlist, and catalogue calls. |
| src/backend.rs | Connects sign-in, playback, gateway routing, fallbacks, and product requests. |
| src/player.rs | Shows the role of the librespot playback session and Spotify Connect state. |
| src/session_reads.rs | Shows the session-backed catalogue and playlist reads that supplement the Web API. |
| docs/…/what-spotify-allows.md | Gives the product-level boundary between Web API, playback-session, private, and unavailable features. |