Sptfy (Sptfy v0.1.3) View Source

Sptfy is a Spotify API client library.

Request resources

Sptfy provides several API modules. For instance using Sptfy.Album, you can fetch an album.

iex> {:ok, album = %Sptfy.Object.FullAlbum{}} = Sptfy.Album.get_album(access_token, id: "0ETFjACtuP2ADo6LFhL6HN")
iex> album.name
...> "Abbey Road (Remastered)"

Or get an error.

iex> Sptfy.Album.get_album("abc", id: "...")
...> {:error, %Sptfy.Object.Error{message: "Invalid access token", status: 401}}

More examples found on integration_test dir.

Obtain access token

Sptfy.OAuth module supports authorization code flow.

iex>  Sptfy.OAuth.url("CLIENT_ID", "https://example.com/callback", %{scope: ["user-top-read"]})
...> "https://accounts.spotify.com/authorize?client_id=..."

iex> Sptfy.OAuth.get_token("CLIENT_ID", "CLIENT_SECRET", "CODE", "https://example.com/callback")
...> %Sptfy.Object.OAuthResponse{access_token: "ACCESS_TOKEN", refresh_token: "REFRESH_TOKEN"}

You can request new access token using Sptfy.OAuth.refresh_token/3 when it expired.

iex> Sptfy.OAuth.refresh_token("CLIENT_ID", "CLIENT_SECRET", "REFRESH_TOKEN")
...> %Sptfy.Object.OAuthResponse{access_token: "NEW ACCESS_TOKEN"}