This library provides a universal wrapper and client for the Spotify Web API, designed to run seamlessly in both Node.js environments and modern browsers via bundlers like browserify, webpack, or rollup. Currently at stable version 5.0.2, the project maintains an active development status, with updates addressing bugs and adding new features, although major version releases can have extended intervals. Its key differentiators include comprehensive coverage of Spotify's Web API endpoints, offering helper functions for fetching music metadata, managing user profiles and playlists, interacting with the user's music library, personalizing content, browsing categories, controlling playback, and managing user/artist following relationships. The library aims to simplify interaction with the Spotify API's OAuth 2.0 authentication flows and various data retrieval/manipulation operations.
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
The primary class is a default export. For CommonJS environments, if using transpiled ESM, you might need to access the `.default` property: `const SpotifyWebApi = require('spotify-web-api-node').default;`
import SpotifyWebApi from 'spotify-web-api-node'
This example demonstrates how to initialize the SpotifyWebApi, obtain an access token using the Client Credentials Flow, and then make a simple API call to search for an artist. It also briefly mentions the Authorization Code Flow setup.
import SpotifyWebApi from 'spotify-web-api-node';
const clientId = process.env.SPOTIFY_CLIENT_ID ?? '';
const clientSecret = process.env.SPOTIFY_CLIENT_SECRET ?? '';
const redirectUri = process.env.SPOTIFY_REDIRECT_URI ?? 'http://localhost:8888/callback'; // Required for Authorization Code Flow
if (!clientId || !clientSecret) {
console.error("Please set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET environment variables.");
process.exit(1);
}
const spotifyApi = new SpotifyWebApi({
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri
});
// Example: Client Credentials Flow (server-side, no user context)
spotifyApi.clientCredentialsGrant()
.then(data => {
console.log('The access token expires in ' + data.body['expires_in'] + ' seconds.');
console.log('The access token is ' + data.body['access_token']);
spotifyApi.setAccessToken(data.body['access_token']);
// Example: Search for an artist (requires no user context)
return spotifyApi.searchArtists('The Weeknd');
})
.then(data => {
console.log('Found artists:', data.body.artists.items[0].name);
return spotifyApi.getArtistAlbums(data.body.artists.items[0].id);
})
.then(data => {
console.log('First album:', data.body.items[0].name);
})
.catch(err => {
console.error('Something went wrong during client credentials flow or API call!', err);
});
// For Authorization Code Flow, you'd typically:
// 1. Generate an authorization URL: spotifyApi.createAuthorizeURL(['user-read-private', 'user-read-email'], 'some-state');
// 2. Redirect user to that URL.
// 3. Handle the callback on your redirectUri, exchanging the code for tokens:
// spotifyApi.authorizationCodeGrant(code).then(...).catch(...);
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.
Agent activity
0 hits · last 30 days
No traffic data recorded yet.