Skip to content

3. APIs

Akshay Chacko edited this page Mar 1, 2021 · 34 revisions

Get New Recommendations

Returns a list of song recommendations and relevant track information based on the Spotify song recommendation API. First we call the Get Recommendations APIto get a list of songs, then we call the Get Audio Features for a Track endpoint to append the appropriate track metadata to each song in our list. Our endpoint combines the data from both these API calls into one consolidated JSON structure to be used by our algorithm.

This JSON list is then run through our own recommendation algorithm on the front end in order to find relevant songs based on additional metrics. The list of songs is turned into a feature vector and then we use k-nearest neighbors to choose the most suitable songs.

Endpoint: GET /recommendations/

Request Parameters

Key Location Type Description
seed_tracks User Input String Spotify ID of user seeded track

Response Codes

Code Description
200 OK Success
400 Bad Request Invalid parameters

Returns

Key Location Type Description
song_recommendations JSON List of Spotify Tracks Takes all the recommended tracks and combines them with their corresponding metadata

Example

curl -b cookies.txt -c cookies.txt -X GET https://OUR_SERVER/recommendations/'

{
  "tracks": [
    {
    "href" : "https://api.spotify.com/v1/tracks/1TKYPzH66GwsqyJFKFkBHQ",
    "id" : "1TKYPzH66GwsqyJFKFkBHQ",
    "is_playable" : true,
    "name" : "Music Is Life",
    "preview_url" : "https://p.scdn.co/mp3-preview/546099103387186dfe16743a33edd77e52cec738",
    "track_number" : 1,
    "type" : "track",
    "uri" : "spotify:track:1TKYPzH66GwsqyJFKFkBHQ",
    "danceability": 0.735,
    "energy": 0.578,
    "key": 5,
    "loudness": -11.84,
    "tempo": 98.002,
    },
    {
      "href" : "https://api.spotify.com/v1/tracks/15iosIuxC3C53BgsM5Uggs",
      "id" : "15iosIuxC3C53BgsM5Uggs",
      "is_playable" : true,
      "name" : "All Night",
      "preview_url" : "https://p.scdn.co/mp3-preview/9ee589fa7fe4e96bad3483c20b3405fb59776424",
      "track_number" : 2,
      "type" : "track",
      "uri" : "spotify:track:15iosIuxC3C53BgsM5Uggs"
      "danceability": 0.735,
      "energy": 0.578,
      "key": 5,
      "loudness": -11.84,
      "tempo": 98.002,
    },
    ...
  ],
    "url": "/recommendations/"
}

Third-Party SDKs

Our front end is developed in Kotlin with Android Studio IDE and we will be using Spotify's Web API.

Spotify Web API

We use Spotify's Web API on the front end for numerous purposes that provide the core functionality of the app. The Spotify API is called to get information about the user-seeded song, and then it is used to get information (ie. BPM/key) to determine which songs to recommend. Lastly, the API is used again to save the recommendations to a playlist in the users account.

Authorization

Spotify's Web API requires authorization before we make any requests. We will be following Client Credentials Flow.

Song Info and Audio Features

When a song is being recommended, we show the name of the song, the name of the song's artist, the title of the album containing the song, the image (album cover) for that album, and a 30-second preview of the song with Spotify's Get a Track endpoint. In addition, we will also display the song's audio features such as danceability, energy, and loudness with Spotify's Get Audio Features for a Track endpoint.

Seeding the Recommender

A user provides the link to whichever song they want to use to seed the recommender. We then get information on the provided song's audio features such as danceability, energy, and loudness with Spotify's Get Audio Features for a Track endpoint. We then use this data to determine how the song recommendations will be calculated.

Clone this wiki locally