Skip to content
DocumentationAPI Reference
API Reference

Unofficial Spotify API Library

Unofficial Spotify API Library

The Unofficial Spotify SDK provides convenient access to the Spotted REST API from applications written in C#.

It is generated with Stainless.

The REST API documentation can be found on spotted.cjav.dev.

Installation

Install the package from NuGet:

dotnet add package Spotted

Requirements

This library requires .NET Standard 2.0 or later.

Usage

See the examples directory for complete and runnable examples.

using System;
using Spotted;
using Spotted.Models.Albums;

SpottedClient client = new();

AlbumRetrieveParams parameters = new() { ID = "4aawyAB9vmqN3uQ7FjRGTy" };

var album = await client.Albums.Retrieve(parameters);

Console.WriteLine(album);

Client configuration

Configure the client using environment variables:

using Spotted;

// Configured using the SPOTIFY_ACCESS_TOKEN and SPOTTED_BASE_URL environment variables
SpottedClient client = new();

Or manually:

using Spotted;

SpottedClient client = new() { AccessToken = "My Access Token" };

Or using a combination of the two approaches.

See this table for the available options:

PropertyEnvironment variableRequiredDefault value
AccessTokenSPOTIFY_ACCESS_TOKENtrue-
BaseUrlSPOTTED_BASE_URLtrue"https://api.spotify.com/v1"

Modifying configuration

To temporarily use a modified client configuration, while reusing the same connection and thread pools, call WithOptions on any client or service:

using System;

var album = await client
    .WithOptions(options =>
        options with
        {
            BaseUrl = "https://example.com",
            Timeout = TimeSpan.FromSeconds(42),
        }
    )
    .Albums.Retrieve(parameters);

Console.WriteLine(album);

Using a with expression makes it easy to construct the modified options.

The WithOptions method does not affect the original client or service.

Requests and responses

To send a request to the Spotted API, build an instance of some Params class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a C# class.

For example, client.Albums.Retrieve should be called with an instance of AlbumRetrieveParams, and it will return an instance of Task<AlbumRetrieveResponse>.

Binary responses

The SDK defines methods that return binary responses, which are used for API responses that shouldn’t necessarily be parsed, like non-JSON data.

These methods return HttpResponse:

using System;
using System.Text;
using Spotted.Models.Playlists.Images;

ImageUpdateParams parameters = new()
{
    PlaylistID = "3cEYpjA9oz9GiPac4AsH4n",
    Body = Encoding.UTF8.GetBytes("text"),
};

var image = await client.Playlists.Images.Update(parameters);

Console.WriteLine(image);

To save the response content to a file, or any Stream, use the CopyToAsync method:

using System.IO;

using var response = await client.Playlists.Images.Update(parameters);
using var contentStream = await response.ReadAsStream();
using var fileStream = File.Open(path, FileMode.OpenOrCreate);
await contentStream.CopyToAsync(fileStream); // Or any other Stream

Raw responses

The SDK defines methods that deserialize responses into instances of C# classes. However, these methods don’t provide access to the response headers, status code, or the raw response body.

To access this data, prefix any HTTP method call on a client or service with WithRawResponse:

var response = await client.WithRawResponse.Albums.Retrieve(parameters);
var statusCode = response.StatusCode;
var headers = response.Headers;

The raw HttpResponseMessage can also be accessed through the RawMessage property.

For non-streaming responses, you can deserialize the response into an instance of a C# class if needed:

using System;
using Spotted.Models.Albums;

var response = await client.WithRawResponse.Albums.Retrieve(parameters);
AlbumRetrieveResponse deserialized = await response.Deserialize();
Console.WriteLine(deserialized);

Error handling

The SDK throws custom unchecked exception types:

  • SpottedApiException: Base class for API errors. See this table for which exception subclass is thrown for each HTTP status code:
StatusException
400SpottedBadRequestException
401SpottedUnauthorizedException
403SpottedForbiddenException
404SpottedNotFoundException
422SpottedUnprocessableEntityException
429SpottedRateLimitException
5xxSpotted5xxException
othersSpottedUnexpectedStatusCodeException

Additionally, all 4xx errors inherit from Spotted4xxException.

false

  • SpottedIOException: I/O networking errors.

  • SpottedInvalidDataException: Failure to interpret successfully parsed data. For example, when accessing a property that’s supposed to be required, but the API unexpectedly omitted it from the response.

  • SpottedException: Base class for all exceptions.

Pagination

The SDK defines methods that return a paginated lists of results. It provides convenient ways to access the results either one page at a time or item-by-item across all pages.

Auto-pagination

To iterate through all results across all pages, use the Paginate method, which automatically fetches more pages as needed. The method returns an IAsyncEnumerable:

using System;

var page = await client.Shows.ListEpisodes(parameters);
await foreach (var item in page.Paginate())
{
    Console.WriteLine(item);
}

Manual pagination

To access individual page items and manually request the next page, use the Items property, and HasNext and Next methods:

using System;

var page = await client.Shows.ListEpisodes(parameters);
while (true)
{
    foreach (var item in page.Items)
    {
        Console.WriteLine(item);
    }
    if (!page.HasNext())
    {
        break;
    }
    page = await page.Next();
}

Network options

Retries

The SDK automatically retries 2 times by default, with a short exponential backoff between requests.

Only the following error types are retried:

  • Connection errors (for example, due to a network connectivity problem)
  • 408 Request Timeout
  • 409 Conflict
  • 429 Rate Limit
  • 5xx Internal

The API may also explicitly instruct the SDK to retry or not retry a request.

To set a custom number of retries, configure the client using the MaxRetries method:

using Spotted;

SpottedClient client = new() { MaxRetries = 3 };

Or configure a single method call using WithOptions:

using System;

var album = await client
    .WithOptions(options =>
        options with { MaxRetries = 3 }
    )
    .Albums.Retrieve(parameters);

Console.WriteLine(album);

Timeouts

Requests time out after 1 minute by default.

To set a custom timeout, configure the client using the Timeout option:

using System;
using Spotted;

SpottedClient client = new() { Timeout = TimeSpan.FromSeconds(42) };

Or configure a single method call using WithOptions:

using System;

var album = await client
    .WithOptions(options =>
        options with { Timeout = TimeSpan.FromSeconds(42) }
    )
    .Albums.Retrieve(parameters);

Console.WriteLine(album);

Undocumented API functionality

The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.

Response validation

In rare cases, the API may return a response that doesn’t match the expected type. For example, the SDK may expect a property to contain a string, but the API could return something else.

By default, the SDK will not throw an exception in this case. It will throw SpottedInvalidDataException only if you directly access the property.

If you would prefer to check that the response is completely well-typed upfront, then either call Validate:

var album = client.Albums.Retrieve(parameters);
album.Validate();

Or configure the client using the ResponseValidation option:

using Spotted;

SpottedClient client = new() { ResponseValidation = true };

Or configure a single method call using WithOptions:

using System;

var album = await client
    .WithOptions(options =>
        options with { ResponseValidation = true }
    )
    .Albums.Retrieve(parameters);

Console.WriteLine(album);

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  2. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.