Manage Tracks in User’s Profile

Learn to manage tracks in a user profile using Spotify API.

Overview

In this lesson, we'll use two types of HTTP requests. One is used to save a track to a user profile, whereas the other is used to remove a track from a user profile.

The base URL https://api.spotify.com/v1/me/tracks is used to save or delete tracks to the current user's profile. The PUT request saves the track, whereas a DELETE request removes the specified track from the user profile.

Request parameters

This endpoint has the following query parameter.

Query parameter

Category

Type

Description

ids

Required

String

This contains the IDs of the tracks we want to save in the current user's profile. These IDs are separated by a comma(,). We can save a maximum of 20 tracks with one call.

The table below contains the body parameter of this endpoint.

Body parameter

Category

Type

Description

ids

Optional

String

This contains the IDs of the tracks we want to save or delete from the current user's profile. These IDs are separated by a comma(,). We can save or delete a maximum of 50 tracks with one call using this parameter. If we use the ids query parameter, the IDs listed here will be overridden.

Save tracks to a user profile

Let's look at how to use this endpoint to save tracks to a user profile. The code below shows how to use this endpoint with the query parameter ids.

Press + to interact
url = ('https://api.spotify.com/v1/me/tracks?'
'ids=4rHZZAmHpZrA3iH5zx8frV,3C0nOe05EIt1390bVABLyN')
#4rHZZAmHpZrA3iH5zx8frV is the Spotify ID for the song Mirrors
#3C0nOe05EIt1390bVABLyN is the Spotify ID for the song On the Floor
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
response = requests.request("PUT", url, headers=headers,params={})
if response.status_code==200:
print(response.status_code)
else:
print(json.dumps(response.json(), indent=4))

Response fields

In case of a successful request, the response of this endpoint doesn't have any content and contains only status_code (200). If the request is unsuccessful, we'll get a message in response that tells us why the request was unsuccessful.

Remove a track from a user profile

Let's look at how to use this endpoint to remove tracks. The code below shows how to send a DELETE request to delete a track from a user profile. We're using the body parameter id instead of the query parameter to send the ID of the track.

Press + to interact
url = 'https://api.spotify.com/v1/me/tracks'
data = {
"ids":"3C0nOe05EIt1390bVABLyN"
}
#3C0nOe05EIt1390bVABLyN is the Spotify ID for the song On the Floor
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
response = requests.request("DELETE", url, headers=headers,params=data)
if response.status_code==200:
print(response.status_code)
else:
print(json.dumps(response.json(), indent=4))

Response fields

In case of a successful request, the response of this endpoint doesn't have any content and contains only status_code (200). In response to an unsuccessful request, we get a message in the response, that tells us what went wrong.