<titledata-react-helmet="true">5.x.x to 6.x.x | SpotifyAPI-NET</title><metadata-react-helmet="true"property="og:url"content="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/5_to_6"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_version"content="current"><metadata-react-helmet="true"name="docusaurus_tag"content="docs-default-current"><metadata-react-helmet="true"property="og:title"content="5.x.x to 6.x.x | SpotifyAPI-NET"><metadata-react-helmet="true"name="description"content="SpotifyAPI.Web"><metadata-react-helmet="true"property="og:description"content="SpotifyAPI.Web"><linkdata-react-helmet="true"rel="shortcut icon"href="/SpotifyAPI-NET/img/favicon.ico"><linkdata-react-helmet="true"rel="canonical"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/5_to_6"><linkdata-react-helmet="true"rel="alternate"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/5_to_6"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/5_to_6"hreflang="x-default"><linkrel="stylesheet"href="/SpotifyAPI-NET/assets/css/styles.834af7f3.css">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var config = SpotifyClientConfig</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .CreateDefault()</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .WithAuthenticator(new ClientCredentialsAuthenticator(CLIENT_ID, CLIENT_SECRET)); // takes care of access tokens</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var spotify = new SpotifyClient(config);</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>For some performance guides, have a look at the <ahref="/SpotifyAPI-NET/docs/configuration">Configuration Guide</a></p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="proxy"></a>Proxy<aclass="hash-link"href="#proxy"title="Direct link to heading">#</a></h3><p>In <code>5.x</code>, the proxy configuration could be passed to the <code>SpotifyWebAPI</code> constructor. In <code>6.x</code>, they're part of the HTTP Client. The built-in http client supports proxies out of the box:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var httpClient = new NetHttpClient(new ProxyConfig("localhost", 8080)</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> User = "",</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Password = "",</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> SkipSSLCheck = false,</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">});</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var config = SpotifyClientConfig</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .CreateDefault()</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .WithHTTPClient(httpClient);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var spotify = new SpotifyClient(config);</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="calling-api-endpoints"></a>Calling API Endpoints<aclass="hash-link"href="#calling-api-endpoints"title="Direct link to heading">#</a></h3><p>In <code>5.x</code>, there was one big instance to support all API endpoints. Parameters to these endpoints were passed directly as method parameters. Optional parameters were nullable and could be excluded. In <code>6.x</code>, every endpoint group (<code>albums</code>, <code>tracks</code>, <code>userprofile</code>) has their own API-Client, which is available as a property in a <code>SpotifyClient</code> instance. While URI path parameters are still passed as method parameter, query and body parameters are now passed as a grouped class instance, where required parameters are needed in the constructor and optional parameters can be supplied as properties. All endpoints are also only implemented as async methods.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// OLD:</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">PrivateProfile profile = await spotify.GetPrivateProfileAsync();</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var playlists = await spotify.GetUserPlaylists(profile.Id, 100, 0);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// NEW:</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">PrivateUser user = await spotify.UserProfile.Current();</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var playlists = await spotify.Playlists.GetUsers(user.Id, new PlaylistGetUsersRequest</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Limit = 100,</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Offset = 0</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">});</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>All required arguments are checked for non-null values. If it's null, the methods will throw a <code>ArgumentNullException</code></p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="errorheader-handling"></a>Error/Header Handling<aclass="hash-link"href="#errorheader-handling"title="Direct link to heading">#</a></h3><p>In <code>5.x</code>, all response models included a base error model, with properties like <code>Headers</code>, <code>Error</code> and <code>HasError</code>. This was not a good decision since response models should be clean and only contain API response data. In <code>6.x</code>, error handling is <code>Exception</code> based. For example, if the access token is invalid, calling API endpoints will throw a <code>APIUnauthorizedException</code>. If you hit the API too many times, the method will throw a <code>APITooManyRequestsException</code>. They all derive from a base exception <code>APIException</code>, which is also thrown in more general cases, e.g bad request input parameters. If you're interested in the headers of the last response, you can use <code>spotify.LastResponse</code>, <strong>make sure there is only one thread using this instance!</strong></p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// OLD:</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">PrivateProfile profile = await spotify.GetPrivateProfileAsync();</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">if(profile.HasError())</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // handle error</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var headers = profile.Headers(); // access to headers</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// NEW:</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">try</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> PrivateProfile profile = await spotify.GetPrivateProfileAsync();</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> var response = spotify.LastResponse; // response.Headers</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">catch (APIUnauthorizedException e)</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // handle unauthorized error</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // e.Response contains HTTP response</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // e.Message contains Spotify error message</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">catch (APIException e)</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // handle common error</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // e.Response contains HTTP response</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // e.Message contains Spotify error message</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>More Info: <ahref="/SpotifyAPI-NET/docs/error_handling">Error Handling</a></p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="spotifyapiwebauth"></a>SpotifyAPI.Web.Auth<aclass="hash-link"href="#spotifyapiwebauth"title="Direct link to heading">#</a></h2><p>In <code>5.x</code>, <code>SpotifyAPI.Web.Auth</code> contained every logic related to the OAuth flows. In <code>6.x</code>, <code>SpotifyAPI.Web.Auth</code> is only required if you need a HTTP Server for handling OAuth responses. For example, if you're in a ASP.NET environment or just use the <ahref="/SpotifyAPI-NET/docs/client_credentials">Client Credentials</a> flow, there is no need to install <code>SpotifyAPI.Web.Auth</code> anymore.</p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="authorization-code-auth"></a>Authorization Code Auth<aclass="hash-link"href="#authorization-code-auth"title="Direct link to heading">#</a></h3><p>As an example, this shows how to convert a <code>5.x</code> authorization code flow to <code>6.x</code>:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// OLD</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var auth =</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">auth.AuthReceived +