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

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.

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.

Press + to interact
url = ('https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg/top-tracks?'
'market=ES')
#0TnOYISbd1XYRBk9myaseg is the ID of Pitbull a.k.a. Mr. Worldwide
headers = {
'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

album

Array of objects

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

artists

Array of objects

This array contains informattion 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.