Skip to content
A Go wrapper for the Spotify Web API
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Fix examples Jun 14, 2017
test_data Add test for current user's top tracks Oct 13, 2017
.travis.yml Update .travis.yml Oct 13, 2017
LICENSE Add Apache license Jan 26, 2015
README.md Fix examples Jun 14, 2017
album.go add missing fields to simple album Aug 8, 2018
album_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
artist.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
artist_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
audio_analysis.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
audio_analysis_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
audio_features.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
audio_features_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
auth.go Add AuthURLWithDialog to generate auth url w/show_dialog set to true (#… Jun 27, 2019
category.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
category_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
countries.go Remove license note from each file Jul 30, 2016
cursor.go Remove license note from each file Jul 30, 2016
full_tests.bat Add ability to page forward and backward through search results. Simi… Feb 26, 2015
library.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
library_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
page.go Added automatic-retries (#39) May 23, 2017
player.go Add support for new option 'position_ms' in player.PlayOpt() (#74) Sep 12, 2018
player_test.go Use int64 for millisecond epochs Feb 12, 2018
playlist.go Allow Playlist description to be added/modified on playlist creation/… Sep 25, 2018
playlist_test.go Allow Playlist description to be added/modified on playlist creation/… Sep 25, 2018
recommendation.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
recommendation_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
search.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
search_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
spotify.go Client.get: on 204, return early (#88) Feb 10, 2019
spotify_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
track.go Add IsLocal boolean field on PlaylistTrack object (#93) May 20, 2019
track_attributes.go Add support for recommendations based on seeds Jul 29, 2016
track_test.go replace our funky RoundTrippers with httptest servers Jul 28, 2017
user.go Added offset to toptracks (#66) Jun 29, 2018
user_test.go Add test for current user's top tracks Oct 13, 2017

README.md

Spotify

GoDoc Build status Build Status

This is a Go wrapper for working with Spotify's Web API.

It aims to support every task listed in the Web API Endpoint Reference, located here.

By using this library you agree to Spotify's Developer Terms of Use.

Installation

To install the library, simply

go get github.com/zmb3/spotify

Authentication

Spotify uses OAuth2 for authentication and authorization.
As of May 29, 2017 all Web API endpoints require an access token.

You can authenticate using a client credentials flow, but this does not provide any authorization to access a user's private data. For most use cases, you'll want to use the authorization code flow. This package includes an Authenticator type to handle the details for you.

Start by registering your application at the following page:

https://developer.spotify.com/my-applications/.

You'll get a client ID and secret key for your application. An easy way to provide this data to your application is to set the SPOTIFY_ID and SPOTIFY_SECRET environment variables. If you choose not to use environment variables, you can provide this data manually.

// the redirect URL must be an exact match of a URL you've registered for your application
// scopes determine which permissions the user is prompted to authorize
auth := spotify.NewAuthenticator(redirectURL, spotify.ScopeUserReadPrivate)

// if you didn't store your ID and secret key in the specified environment variables,
// you can set them manually here
auth.SetAuthInfo(clientID, secretKey)

// get the user to this URL - how you do that is up to you
// you should specify a unique state string to identify the session
url := auth.AuthURL(state)

// the user will eventually be redirected back to your redirect URL
// typically you'll have a handler set up like the following:
func redirectHandler(w http.ResponseWriter, r *http.Request) {
      // use the same state string here that you used to generate the URL
      token, err := auth.Token(state, r)
      if err != nil {
            http.Error(w, "Couldn't get token", http.StatusNotFound)
            return
      }
      // create a client using the specified token
      client := auth.NewClient(token)

      // the client can now be used to make authenticated requests
}

You may find the following resources useful:

  1. Spotify's Web API Authorization Guide: https://developer.spotify.com/web-api/authorization-guide/

  2. Go's OAuth2 package: https://godoc.org/golang.org/x/oauth2/google

Helpful Hints

Optional Parameters

Many of the functions in this package come in two forms - a simple version that omits optional parameters and uses reasonable defaults, and a more sophisticated version that accepts additional parameters. The latter is suffixed with Opt to indicate that it accepts some optional parameters.

Automatic Retries

The API will throttle your requests if you are sending them too rapidly. The client can be configured to wait and re-attempt the request. To enable this, set the AutoRetry field on the Client struct to true.

For more information, see Spotify rate-limits.

API Examples

Examples of the API can be found in the examples directory.

You may find tools such as Spotify's Web API Console or Rapid API valuable for experimenting with the API.

You can’t perform that action at this time.