Artist’s Top Tracks
Get the top tracks of an artist using the artist's Spotify ID.
Overview
In this lesson, we'll discuss the Get Artist's Top Tracks endpoint, which we can use 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 frequently played now.
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 |
Retrieve artist's top tracks using client credentials access token
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.
url = ('https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg/top-tracks?''market=ES')#0TnOYISbd1XYRBk9myaseg is the ID of Pitbull a.k.a. Mr. Worldwideheaders = {'Content-Type': 'application/json','Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'}response = requests.request("GET", url, headers=headers)print(json.dumps(response.json(), indent=4))
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 informattion 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. |