| Crates.io | spotify_web_api |
| lib.rs | spotify_web_api |
| version | 0.2.0 |
| created_at | 2025-05-28 01:22:15.013086+00 |
| updated_at | 2025-12-23 00:41:04.845437+00 |
| description | A wrapper for the Spotify Web API |
| homepage | |
| repository | https://github.com/ry-sev/spotify_web_api |
| max_upload_size | |
| id | 1692200 |
| size | 544,119 |
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.
You can add this to your project by running the command
cargo add spotify_web_api
There are more examples in the examples folder.
use spotify_web_api::{
api::{artists::GetArtist, Query as _},
model::Artist,
Spotify,
};
fn main() -> anyhow::Result<()> {
let spotify = Spotify::with_client_credentials("client_id", "client_secret")?;
spotify.request_token()?;
let artist: Artist = GetArtist::from("0559tR6WyukLWH68JIGBuC").query(&spotify)?;
println!("{artist:#?}");
Ok(())
}
use spotify_web_api::{
api::{users::GetCurrentUserProfile, AsyncQuery as _},
auth::scopes,
model::CurrentUserProfile,
AsyncSpotify,
};
use std::io::{self, Write};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut spotify = AsyncSpotify::with_authorization_code_pkce(
"client_id",
"http://127.0.0.1:8888/callback",
scopes::all(),
)?;
let user_auth_url = spotify.user_authorization_url();
println!("User Authorization URL:\n\n{user_auth_url}");
println!("Please paste the full URL you were redirected to after authorization:\n");
io::stdout().flush()?;
let mut redirect_url = String::new();
io::stdin().read_line(&mut redirect_url)?;
let redirect_url = redirect_url.trim();
spotify.request_token_from_redirect_url(redirect_url).await?;
let user_profile: CurrentUserProfile = GetCurrentUserProfile.query_async(&spotify).await?;
println!("{user_profile:#?}");
Ok(())
}
Choosing one flow over the rest depends on the application you are building:
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 |
Supported endpoints are organized under the api module. To interact with an endpoint, you can use either the Query or AsyncQuery traits.
Query is 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.AsyncQuery is intended for asynchronous code and integrates seamlessly with an asynchronous runtime of your choice, such as Tokio or async-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.
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 the available_markets field in various models, such as Track. 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 the tracks field in Playlist.Format: [x] [Title] [Method] [Endpoint] [Spotify Docs]
GET /albums/{id} get-an-albumGET /albums get-multiple-albumsGET /albums/{id}/tracks get-an-albums-tracksGET me/albums get-users-saved-albumsPUT me/albums save-albums-userDELETE me/albums remove-albums-userGET me/albums/contains check-users-saved-albumsGET /browse/new-releases get-new-releasesGET /artists/{id} get-an-artistGET /artists get-multiple-artistsGET /artists/{id}/albums get-an-artists-albumsGET /artists/{id}/top-tracks get-an-artists-top-tracksGET /audiobooks/{id} get-an-audiobookGET /audiobooks get-multiple-audiobooksGET /audiobooks/{id}/chapters get-audiobook-chaptersGET me/audiobooks get-users-saved-audiobooksPUT me/audiobooks save-audiobooks-userDELETE me/audiobooks remove-audiobooks-userGET me/audiobooks/contains check-users-saved-audiobooksGET /browse/categories get-categoriesGET /browse/categories/{category_id} get-a-categoryGET /chapters/{id} get-a-chapterGET /chapters get-several-chaptersGET /episodes/{id} get-an-episodeGET /episodes get-multiple-episodesGET me/episodes get-users-saved-episodesPUT me/episodes save-episodes-userDELETE me/episodes remove-episodes-userGET me/episodes/contains check-users-saved-episodesGET /recommendations/available-genre-seeds get-recommendation-genresGET /markets get-available-marketsGET /me/player get-information-about-the-users-current-playbackPUT /me/player transfer-a-users-playbackGET /me/player/devices get-a-users-available-devicesGET /me/player/currently-playing get-the-users-currently-playing-trackPUT /me/player/play start-a-users-playbackPUT /me/player/pause pause-a-users-playbackPOST /me/player/next skip-users-playback-to-next-trackPOST /me/player/previous skip-users-playback-to-previous-trackPUT /me/player/seek seek-to-position-in-currently-playing-trackPUT /me/player/repeat set-repeat-mode-on-users-playbackPUT /me/player/volume set-volume-for-users-playbackPUT /me/player/shuffle toggle-shuffle-for-users-playbackGET /me/player/recently-played get-recently-playedGET /me/player/queue get-queuePOST /me/player/queue add-to-queueGET /playlists/{playlist_id} get-playlistPUT /playlists/{playlist_id} change-playlist-detailsGET /playlists/{playlist_id}/tracks get-playlists-tracksPUT /playlists/{playlist_id}/tracks reorder-or-replace-playlists-tracksPOST /playlists/{playlist_id}/tracks add-tracks-to-playlistDELETE /playlists/{playlist_id}/tracks remove-tracks-playlistGET /me/playlists get-a-list-of-current-users-playlistsGET /users/{user_id}/playlists get-list-users-playlistsPOST /users/{user_id}/playlists create-playlistGET /playlists/{playlist_id}/images get-playlist-coverPUT /playlists/{playlist_id}/images upload-custom-playlist-coverGET /search searchGET /shows/{id} get-a-showGET /shows get-multiple-showsGET /shows/{id}/episodes get-a-shows-episodesGET me/shows get-users-saved-showsPUT me/shows save-shows-userDELETE me/shows remove-shows-userGET me/shows/contains check-users-saved-showsGET /tracks/{id} get-trackGET /tracks get-several-tracksGET me/tracks get-users-saved-tracksPUT me/tracks save-tracks-userDELETE me/tracks remove-tracks-userGET me/tracks/contains check-users-saved-tracks Get Current User's Profile GET /me get-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}/followers follow-playlist
Unfollow Playlist DELETE /playlists/{playlist_id}/followers unfollow-playlist
Get Followed Artists GET /me/following get-followed
Follow Artists or Users PUT /me/following follow-artists-users
Unfollow Artists or Users DELETE /me/following unfollow-artists-users
Check If User Follows Artists or Users GET /me/following/contains check-current-user-follows
Check if Current User Follows Playlist GET /playlists/{playlist_id}/followers/contains check-if-user-follows-playlist