Skip to content

danjesus/spotify-api.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation



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 and its details

console.log(token.access_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].images[0].url) // Get the image url
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] }

Example for the cover and code image

About

A complete node js wrapper of spotify api with oauth support

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%