5.9 KiB
sidebarDepth |
---|
2 |
Getting Started
This API provides full access to the new SpotifyWebAPI introduced here. With it, you can search for Tracks/Albums/Artists and also get User-based information. It's also possible to create new playlists and add tracks to it.
First steps
Imports
So after you added the API to your project, you may want to add following imports to your files:
using SpotifyAPI.Web; //Base Namespace
using SpotifyAPI.Web.Enums; //Enums
using SpotifyAPI.Web.Models; //Models for the JSON-responses
Basic-Usage
Now you can actually start doing calls to the SpotifyAPI, just create a new Instance of SpotifyWebAPI:
private static SpotifyWebAPI _spotify;
public static void Main(String[] args)
{
_spotify = new SpotifyWebAPI()
{
AccessToken = "XXXXXXXXXXXX",
TokenType = "Bearer"
}
FullTrack track = _spotify.GetTrack("3Hvu1pq89D4R0lyPBoujSv");
Console.WriteLine(track.Name); //Yeay! We just printed a tracks name.
}
You may note that we used AccessToken
and TokenType
. Spotify does not allow un-authorized access to their API. You will need to implement one of the auth flows. Luckily, SpotifyAPI.Web.Auth
exists for this reason. A simple way to receive a AccessToken
is via CredentialAuth
:
CredentialsAuth auth = new CredentialsAuth("YourClientID", "YourClientSecret");
Token token = await auth.GetToken();
_spotify = new SpotifyWebAPI()
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
}
For more info, visit the Getting Started of SpotifyAPI.Web.Auth
Examples
A list of small examples can be found here. Do you think a specific example is missing? Feel free to open a PR.mdIssue!
Error-Handling
Every API-Call returns a reponse-model which consists of base-error model. To check if a specific API-Call was successful, use the following approach:
PrivateProfile profile = _spotify.GetPrivateProfile();
if (profile.HasError())
{
Console.WriteLine("Error Status: " + profile.Error.Status);
Console.WriteLine("Error Msg: " + profile.Error.Message);
}
In case some or all of the returned values are null, consult error status and message, they can lead to an explanation!
Sync vs Asynchronous
Every API-Call has an asynchronous
and synchronous
method.
public async void Test()
{
var asyncProfile = await _spotify.GetPrivateProfileAsync();
var syncProfile = _spotify.GetPrivateProfile();
}
Note that the synchronous
call will block the current Thread!
API-Reference
Albums
Artists
Browse
Follow
Library
- SaveTracks
- SaveTrack
- GetSavedTracks
- RemoveSavedTracks
- CheckSavedTracks
- SaveAlbums
- SaveAlbum
- GetSavedAlbums
- RemoveSavedAlbums
- CheckSavedAlbums
Personalization
Player
- GetDevices
- GetPlayback
- GetPlayingTrack
- TransferPlayback
- ResumePlayback
- PausePlayback
- SkipPlaybackToNext
- SkipPlaybackToPrevious
- SetRepeatMode
- SetVolume
- SetShuffle
Playlists
- GetUserPlaylists
- GetPlaylist
- GetPlaylistTracks
- CreatePlaylist
- UpdatePlaylist
- ReplacePlaylistTracks
- RemovePlaylistTracks
- RemovePlaylistTrack
- AddPlaylistTracks
- AddPlaylistTrack
- ReorderPlaylist