npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

spotify-context-history

v0.3.1

Published

npm package to work on top of the context history from a Spotify user

Downloads

8

Readme

Spotify Context History

npm package to work on top of the context history from a Spotify user. You can retrieve the context (most recently played tracks) and resume playing a context.

Use case

A user is listening to an album named Foo and stops listening to in the middle of it. It then starts listening to another album named Bar and it listens to just a couple of tracks.

In a normal scenario, if he/she is using the app from Spotify, then going back to play Foo does not resume playing to the last track.

This package empowers you with the ability to build this feature. Both albums Foo and Bar are separate contexts and each context points to the last track played. Hence, you can resume playing a context (an album in the given example).

Why name it "context"?

In the previous example, it is only talked about albums. But this package also supports playlists and artists. A track can be from one of these three types depending on the context where it was played to.

Requirements

  • Valid Access Token (otherwise you'll get just a bunch of failed promises)
  • Spotify scopes: user-read-currently-playing, user-read-recently-played and user-modify-playback-state

Installation

npm install spotify-context-history

Example

const SpotifyContextHistory = require('spotify-context-history');

const spotifyContextHistory = new SpotifyContextHistory('valid access token');

// You can use this method to change the access token
spotifyContextHistory.setAccessToken('valid access token');

// Returns a promise that resolves to the user play contexts
// Argument:
//  - Optional
//  - Unix timestamp in milliseconds
//  - If passed, it will get all tracks from the Spotify API since the timestamp passed
//  - If not passed, it just gets the 50 most recent tracks or
//      the tracks played since the last timestamp.
// Return:
//  - Very similar to https://developer.spotify.com/documentation/web-api/reference/player/get-recently-played/
//  - Array of JSON objects with three fields:
//     - `played_at` -> The date and time the track was played. (e.g., "2016-12-13T20:42:17.016Z")
//     - `context`   -> One of these Spotify Objects: Artist, Album or Playlist
//     - `track`     -> Track Object from Spotify (simplified)
// - Very succinctly, the return is similar to Get Current User's Recently Played Tracks,
//     but the context has more information from:
//      - https://developer.spotify.com/documentation/web-api/reference/artists/get-several-artists/
//      - https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlist/
//      - https://developer.spotify.com/documentation/web-api/reference/albums/get-several-albums/
spotifyContextHistory.update(123456789)
    .then(console.log, console.error);

// Returns the same as `update`
spotifyContextHistory.getContextHistory()
    .then(console.log, console.error);

// First updates the context and then returns the same as `getContextHistory`
spotifyContextHistory.getUpdatedContextHistory()
    .then(console.log, console.error);

// Returns a promise which indicates the success/failure of the play operation.
// This method resumes playing from a context.
// If the track is not present in the album/playlist/artist or the uri/id it is not found
// in the stored contexts, then it starts from the beginning.
spotifyContextHistory.play("album/playlist's id/uri")
    .then(console.log, console.error);

Limitations