/spotify-mini

Tiny Spotify client for node.js exposing some useful methods to get your latest Spotify status

Primary LanguageTypeScript

spotify-mini

GitHub Workflow Status (branch) npm

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.


Installation

yarn add spotify-mini

Usage

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>',
 }
*/

API

SpotifyClient

The exported class that needs to be instanciated to interact with the exposed APIs.

getCurrentlyPlaying

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

getLastPlayed

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 )

Development

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.

License

MIT Rocktim Saikia © 2022