A simple node wrapper around the Spotify web api that exposes some useful methods like easily getting your currently playing track, last played track or both.
I have been using a rough version of this lib in my portfolio's spotify widget from the very begining, so finally decided to turn it into a proper npm module.
yarn add spotify-mini
import { SpotifyClient } from 'spotify-mini';
const spotify = new SpotifyClient({
clientId: '<YOUR-SPOTIFY-CLIENT-ID>',
clientSecret: '<YOUR-SPOTIFY-CLIENT_SECRET>',
refreshToken: '<YOUR-SPOTIFY-REFRESH-TOKEN>'
});
// Get the currently playing track,(if there is no track playing, it will return null)
const currentlyPlayingTrack = await spotify.getCurrentlyPlaying();
/**
{
isPlaying: true,
title: '<track title>',
artist: '<artist name>',
album: '<album name>',
}
*/
// Get the last played track
const lastPlayedTrack = await spotify.getLastPlayed();
/**
[
{
title: '<track title>',
artist: '<artist name>',
album: '<album name>',
}
]
*/
// To get a specific number of the recently played songs, just pass it to the method (1 < n < 50), default is 1
const recentTracks = await spotify.getLastPlayed(2)
/**
[
{
title: '<track title>',
artist: '<artist name>',
album: '<album name>',
},
{
title: '<track title>',
artist: '<artist name>',
album: '<album name>',
}
]
*/
// // If there is no track playing, this will return the last played song, to prevent this feature pass `fallbackToLastPlayed: false`
const currentTrack = await spotify.getCurrentlyPlaying({
fallbackToLastPlayed: true
});
/**
{
isPlaying: false,
title: '<track title>',
artist: '<artist name>',
album: '<album name>',
}
*/
The exported class that needs to be instanciated to interact with the exposed APIs.
Returns your currently playing track, if none returns null.
Options:
fallbackToLastPlayed
: (default: false) If true, it will return the last played track if there is no currently playing track
Returns your last played track. But can be used to get a list of your recently played tracks; accepts an optional integer as argument to get your desired number of recently played tracks. (default: 1) (limit is 1<n<50 )
Fork this repo and run
yarn install
or
npm install
Once the required packages are installed, create a .env
file with the properties of .env.example
.
To run the tests, you will need to generate a refresh_token
with the minium of the following spotify api scopes: user-read-currently-playing
, user-read-recently-played
.
MIT Rocktim Saikia © 2022