Skip to content

shabanovtg/spotify-middleware-webapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spotify-middleware-webapi

Spotify WebApi for Node.js

  • refresh accessToken with help refreshToken
  • get user profile
  • get user playlists
  • following users/artists
    • get
    • add (follow)
    • delete (unfollow)
    • contains
  • following playlist
    • add (follow)
    • delete (unfollow)
    • contains

Usage

var SpotifyModule = require('spotify-middleware-webapi');
var spotifyModule = new SpotifyModule({
  "credentials": {
    "clientId": "[clientId]",
    "clientSecret": "[clientSecret]",
    "redirect_uri": "http://localhost:3000/auth/callback"
  }
});

Refresh Token

You can use passport-spotify for auth and receive accessToken and refreshToken.

var refreshToken = [your refresh token];

spotifyModule.tokenRefresh(refreshToken, function(err, accessToken) {
  //...
});

Params

accessToken is required. refreshToken - optional. Only for update accessToken.

variant 1 - object

spotifyModule.user({
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, profile, accessToken) {
  // updated accessToken
});

or

spotifyModule.user({
  accessToken: accessToken
}, function(err, profile, accessToken) {
  // updated accessToken
});

variant 2 - accessToken string (without refreshToken)

spotifyModule.user(accessToken, function(err, profile, accessToken) {
  // updated accessToken
});

Search

var q = 'search_string';
var opts = {
  limit: 20,
  offset: 0,
  market: 'US',
  type: ['album', 'artist', 'track', 'playlist']
};

spotifyModule.search(q, opts, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Get User profile

spotifyModule.user({
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, profile, accessToken) {
  //...
});

Get User's Playlists

For current user

spotifyModule.playlists({
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

For other user

spotifyModule.playlists(userId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Get Playlist

By ownerId and playlistId

spotifyModule.playlist(ownerId, userId, opts, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following

Following user get

spotifyModule.following.user({
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following user add

spotifyModule.following.user.add(followingId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following user delete

spotifyModule.following.user.delete(followingId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following user contains

spotifyModule.following.user.contains(followingId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following playlist add

spotifyModule.following.playlist.add(playlistOwnerId, playlistId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following playlist delete

spotifyModule.following.playlist.delete(playlistOwnerId, playlistId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});

Following playlist contains

spotifyModule.following.playlist.contains(playlistOwnerId, playlistId, userId, {
  accessToken: accessToken,
  refreshToken: refreshToken
}, function(err, results, accessToken) {
  //...
});