Spotipy2

raw JSON →
0.6.1 verified Sat May 09 auth: no python

The next generation Spotify Web API wrapper for Python. Current version 0.6.1, supports Python 3.7+. Active development with async support, dataclass types, and OAuth flows.

pip install spotipy2
error ImportError: cannot import name 'Spotify' from 'spotipy2.client'
cause Incorrect import path for Spotify client.
fix
Use: from spotipy2 import Spotify
error TypeError: Object of type Track is not JSON serializable
cause Trying to json.dumps a spotipy2 type directly.
fix
Use dataclasses.asdict(track) or access track.__dict__ before serializing.
error spotipy2.exceptions.SpotifyException: 401 - Unauthorized
cause Invalid or missing credentials.
fix
Check that SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET environment variables are set correctly.
gotcha In v0.6 simplified types were removed; all types now have optional fields defaulting to None.
fix Check for None on fields like track.album when accessing nested data.
breaking In v0.3 Type.from_dict became synchronous. Async code expecting await will break.
fix Remove await when calling Type.from_dict; it now returns directly.
gotcha Caching with MongoDB is only available if MongoDB is installed and configured; it's not built-in.
fix Ensure pymongo is installed and a MongoDB instance is running. Pass cache=... to Spotify constructor.

Authenticate using Client Credentials Flow and fetch a track.

import os
from spotipy2 import Spotify
from spotipy2.auth import ClientCredentialsFlow

client_id = os.environ.get('SPOTIFY_CLIENT_ID', '')
client_secret = os.environ.get('SPOTIFY_CLIENT_SECRET', '')

auth = ClientCredentialsFlow(client_id=client_id, client_secret=client_secret)
client = Spotify(auth)

track = client.get_track('4cOdK2wGLETKBW3PvgPWqT')
print(track.name)