Announcements
The Spotify Stars Program: Celebrating Values Week!

Help Wizard

Step 1

NEXT STEP

"Current User's Recently Played Tracks" before param not working as expected

"Current User's Recently Played Tracks" before param not working as expected

Hi Spotify Developers!

I am the maintainer of http://github.com/wittydeveloper/spotify-graphql/ client and I am currently implementing a tool using the Spotify API "Current User's Recently Played Tracks" endpoint: https://api.spotify.com/v1/me/player/recently-played

I am willing to navigate the player history using the bidirectional cursors (as written in your doc), here, using the "before" pagination param.
However, anytime I provide a before cursor that goes beyond the last 50 listened to tracks, I get empty responses (no tracks), even when using the "before" cursor of the initial request.


Step to reproduce are the following:

1. perform the following query: "GET https://api.spotify.com/v1/me/player/recently-played?limit=50"
2. perform a second query, using the "next" URL from the previous response: "GET https://api.spotify.com/v1/me/player/recently-played?before=1610564232420&limit=50"

The second query always returns the following response:

{
  "items": [],
  "next": null,
  "cursors": null,
  "limit": 50,
  "href": "https://api.spotify.com/v1/me/player/recently-played?before=1610564232420&limit=50"
}


Am I doing something wrong? My OAuth token has the required `user-read-recently-played` scope.
Is this behavior expected?

Thank you very much! 🙏

 

Reply
3 Replies

This endpoint has only ever returned 50 items total. You can use the cursors to paginate within that limit but not outside of it. Possibly they have set up the cursors to support a larger list of favourites eventually, but this isn't yet available.

@wittydeveloper I recently found this limitation as well. I think this is a substantial deficiency in Spotify. Also, there is no value that shows when a track was last played (i.e., timestamp). I think the latter would solve the former. Is there a way to submit a feature request?

Hello,

I was also facing the same error but after some studies, I found out that I haven't played any songs
yesterday for which day I was requesting the data. I hope you have done the same mistake.
So instead of the last 1 day, I set it to last 5 days, and now I'
m able to get the results.
I'm sharing my timestamp code here, so you might get a rough idea. 


# Before ( last 1 day timestamp) : 
today = datetime.now()
yesterday = today - timedelta(days=1) # 1 for yesterday's result.
yesterday_unix_timestamp = int(yesterday.timestamp()) * 1000

# Download all songs you've listened to "after yesterday", which means in the last 24 hours
r = requests.get(
"https://api.spotify.com/v1/me/player/recently-played?after={time}".format(time=yesterday_unix_timestamp),
headers = header1)
 
#After : 
today = datetime.now()
last_5_days = today - timedelta(days=5)
last_5_days_unix_timestamp = int(last_5_days.timestamp()) * 1000

# Download all songs you've listened to last 5 days, which means in the last 5 days
r = requests.get(
"https://api.spotify.com/v1/me/player/recently-played?after={time}".format(time=last_5_days_unix_timestamp),
headers = header1)

I hope it will help you get the result. 

@charlypoly 



Thank you.

Suggested posts