Skip to content
master
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 

README.md



Quick Intro

Spotify-api.js is a quick wrapper to interact with spotify api...

Installation

npm i spotify-api.js

Getting Started

Please make an App from here https://developer.spotify.com/dashboard/

Getting Access Token

const { Client } = require("spotify-api.js");
const spotify = new Client("NO TOKEN"); // Keep it like that to get a new token...

const token = await spotify.oauth.get({
  client_id: "client id",
  client_secret: "client secret",
}); // Will return a promise of token 

console.log(token); // Spotify resets its token each every 1-5 minutes to prevent api spam!

Tracks

const track = await spotify.tracks.search("oh my god by alec benjamin", {
  limit: 1,
}); // Searches for the track and limit will be 20 by default
const advanced = await spotify.tracks.search("oh my god by alec benjamin", {
  limit: 1,
  advanced: true,
}); // Same but this will return a `codeImage` and `dominantColor` key with it!
const get = await spotify.tracks.get("track id"); // Get tracks by id...
const audioAnalysis = await spotify.tracks.audioAnalysis("track id"); // Get audio analysis of the track
const audioFeatures = await spotify.tracks.audioFeatures("track id"); // Get audio features of the track

Artists

const artist = await spotify.artists.search("alec benjamin", { limit: 1 }); // Searches for the artist with a default limit as 1...
const advanced = await spotify.artists.search("alec benjamin", {
  limit: 1,
  advanced: true,
}); // Returns a `dominantColor` and `codeImage` key with the response../
const get = await spotify.artists.get("artist id"); // Get artists by id. Has advanced option too...
const albums = await spotify.artists.getAlbums("artist id"); // Get artist albums by id. Has advanced and limit option too...
const topTracks = await spotify.artists.topTracks("artist id"); // Returns top tracks of the artist. Has advanced and limit option too...
const relatedArtists = await spotify.artists.relatedArtists("artist id"); // Returns related artists. Has advanced and limit option too...

Albums

const album = await spotify.albums.search("these two windows", { limit: 1 }); // Searches for an album. Has advanced option too...
const get = await spotify.albums.get("album id"); // Get album by id...
const tracks = await spotify.albums.getTracks("album id", { limit: 5 }); // Get all tracks of an album. Has advanced option too...

Users

const user = await spotify.users.get("id"); // Returns the user details by id...

Playlists

const playlist = await spotify.playlists.get("id"); // Get playlist data by id
const tracks = await spotify.playlists.getTracks("id", { limit: 1 }); // Get all tracks in an album by id. Has advanced option too...

Example for advanced option...

Take the following code for example

const { Client } = require("spotify-api.js"); // Import package
const spotify = new Client("token"); // Load client with token or using oauth

const track = await spotify.tracks.search("oh my god by alec benjamin", {
  limit: 1,
  advanced: true,
}); // Search albums
console.log(track[0].codeImage); // Get the code image for advanced...
console.log(track[0].dominantColor); // Get the dominant color... Returns { hex: string, rgb: [r, g, b, a] }

Deep dive into Refrehing a token

To refresh a token you need 4 params.

  • client id
  • client secret
  • redirect uri (should be defined in your spotify application)
  • Authorization token (granted from the /authorize endpoint)

Example

const {refresh_token} = await spotify.oauth.refresh({
  client_id: "my client id",
  client_secret : "my client secret",
  redirect_uri : "my redirection uri ",
}, "my token"
)

Example for the cover and code image

You can’t perform that action at this time.