Simple Spotify client for nodejs exposing few useful methods

Overview

spotify-mini

GitHub Workflow Status (branch) npm

Simple Spotify client for nodejs exposing useful methods

I have been using a rough version of this lib in my portfolio from a long time. So decided to turn it into a proper module.


Prerequisite

Make sure to create a refresh token with atleast these permissions enabled user-read-currently-playing, user-read-recently-played, user-top-read. You can use spotify-rtoken-cli to easily create a refresh_token right from your terminal.


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 the last played track)
const currentlyPlayingTrack = await spotify.getCurrentTrack()
/**
 {
    isPlaying: true,
    title: '<track title>',
    artist: '<artist name>',
    album: '<album name>',
 }
*/

// Get the most recently played tracks in order, default return limit is 1
const lastPlayedTracks = await spotify.getRecentTracks(2)
/**
 [
   {
      title: '<track title>',
      artist: '<artist name>',
      album: '<album name>',
   },
   {
      title: '<track title>',
      artist: '<artist name>',
      album: '<album name>',
   }
 ]
*/

// Get your most played tracks, see API doc below for available options
const topRecentTracks = await spotify.getTopTracks()
const topOverallTracks = await spotify.getTopTracks({ timeRange: 'long' })

API

getCurrentTrack

Options Type Description
fallbackToLastPlayed boolean Returns the last played track, if there is no ongoing track atm. (default:true)

getRecentTracks

Options Type Description
limit number (1 <= n <= 50) Limit the number of recently played tracks to return. (default: 1)

getTopTracks

Options Type Description
limit number Limit the number of recently played tracks to return. (Default: 10)
timeRange short, medium, long Over what time range the top tracks should be calculated. (Default: short)

Related

License

MIT License © Rocktim Saikia 2022

Comments
  • Add `limit` option to `getLastPlayed` method to get the n number of last played tracks

    Add `limit` option to `getLastPlayed` method to get the n number of last played tracks

    Currently, the default limit is 1 which is fine. But there should be a limit option to get the n number of last played tracks https://developer.spotify.com/console/get-recently-played/

    enhancement help wanted good first issue 
    opened by rocktimsaikia 3
  • add accesToken to client + add limit to getLastPlayed

    add accesToken to client + add limit to getLastPlayed

    add caching of the accesToken to the client add limit to the getLastPlayed function to allow the user to pick the amount of tracks to receive. max being 50 and minimum being 1. add test to test the limiting feature on getlastPlayed fixes #10 fixes #7

    opened by melosh101 2
  • replaced isomorphic-unfetch with node-fetch

    replaced isomorphic-unfetch with node-fetch

    I have replaced isomorphic-unfetch with node-fetch without receiving any errors during tests.

    fixes: https://github.com/rocktimsaikia/spotify-mini/issues/11

    opened by melosh101 2
  • Don't generate new Access token when used with `fallbackToLastPlayed`

    Don't generate new Access token when used with `fallbackToLastPlayed`

    https://github.com/rocktimsaikia/spotify-mini/blob/50bc8ec3a309aabe56adb079d7740a14c7a62702/index.ts#L72 Use the same access token in getLastPlayed() generated by currently playing API call when there is no ongoing track. It does not make sense to regenerate a new token instead of using the existing one

    bug help wanted 
    opened by rocktimsaikia 2
  • Default to return the last played song if there is no currently playing track

    Default to return the last played song if there is no currently playing track

    This should be a pretty straightforward PR. fallbackToLastPlayed will default to true. PR should take care of any edge cases and the README document should be updated to reflect the new changes.

    enhancement 
    opened by rocktimsaikia 0
  • Use `node-fetch`

    Use `node-fetch`

    Replace isomorphic-unfetch with node-fetch. isomorphic-unfetch just switches between unfetch and node-fetch for Browser and Node support.

    Switches between unfetch & node-fetch for client & server.

    Since this lib is only aimed to be used in Node environment without exposing any secret key, we can get rid of isomorphic-unfetch and use node-fetch directly insteaad.

    enhancement good first issue 
    opened by rocktimsaikia 0
  • Write proper tests

    Write proper tests

    Set up a mock server and re-write all the possible tests without needing to set up GitHub secrets.

    Resource: https://mswjs.io/docs/getting-started/integrate/node

    enhancement help wanted 
    opened by rocktimsaikia 0
  • Add a fallback option in getCurrentlyPlaying method to get the last track

    Add a fallback option in getCurrentlyPlaying method to get the last track

    Currently, the getCurrentlyPlaying() method returns null if there is no ongoing track.

    https://github.com/rocktimsaikia/spotify-mini/blob/6dbdac758a35d8dcc4d159ee956fe26c1c042477/index.ts#L63

    Instead, there should be an option to pass on to this method named fallbackToLastPlayed which will take a boolean value and if it's true it will return the last played song instead of null when there is no ongoing track.

    help wanted good first issue 
    opened by rocktimsaikia 0
