DEV Community

Cover image for Getting random tracks using the Spotify API
Perry Janssen
Perry Janssen

Posted on

Getting random tracks using the Spotify API

While the Spotify API offers quite a lot of ways to interact with one of the most used streaming services, it lacks the ability of accessing all Spotify tracks as a whole. This might be because of the enormous amount of available songs. Grabbing a list of all wouldn’t be very efficient. While I was creating an application with which a user can discover new music, I wanted to show the user seemingly random tracks using Spotify. In this article, I’ll go over a couple of ways I tried to achieve this.

This is not a tutorial on the basics of the Spotify API, but on kind of a obscure, specific issue. Check out the ‘Get Started’ guide: https://developer.spotify.com/documentation/web-api/quick-start/

Developing using (Node)JS? This module really made connecting with Spotify and especially oAuth, a lot easier: https://www.npmjs.com/package/spotify-web-api-js

Genres

The first method I tried made use of Spotify’s genre system. By fetching a list of all genres, picking a random genre and getting a track by that genre would result in a random track, or so I thought. First, Spotify doesn’t assign a genre to each track. A genre is linked to an artist, instead. Secondly, the list of available genres does not match the genres linked to the artists.

According to the list of ‘available genre seeds’ only contain about one- or two hundred genres. These genres are all very general. The genres linked to artist accounts are usually more specific. This makes it impossible to search all artists, and therefore all tracks, by using genres.

This problem has already been noticed by other developers. You can check out the issue on GitHub: https://github.com/spotify/web-api/issues/868

Search

The second method I tried made use of a simple search query. Using Spotify’s Search API, you can search through all available tracks, artists, playlist and more. This method worked best for me. According to Spotify’s documentation, a wildcard character can be used to search. A wildcard is a character that can be anything. For instance, a search query like this: My % will return all tracks that begin with the word My.

Now I had to think of a way of creating random searches, and getting a random track from that search. The best way I could think of was having a random character prefixing the wildcard character, like this: d%. This would return all tracks that start with a d. You could also put the wildcard at the beginning, or at both the beginning or end.

Because Spotify shows the more famous artists and tracks at the top of the search results, I also had to randomize an offset. According to documentation, a maximum limit of 10.000 can be applied. The offset will be the index of the first returned item, or the first returned page if a limit is set.

Check out the Spotify Search documentation: https://developer.spotify.com/documentation/web-api/reference/search/search/

Getting a random search query

To get a random search query, you’ll need to grab a random character from a list of characters and add the wildcard character to the string. I created a simple function that would do just that:

function getRandomSearch() {
  // A list of all characters that can be chosen.
  const characters = 'abcdefghijklmnopqrstuvwxyz';

  // Gets a random character from the characters string.
  const randomCharacter = characters.charAt(Math.floor(Math.random() * characters.length));
  let randomSearch = '';

  // Places the wildcard character at the beginning, or both beginning and end, randomly.
  switch (Math.round(Math.random())) {
    case 0:
      randomSearch = randomCharacter + '%';
      break;
    case 1:
      randomSearch = '%' + randomCharacter + '%';
      break;
  }

  return randomSearch;
}
Enter fullscreen mode Exit fullscreen mode

To create a random offset, just generate a random number between 0–10.000:

const randomOffset = Math.floor(Math.random() * 10000);

The Spotify Search endpoint allows you to filter on different types. To get tracks, just add type=track to the request.

Now that we have all the required parameters, we can make a GET request to the Spotify Search endpoint, like this:

GET https://api.spotify.com/v1/search
type=track
q=getRandomSearch()
offset=randomOffset
Enter fullscreen mode Exit fullscreen mode

Locality issue

To use the Spotify API, Spotify requires the user to be logged in. A logged in user is linked to a certain location. This means that searching, even through the API, can be a little biased towards tracks originating from the user’s country. There isn’t really a way around this, but it is possible to define a market. A market is, kind of, the country the user is from. You can set the market the same way as any other parameter, but setting a market means you’ll only get results from artists from that specific country. Of course, you could randomize this as well.

Top comments (1)

Collapse
 
dmahely profile image
Doaa Mahely

This is a helpful article, thank you for documenting your process.