Artist’s Top Tracks

Get the top tracks of an artist using the artist's Spotify ID.

In this lesson, we'll discuss the Get Artist's Top Tracks endpoint, which is used to get the top tracks of an artist. The top tracks are the ones that have the most number of recent plays. If a track was played frequently in the past, it would be lower in the ranking than the tracks that are being frequently played now.

Retrieve artist's top tracks

The base URL https://api.spotify.com/v1/artists/{id}/top-tracks is used to get the top tracks of an artist. The path parameter {id} is replaced with the artist’s ID whose top tracks we want to fetch. So if we want the top tracks of an artist whose Spotify ID is sample_id, the URL will be https://api.spotify.com/v1/artists/sample_id/tracks.

Request parameters

The information obtained by calling this endpoint can be filtered using the following query parameter:

Query parameter

Category

Type

Description

market

Optional

String

This is an ISO 3166-1 alpha-2 country code that we can use to fetch only the content available in a specific country. Some examples of country codes are us for the United States, es for Spain, and fr for France. If we send an auth code access token in the request header, the user's country will take priority over the country specified in this parameter.

Let's look at how we can use this endpoint to get the top tracks of an artist via the client credentials token for authorization. The code below demonstrates how to use this endpoint with the market query parameter.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg/top-tracks');
//0TnOYISbd1XYRBk9myaseg is the ID of Pitbull a.k.a. Mr. Worldwide
const queryParameters = new URLSearchParams({
market: 'ES'
});
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters
};
async function fetchTopTracks() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
}
catch (error) {
printError(error);
}
}
fetchTopTracks();

Let's look at a brief explanation of the code above:

  • Line 1: We set the URL to https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg/top-tracks. We can replace 0TnOYISbd1XYRBk9myaseg with the ID of another artist to get a different list.

  • Lines 4–6: We add the query parameters.

  • Lines 17–27: We define a function named fetchTopTracks(). It calls the defined endpoint and prints the response.

Try changing the Spotify ID of the artist in line 1 to get the top tracks of another artist.

Note: If we use the authorization code access token, the response will contain only the artist's top tracks available in the country associated with our Spotify ID.

Response fields

The response of this endpoint will contain the following information:

Response field

Type

Description

album

Array of objects

This array contains information about the albums which contain these tracks.

artists

Array of objects

This array contains information about the artist.

name

String

This is the name of the track.

spotify

Array of objects

This contains a link which can be used to open the track on Spotify.

popularity

Integer

This shows the popularity of the track. It ranges from 0 to 100 with 100 being the most popular.

preview_url

String

This is a link to a 30-second preview of the tracks which we can embed in our app.

duration_ms

Integer

This is the duration of the track in milliseconds.

id

String

This is the Spotify ID of the track.

total

Integer

This tells us the total number of tracks that we get in response.