Releases(v2.0.0)
  • v2.0.0(Apr 10, 2022)

    Breaking:

    • rename primary API methods 078f20e

    Minor

    • add getTopTracks 078f20e

    https://github.com/rocktimsaikia/spotify-mini/compare/v1.1.0...v2.0.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Apr 2, 2022)

    • update deps f43e1fb
    • fallbackToLast defaults to true (#15) 17323e5
    • Update readme.md 4c56443

    https://github.com/rocktimsaikia/spotify-mini/compare/v1.0.0...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Mar 11, 2022)

    • add exposed API descriptions 1b31441
    • cache accesToken + add limit to getLastPlayed (#13) 974d098

    https://github.com/rocktimsaikia/spotify-mini/compare/v0.1.3...v1.0.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.3(Mar 9, 2022)

    • update readme c5eaf5f
    • replaced isomorphic-unfetch with node-fetch (#12) 1332454

    https://github.com/rocktimsaikia/spotify-mini/compare/v0.1.2...v0.1.3

    Source code(tar.gz)
    Source code(zip)
  • v0.1.2(Mar 2, 2022)

  • v0.1.1(Mar 2, 2022)

    • chore: use native node querystring api d8aa540
    • chore: set up husky (#9) 707e31c
    • fix email typo in package.json (#8) b45b6f0
    • chore: update package.json d5f1c5b

    https://github.com/rocktimsaikia/spotify-mini/compare/v0.1.0...v0.1.1

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 1, 2022)

    • feat: add fallbackToLastPlayed option 4337a8d
    • refactor: remove unsued deps 13a6405

    https://github.com/rocktimsaikia/spotify-mini/compare/v0.0.3...v0.1.0

    Source code(tar.gz)
    Source code(zip)
  • v0.0.3(Feb 28, 2022)

    • Update readme.md by @valanm22 in https://github.com/rocktimsaikia/spotify-mini/pull/5

    https://github.com/rocktimsaikia/spotify-mini/compare/v0.0.2...v0.0.3

    Source code(tar.gz)
    Source code(zip)
Owner
Rocktim
Coding my way through life
Rocktim
Spotify clone using Next.js, Spotify API, Tailwind, NextAuth, Recoil

Next.js + Tailwind CSS Example This example shows how to use Tailwind CSS (v2.2) with Next.js. It follows the steps outlined in the official Tailwind

Abdelwahab Hussein 2 Dec 25, 2021
spotify.ts is an wrapper built around Spotify's Web API

spotify.ts About spotify.ts is an wrapper built around Spotify's Web API. Features Fast Object Oriented Typescript, ESM, CJS support Easy to Use Insta

null 6 Nov 17, 2022
Web Application that represents a music player using the spotify API, React, JS, CSS, HTML, nodeJS, Firebase, material-ui, JSON and other technologies. Made by Yohan Hmaiti

Web Application that represents a music player using the spotify API, React, JS, CSS, HTML, nodeJS, Firebase, material-ui, JSON and other technologies. Made by Yohan Hmaiti

Yohan Hmaiti 2 Jan 8, 2022
A useful nodejs program to send notification on discord when new result

A useful nodejs program to send notification on discord when new result

Alwyn974 5 Mar 23, 2022
Get your video lectures summed up in a few minutes

Nutshell allows students to quickly summarise their entire video lectures in just a few minutes. Whether it's for a quick revision, catching up on missed classes, or simply a reference for what happened, Nutshell helps students by providing them with the most important parts of the class without them having to watch a second of the recorded video.

Lakshya 5 Sep 17, 2022
A simple package to download music tracks from spotify 🎵

A simple package to download music tracks from spotify ??

Alen Yohannan 26 Dec 25, 2022
I’m a very useful music bot that can make you play a music with a simple command! I have a lot of good commands that you can have a better experience playing your favorites songs!

I’m a very useful music bot that can make you play a music with a simple command! I have a lot of good commands that you can have a better experience playing your favorites songs!

Hugo Kishi 2 Aug 16, 2022
Add some fire to your Spotify music playlists 🔥🎧

Spotifire ?? Add some fire to your Spotify music playlists ?? Built With Next.js React Tailwind CSS (v3.0) NextAuth Recoil Spotify Web API Preview Vis

Niloy Sikdar 15 Jun 19, 2022
SpotifyUiClone - A Ui Clone of Spotify, only studing purporses

Spotify Ui Clone An Kinda Clone of the Spotify Ui for studing purposes Summary This is a project made in React Js, so the idea is to make an ui for an

Jf_Dess 1 Jan 7, 2022
A review aggregator web application that allows users to review albums by leveraging the Spotify Web API.

Reviewify Summary Reviewify is a review aggregator platform that leverages the Spotify Web API. It allows users to: Login with their Spotify accounts

Abhi Ardeshana 3 Oct 7, 2022
SpotLight is a web app which uses spotify developer APIs and lists your most listened songs, albums, artists and genres.

SpotLight is a web app which uses spotify developer APIs and lists your most listened songs, albums, artists and genres.

Sahil Saha 9 Dec 31, 2022
now-playing: A website to show what I'm currently listening to on Spotify Run on Repl.it

now-playing: A website to show what I'm currently listening to on Spotify ❓ How does it work? This project is split into two parts - the frontend and

Connor Dennison 3 Apr 29, 2022
Edit CSS directly from Spotify.

Spotify CSS Editor Built with Spicetify Creator. Download from Spicetify Marketplace. Edit CSS directly from Spotify. Features Edit CSS directly from

Flafy 10 Dec 27, 2022
Spotify Web API with Next.js

Resumo Statsfy é um site que exibe seus artistas e músicas mais escutadas, além de músicas recentes. Você pode usar clicando neste link. (Apenas usuár

otaviozin 8 Sep 5, 2022
Randomfy takes your most listened Spotify artists

Randomfy takes your most listened Spotify artists, selects one of them and shows you similar artists, so you can meet new artists based on the previous selected one.

Raul Andrade 9 Nov 6, 2022
Spotify Radio - JS Expert Week 6.0

Spotify Radio - JS Expert Week 6.0 Welcome to the sixth Javascript Expert Week. This is the starting code to start our journey. Tag this project with

Alison Lima 6 Mar 16, 2022
Pomodoro + spotify + todo list = awesome!

Turborepo starter This is an official Yarn v1 starter turborepo. What's inside? This turborepo uses Yarn as a package manager. It includes the followi

Cesar Muñoz 3 Jan 3, 2023
Spotify radio is an audio streaming app where you can add effects in real time.

Spotify Radio - Semana JS Expert 6.0 Spotify radio is an audio streaming app where you can add effects in real time. JS-Expert Project of the Week by

Luis Fernando 6 Apr 25, 2022
Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify

Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify, apple music support . You can save your songs and play it. It also has DJ mode system.

Diwas Atreya 65 Dec 25, 2022