Skip to main content is an asynchronous API wrapper for Spotify written in Python.

Project description


Version infoGithub IssuesGithub forksGitHub starsLicenseDiscordTravis

An API library for the spotify client and the Spotify Web API written in Python. is an asyncronous API library for Spotify. While maintaining an emphasis on being purely asyncronous the library provides syncronous functionality with the spotify.sync module.

import spotify.sync as spotify  # Nothing requires async/await now!



To install the library simply clone it and run

  • git clone
  • pip3 install -U .

or use pypi

  • pip3 install -U spotify (latest stable)
  • pip3 install -U git+ (nightly)


Sorting a playlist by popularity

import sys
import getpass

import spotify

async def main():
    playlist_uri = input("playlist_uri: ")
    client_id = input("client_id: ")
    secret = getpass.getpass("application secret: ")
    token = getpass.getpass("user token: ")

    async with spotify.Client(client_id, secret) as client:
        user = await spotify.User.from_token(client, token)

        for playlist in await user.get_playlists():
            if playlist.uri == playlist_uri:
                await playlist.sort(reverse=True, key=(lambda track: track.popularity))
            print('No playlists were found!', file=sys.stderr)

if __name__ == '__main__':

Required oauth scopes for methods

import spotify
from spotify.oauth import get_required_scopes

# In order to call this method sucessfully the "user-modify-playback-state" scope is required.
print(get_required_scopes(  # => ["user-modify-playback-state"]

# Some methods have no oauth scope requirements, so `None` will be returned instead.
print(get_required_scopes(spotify.Playlist.get_tracks))  # => None

Usage with flask

import string
import random
from typing import Tuple, Dict

import flask
import spotify.sync as spotify


APP = flask.Flask(__name__)
APP.config.from_mapping({'spotify_client': SPOTIFY_CLIENT})

REDIRECT_URI: str = 'http://localhost:8888/spotify/callback'

OAUTH2_SCOPES: Tuple[str] = ('user-modify-playback-state', 'user-read-currently-playing', 'user-read-playback-state')
OAUTH2: spotify.OAuth2 = spotify.OAuth2(, REDIRECT_URI, scopes=OAUTH2_SCOPES)

SPOTIFY_USERS: Dict[str, spotify.User] = {}

def spotify_callback():
        code = flask.request.args['code']
    except KeyError:
        return flask.redirect('/spotify/failed')
        key = ''.join(random.choice(string.ascii_uppercase) for _ in range(16))
        SPOTIFY_USERS[key] = spotify.User.from_code(

        flask.session['spotify_user_id'] = key

    return flask.redirect('/')

def spotify_failed():
    flask.session.pop('spotify_user_id', None)
    return 'Failed to authenticate with Spotify.'

def index():
        return repr(SPOTIFY_USERS[flask.session['spotify_user_id']])
    except KeyError:
        return flask.redirect(OAUTH2.url)

if __name__ == '__main__':'', port=8888, debug=False)


For resources look at the examples or ask in the discord

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spotify-0.8.1.tar.gz (38.3 kB view hashes)

Uploaded Source

Built Distribution

spotify-0.8.1-py3-none-any.whl (44.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page