Spotify Web API
A wrapper for the Spotify Web API written in Rust.
Spotify Web API enables the creation of applications that can interact with Spotify's streaming service, such as retrieving content metadata, getting recommendations, creating and managing playlists, or controlling playback.
Adding as a Dependency
You can add this to your project by running the command
Examples
There are more examples in the examples folder.
Client Credentials
use ;
Authorization Code with PKCE
use ;
use ;
async
Which OAuth flow should I use? (source)
Choosing one flow over the rest depends on the application you are building:
- In scenarios where storing the client secret is not safe (e.g. desktop, mobile apps or JavaScript web apps running in the browser), you can use the authorization code with PKCE, as it provides protection against attacks where the authorization code may be intercepted.
- For some applications running on the backend, such as CLIs or daemons, the system authenticates and authorizes the app rather than a user. For these scenarios, Client credentials is the typical choice. This flow does not include user authorization, so only endpoints that do not request user information (e.g. user profile data) can be accessed.
The following table summarizes the flows' behaviors:
| Flow | Access User Resources | Requires Secret Key (Server-Side) | Access Token Refresh |
|---|---|---|---|
| Authorization code with PKCE | Yes | No | Yes |
| Client credentials | No | Yes | No |
API
Supported endpoints are organized under the api module. To interact with an endpoint, you can use either the Query or AsyncQuery traits.
Queryis designed for blocking code, making it ideal for synchronous workflows or environments where asynchronous execution is unnecessary or not supported. Opt for this when simplicity is key, such as in single-threaded environments or scripts where blocking is acceptable.AsyncQueryis intended for asynchronous code and integrates seamlessly with an asynchronous runtime of your choice, such asTokioorasync-std. This approach is particularly useful when working in environments that benefit from non-blocking operations. Use this trait when building applications that require high concurrency or when interacting with other asynchronous code.
There are additional helpers to handle different cases:
api::ignore: Ignore the Spotify response (useful for POST or PUT endpoints).api::paged: Fetch results that are paginated.api::raw: Return the raw data from Spotify instead of deserializing into a structure.
You're not restricted to the predefined endpoints; you can define your own by implementing the Endpoint trait. See example.
All endpoints return data types chosen by the caller, provided these types implement serde's Deserialize trait. The library offers predefined structs in the model module, but you are free to use your own structs by implementing the Deserialize trait. This flexibility is particularly useful when a custom data structure better suits the your needs or when avoiding the overhead of deserializing the entire response is desirable. See example.
Feature Flags
A set of feature flags are available to customize the data models. These are enabled by default, but you can disable them to reduce the size of the compiled library or to avoid unnecessary data in your application.
markets- Enables theavailable_marketsfield in various models, such asTrack. This field contains a list of markets where the content is available.page_items- Enables the field in various models that contain paginated items, such as thetracksfield inPlaylist.
Implemented Endpoints
Format: [x] [Title] [Method] [Endpoint] [Spotify Docs]
Albums
- Get Album
GET/albums/{id}get-an-album - Get Several Albums
GET/albumsget-multiple-albums - Get Album Tracks
GET/albums/{id}/tracksget-an-albums-tracks - Get User's Saved Albums
GETme/albumsget-users-saved-albums - Save Albums for Current User
PUTme/albumssave-albums-user - Remove User's Saved Albums
DELETEme/albumsremove-albums-user - Check User's Saved Albums
GETme/albums/containscheck-users-saved-albums - Get New Releases
GET/browse/new-releasesget-new-releases
Artists
- Get Artist
GET/artists/{id}get-an-artist - Get Several Artists
GET/artistsget-multiple-artists - Get Artist's Albums
GET/artists/{id}/albumsget-an-artists-albums - Get Artist's Top Tracks
GET/artists/{id}/top-tracksget-an-artists-top-tracks
Audiobooks
- Get an Audiobook
GET/audiobooks/{id}get-an-audiobook - Get Several Audiobooks
GET/audiobooksget-multiple-audiobooks - Get Audiobook Chapters
GET/audiobooks/{id}/chaptersget-audiobook-chapters - Get User's Saved Audiobooks
GETme/audiobooksget-users-saved-audiobooks - Save Audiobooks for Current User
PUTme/audiobookssave-audiobooks-user - Remove User's Saved Audiobooks
DELETEme/audiobooksremove-audiobooks-user - Check User's Saved Audiobooks
GETme/audiobooks/containscheck-users-saved-audiobooks
Categories
- Get Several Browse Categories
GET/browse/categoriesget-categories - Get Single Browse Category
GET/browse/categories/{category_id}get-a-category
Chapters
- Get a Chapter
GET/chapters/{id}get-a-chapter - Get Several Chapters
GET/chaptersget-several-chapters
Episodes
- Get Episode
GET/episodes/{id}get-an-episode - Get Several Episodes
GET/episodesget-multiple-episodes - Get User's Saved Episodes
GETme/episodesget-users-saved-episodes - Save Episodes for Current User
PUTme/episodessave-episodes-user - Remove User's Saved Episodes
DELETEme/episodesremove-episodes-user - Check User's Saved Episodes
GETme/episodes/containscheck-users-saved-episodes
Genres
- Get Available Genre Seeds
GET/recommendations/available-genre-seedsget-recommendation-genres
Markets
- Get Available Markets
GET/marketsget-available-markets
Player
- Get Playback State
GET/me/playerget-information-about-the-users-current-playback - Transfer Playback
PUT/me/playertransfer-a-users-playback - Get Available Devices
GET/me/player/devicesget-a-users-available-devices - Get Currently Playing Track
GET/me/player/currently-playingget-the-users-currently-playing-track - Start/Resume Playback
PUT/me/player/playstart-a-users-playback - Pause Playback
PUT/me/player/pausepause-a-users-playback - Skip To Next
POST/me/player/nextskip-users-playback-to-next-track - Skip To Previous
POST/me/player/previousskip-users-playback-to-previous-track - Seek To Position
PUT/me/player/seekseek-to-position-in-currently-playing-track - Set Repeat Mode
PUT/me/player/repeatset-repeat-mode-on-users-playback - Set Playback Volume
PUT/me/player/volumeset-volume-for-users-playback - Toggle Playback Shuffle
PUT/me/player/shuffletoggle-shuffle-for-users-playback - Get Recently Played Tracks
GET/me/player/recently-playedget-recently-played - Get the User's Queue
GET/me/player/queueget-queue - Add Item to Playback Queue
POST/me/player/queueadd-to-queue
Playlists
- Get Playlist
GET/playlists/{playlist_id}get-playlist - Change Playlist Details
PUT/playlists/{playlist_id}change-playlist-details - Get Playlist Items
GET/playlists/{playlist_id}/tracksget-playlists-tracks - Update Playlist Items
PUT/playlists/{playlist_id}/tracksreorder-or-replace-playlists-tracks - Add Items to Playlist
POST/playlists/{playlist_id}/tracksadd-tracks-to-playlist - Remove Playlist Items
DELETE/playlists/{playlist_id}/tracksremove-tracks-playlist - Get Current User's Playlists
GET/me/playlistsget-a-list-of-current-users-playlists - Get User's Playlists
GET/users/{user_id}/playlistsget-list-users-playlists - Create Playlist
POST/users/{user_id}/playlistscreate-playlist - Get Playlist Cover Image
GET/playlists/{playlist_id}/imagesget-playlist-cover - Add Custom Playlist Cover Image
PUT/playlists/{playlist_id}/imagesupload-custom-playlist-cover
Search
- Search for Item
GET/searchsearch
Shows
- Get Show
GET/shows/{id}get-a-show - Get Several Shows
GET/showsget-multiple-shows - Get Show Episodes
GET/shows/{id}/episodesget-a-shows-episodes - Get User's Saved Shows
GETme/showsget-users-saved-shows - Save Shows for Current User
PUTme/showssave-shows-user - Remove User's Saved Shows
DELETEme/showsremove-shows-user - Check User's Saved Shows
GETme/shows/containscheck-users-saved-shows
Tracks
- Get Track
GET/tracks/{id}get-track - Get Several Tracks
GET/tracksget-several-tracks - Get User's Saved Tracks
GETme/tracksget-users-saved-tracks - Save Tracks for Current User
PUTme/trackssave-tracks-user - Remove User's Saved Tracks
DELETEme/tracksremove-tracks-user - Check User's Saved Tracks
GETme/tracks/containscheck-users-saved-tracks
Users
- Get Current User's Profile
GET/meget-current-users-profile - Get User's Top Items
GET/me/top/{type}get-users-top-artists-and-tracks - Get User's Profile
GET/users/{user_id}get-users-profile - Follow Playlist
PUT/playlists/{playlist_id}/followersfollow-playlist - Unfollow Playlist
DELETE/playlists/{playlist_id}/followersunfollow-playlist - Get Followed Artists
GET/me/followingget-followed - Follow Artists or Users
PUT/me/followingfollow-artists-users - Unfollow Artists or Users
DELETE/me/followingunfollow-artists-users - Check If User Follows Artists or Users
GET/me/following/containscheck-current-user-follows - Check if Current User Follows Playlist
GET/playlists/{playlist_id}/followers/containscheck-if-user-follows-playlist