Registry / http-networking / spotify-web-api-node

spotify-web-api-node

JSON →
library5.0.2jsnpmunverified

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.

npm install spotify-web-api-node
INSTALL
IMPORT
SIG · SPOTIFY-WEB-API-NO
S
spotify-web-api-node
http-networkingjavascriptv5.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SpotifyWebApi
import SpotifyWebApi from 'spotify-web-api-node'
import { SpotifyWebApi } from 'spotify-web-api-node'; const SpotifyWebApi = require('spotify-web-api-node'); const SpotifyWebApi = require('spotify-web-api-node').SpotifyWebApi;
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;`

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(...);
Debug
Known issues
breakingVersion 4.0.0 introduced a breaking change by modifying playlist-related functions to drop the `userId` parameter. The authenticated user for these operations is now inferred from the access token set on the API instance, removing the need to explicitly pass the user's ID.
fix
Remove the `userId` parameter from all calls to playlist manipulation functions (e.g., `getPlaylists`, `createPlaylist`, `addTracksToPlaylist`). Ensure the API instance has an access token for the correct user set.
affects: >=4.0.0
breakingVersion 5.0.0 included significant breaking changes to incorporate new features and streamline the API. The README explicitly advises checking the `CHANGELOG.md` for a detailed list of these modifications.
fix
Before upgrading to or using version 5.0.0+, consult the `CHANGELOG.md` file in the `spotify-web-api-node` GitHub repository to identify and implement all necessary code adjustments for your application.
affects: >=5.0.0
gotchaAuthentication with the Spotify Web API relies on OAuth 2.0, which involves managing access tokens, refresh tokens, and different authorization flows (e.g., Authorization Code, Client Credentials). Access tokens have a limited lifespan and must be refreshed regularly to maintain active sessions.
fix
Implement robust token management logic. For web applications, use the Authorization Code Flow and handle refreshing tokens with `spotifyApi.refreshAccessToken()`. For server-to-server communication, periodically call `spotifyApi.clientCredentialsGrant()` to get new access tokens.
affects: >=2.0.0
securityVersion 4.0.0 included an update to the underlying `superagent` dependency to fix a security warning. Running older versions (prior to v4.0.0) might expose applications to vulnerabilities present in outdated HTTP client libraries.
fix
Upgrade `spotify-web-api-node` to version 4.0.0 or newer to ensure your application benefits from security fixes in its internal dependencies.
affects: <4.0.0
Errors
Common errors & fixes
Error: invalid_grant
This error typically occurs during the Authorization Code Flow if the provided authorization code is invalid, expired, or has already been exchanged for tokens. It can also happen if a refresh token is invalid or expired.
fix
Ensure the authorization code is used immediately after redirection and only once. For refresh tokens, verify it's the current valid token and that the client ID/secret are correct.
Error: No access token specified.
An API request was attempted without a valid access token being set on the `SpotifyWebApi` instance, or the token expired before the request was made.
fix
Call `spotifyApi.setAccessToken(yourValidAccessToken)` with a non-expired token before making any authenticated API calls. Implement token refresh logic to automatically update the token.
TypeError: spotifyApi.getMe is not a function
This usually indicates that the `SpotifyWebApi` class was not correctly imported or instantiated, or that `spotifyApi` is not the expected instance. It can also occur if a method name is misspelled.
fix
Verify that `import SpotifyWebApi from 'spotify-web-api-node';` is used (default import) and that `spotifyApi = new SpotifyWebApi(...)` is correctly called to create an instance. Double-check method names against the documentation.
Error: 400 Bad Request
A 'Bad Request' often points to an issue with the parameters or request body sent to the Spotify API, such as incorrect data format, missing required fields, or exceeding character limits. For playlist modifications, it could also be due to insufficient user scopes. In versions < 2.4.0, a long URL for `addTracksToPlaylist` could trigger this.
fix
Carefully review the parameters being passed to the API method, ensuring they match the Spotify API documentation (e.g., URIs as arrays). Check that the access token has the necessary scopes (e.g., `playlist-modify-public`). If adding many tracks, ensure you are on `v2.4.0` or higher which passes track data in the body.
Upgrade
Version history
5.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
spotify-web-api-node — npm install spotify-web-api-node · libregistry