Artist’s Top Tracks
Get the top tracks of an artist using the artist's Spotify ID.
We'll cover the following
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 |
| 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 |
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.
const endpointUrl = new URL('https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg/top-tracks');//0TnOYISbd1XYRBk9myaseg is the ID of Pitbull a.k.a. Mr. Worldwideconst 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 replace0TnOYISbd1XYRBk9myaseg
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 |
| Array of objects | This array contains information about the albums which contain these tracks. |
| Array of objects | This array contains information about the artist. |
| String | This is the name of the track. |
| Array of objects | This contains a link which can be used to open the track on Spotify. |
| Integer | This shows the popularity of the track. It ranges from |
| String | This is a link to a 30-second preview of the tracks which we can embed in our app. |
| Integer | This is the duration of the track in milliseconds. |
| String | This is the Spotify ID of the track. |
| Integer | This tells us the total number of tracks that we get in response. |