User Saved Tracks and Track Information

Learn how to get information about the tracks saved by the user on Spotify using the Spotify API.

In this lesson, we'll look at how we can get a list of tracks and their details that are saved in a user profile.

We can get information about a user’s taste in music using the Get User’s Saved Tracks endpoint. The base URL https://api.spotify.com/v1/me/tracks provides a list of tracks saved in the current user profile. We have already used PUT and DELETE requests with this endpoint to save and remove tracks from a user profile, respectively. Now, we’ll use the GET request to fetch the list of tracks saved in a user profile.

Request parameters

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

Query parameter

Type

Category

Description

limit

Integer

Optional

This limits the number of items to be returned in the response. Its value ranges from 1 to 50 and the default value is 20.

market

String

Optional

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.

offset

Integer

Optional

This parameter can be used to offset the first item we get in response.

The code below demonstrates how to get the saved tracks from a user profile with the authorization access token.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/me/tracks');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchUsersTracks() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchUsersTracks();

Let's look at the explanation of our code:

  • Line 1: We define the URL as https://api.spotify.com/v1/me/tracks.

  • Lines 3–6: We add header parameters.

  • Line 9: We set the request type to GET.

  • Lines 13–20: We define a function, fetchUsersTracks(), that calls the endpoint and prints the response.

Save the ID of the first track, which is automatically extracted from the response to use it further in this lesson.

Response fields

The table below contains the important fields that we get in response to an API call to this endpoint:

Response field

Type

Description

album

Array

This array contains the information about the album of the track saved in the user's profile.

name

String

This is the name of the track.

spotify

String

This is the link to the Spotify page of the item.

popularity

String

This shows how popular a track is. It ranges from 0 to 100 with 100 being the most popular.

preview_url

String

This is the URL for a 30 seconds preview of the track.

artists

Array

This array contains the information about the artists of the track saved in the user's profile.

id

String

This is the Spotify ID of the track.

available_markets

Array

This is the list of markets in which the track is available.

added_at

String

This contains the date and time at which the track was saved by the user.

The Get Track endpoint is used to get information about a track. The base URI of this endpoint is https://api.spotify.com/v1/tracks/{id}. The path parameter {id} is replaced with the ID of the track we want to get. So if we want to fetch a track whose Spotify ID is sample_id, the URL will be https://api.spotify.com/v1/tracks/sample_id.

Request parameters

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

Query parameter

Type

Category

Description

market

String

Optional

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 to use this endpoint to get the information of a track. We're getting the information about the track whose ID was saved in the previous executable.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/tracks/{{TRACK_ID}}');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchTrackInfo() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchTrackInfo();

Let's look at how we changed the code:

  • Line 1: We change the URL to https://api.spotify.com/v1/tracks/{{TRACK_ID}}.

  • Line 5: We change the access token to client credentials access token.

  • Line 13: We change the function's name to fetchTrackInfo().

Try changing the ID of the track in line 1 to get information for a different track. Here are some track IDs:

Track

Spotify ID

Love Without Question

2XQdvzdqvyq7XHkoPvizl5

Without Me

7lQ8MOhq6IN2w8EYcFNSUk

Perfect Duet

1bhUWB0zJMIKr9yVPrkEuI

Bones

0HqZX76SFLDz2aW8aiqi7G

Highwayman

7jWbXvrgdbkajU8L28ahn5

Response fields

The response of this endpoint contains the following information:

Response field

Type

Description

album

Array

This contains the information about the album of the track.

name

String

This is the name of the track.

spotify

String

This contains the links for the tracks of the artist.

popularity

Integer

This index defines how popular a track is on Spotify. It ranges from 0 to 100 with 100 being the most popular.

preview_url

String

This is the link to a 30 seconds preview of the track. It may be null depending on the availability of the preview.