Spotify.NET/SpotifyAPI.Docs/versioned_docs/version-5.1.1/web/getting_started.md

184 lines
5.8 KiB
Markdown
Raw Permalink Normal View History

2019-08-16 23:40:04 +01:00
---
2020-05-13 17:25:42 +01:00
id: getting_started
title: Getting Started
sidebar_label: Getting Started
2019-08-16 23:40:04 +01:00
---
This API provides full access to the new SpotifyWebAPI introduced [here](https://developer.spotify.com/web-api/).
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:
```csharp
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:
2019-08-16 23:40:04 +01:00
```csharp
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`:
```csharp
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](auth/getting_started.md)
2019-08-16 23:40:04 +01:00
## Error-Handling
2019-08-16 23:40:04 +01:00
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:
2019-08-16 23:40:04 +01:00
```csharp
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
2019-08-16 23:40:04 +01:00
Every API-Call has an `asynchronous` and `synchronous` method.
```csharp
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
- [GetAlbumTracks](/web/albums.md#getalbumtracks)
- [GetAlbum](/web/albums.md#getalbum)
- [GetSeveralAlbums](/web/albums.md#getseveralalbums)
2019-08-16 23:40:04 +01:00
### Artists
- [GetArtist](/web/artists.md#getartist)
- [GetRelatedArtists](/web/artists.md#getrelatedartists)
- [GetArtistsTopTracks](/web/artists.md#getartiststoptracks)
- [GetArtistsAlbums](/web/artists.md#getartistsalbums)
- [GetSeveralArtists](/web/artists.md#getseveralartists)
2019-08-16 23:40:04 +01:00
### Browse
- [GetFeaturedPlaylists](/web/browse.md#getfeaturedplaylists)
- [GetNewAlbumReleases](/web/browse.md#getnewalbumreleases)
- [GetCategories](/web/browse.md#getcategories)
- [GetCategory](/web/browse.md#getcategory)
- [GetCategoryPlaylists](/web/browse.md#getcategoryplaylists)
2019-08-16 23:40:04 +01:00
### Follow
- [Follow](/web/follow.md#follow)
- [Unfollow](/web/follow.md#unfollow)
- [IsFollowing](/web/follow.md#isfollowing)
- [FollowPlaylist](/web/follow.md#followplaylist)
- [UnfollowPlaylist](/web/follow.md#unfollowplaylist)
- [IsFollowingPlaylist](/web/follow.md#isfollowingplaylist)
2019-08-16 23:40:04 +01:00
### Library
- [SaveTracks](/web/library.md#savetracks)
- [SaveTrack](/web/library.md#savetrack)
- [GetSavedTracks](/web/library.md#getsavedtracks)
- [RemoveSavedTracks](/web/library.md#removesavedtracks)
- [CheckSavedTracks](/web/library.md#checksavedtracks)
- [SaveAlbums](/web/library.md#savealbums)
- [SaveAlbum](/web/library.md#savealbum)
- [GetSavedAlbums](/web/library.md#getsavedalbums)
- [RemoveSavedAlbums](/web/library.md#removesavedalbums)
- [CheckSavedAlbums](/web/library.md#checksavedalbums)
2019-08-16 23:40:04 +01:00
### Personalization
- [GetUsersTopTracks](/web/personalization.md#getuserstoptracks)
- [GetUsersTopArtists](/web/personalization.md#getuserstopartists)
- [GetUsersRecentlyPlayedTracks](/web/personalization.md#getusersrecentlyplayedtracks)
2019-08-16 23:40:04 +01:00
### Player
- [GetDevices](/web/player.md#getdevices)
- [GetPlayback](/web/player.md#getplayback)
- [GetPlayingTrack](/web/player.md#getplayingtrack)
- [TransferPlayback](/web/player.md#transferplayback)
- [ResumePlayback](/web/player.md#resumeplayback)
- [PausePlayback](/web/player.md#pauseplayback)
- [SkipPlaybackToNext](/web/player.md#skipplaybacktonext)
- [SkipPlaybackToPrevious](/web/player.md#skipplaybacktoprevious)
- [SetRepeatMode](/web/player.md#setrepeatmode)
- [SetVolume](/web/player.md#setvolume)
- [SetShuffle](/web/player.md#setshuffle)
2019-08-16 23:40:04 +01:00
### Playlists
- [GetUserPlaylists](/web/playlists.md#getuserplaylists)
- [GetPlaylist](/web/playlists.md#getplaylist)
- [GetPlaylistTracks](/web/playlists.md#getplaylisttracks)
- [CreatePlaylist](/web/playlists.md#createplaylist)
- [UpdatePlaylist](/web/playlists.md#updateplaylist)
- [ReplacePlaylistTracks](/web/playlists.md#replaceplaylisttracks)
- [RemovePlaylistTracks](/web/playlists.md#removeplaylisttracks)
- [RemovePlaylistTrack](/web/playlists.md#removeplaylisttrack)
- [AddPlaylistTracks](/web/playlists.md#addplaylisttracks)
- [AddPlaylistTrack](/web/playlists.md#addplaylisttrack)
- [ReorderPlaylist](/web/playlists.md#reorderplaylist)
2019-08-16 23:40:04 +01:00
### Profiles
- [GetPublicProfile](/web/profiles.md#getpublicprofile)
- [GetPrivateProfile](/web/profiles.md#getprivateprofile)
2019-08-16 23:40:04 +01:00
### Search
- [SearchItems](/web/search.md#searchitems)
- [SearchItemsEscaped](/web/search.md#searchitemsescaped)
2019-08-16 23:40:04 +01:00
### Tracks
- [GetSeveralTracks](/web/tracks.md#getseveraltracks)
- [GetTrack](/web/tracks.md#gettrack)
- [GetAudioAnalysis](/web/tracks.md#getaudioanalysis)
2019-08-16 23:40:04 +01:00
### Util
- [Utility-Functions](/web/utilities.md)