Manage Albums in a User’s Profile

Learn how to save or delete an album from a user profile using Spotify API.

We can manage the albums saved in a user profile using the Spotify API. In this lesson, we’ll discuss two types of HTTP requests. One is used to save an album to the current user profile whereas the other is used to remove an album from a user profile.

We can use the base URL https://api.spotify.com/v1/me/albums to save or delete an album to the current user's profile. A PUT request to this endpoint saves the album whereas a DELETE request deletes the specified album.

Note: The user is the one with whom the authorization code access token is associated. Currently, we're using access tokens associated with our own profile, so we'll be making changes to our own resources.

Request parameters

This endpoint has the following query parameter:

Query parameter

Category

Type

Description

ids

Optional

String

This contains the IDs of the albums we want to save or delete from the current user's profile. These IDs are separated by commas(,). We can save a maximum of 20 albums 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 albums we want to save or delete from the current user's profile. These IDs are separated by commas(,). We can save or delete a maximum of 50 albums with one call using this parameter. If we use the ids query parameter, the IDs listed here will be overridden.

Note: We can use the Search for Item endpoint to get the Spotify ID of an album.

Save an album to a user profile

Let's look at how this endpoint is used to save albums with the authorization code access token. We're providing the IDs of Pitbull's album Can't Stop Us Now, and Bruno Mars' albums Doo-Wops & Hooligans as the values for the query parameter ids.

Press + to interact
url = ('https://api.spotify.com/v1/me/albums?'
'ids=4aawyAB9vmqN3uQ7FjRGTy,1uyf3l2d4XYwiEqAb7t7fX')
#4aawyAB9vmqN3uQ7FjRGTy is the ID of Pitbull's album Can't Stop Us Now
#1uyf3l2d4XYwiEqAb7t7fX is the ID of Bruno Mars's album Doo-Wops And Hooligans
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

The content of the response is empty for a successful request, and the value of status_code is 200. We get an error message for an unsuccessful request, which tells us why the request was unsuccessful.

Remove an album from the user profile

Let's delete one of the two albums we saved above from our profile by sending a DELETE request to https://api.spotify.com/v1/me/albums. Unlike the case where we saved albums, we'll use the body parameters to delete the album.

Press + to interact
url = ('https://api.spotify.com/v1/me/albums')
data={"ids":"4aawyAB9vmqN3uQ7FjRGTy"}
#4aawyAB9vmqN3uQ7FjRGTy is the ID of Pitbull's album Can't Stop Us Now
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))

Try removing multiple albums from the user's library by adding more IDs (separated by commas) in line 3.

Response fields

The content of the response is empty for a successful request, and the value of status_code is 200. For an unsuccessful request, we get a message in response that tells us about the error.