/spotify-py-sdk

Python SDK for Spotify Web API

Primary LanguagePythonMIT LicenseMIT

Python SDK for Spotify Web API

Downloads

This library is still a work in progress.

This is a Python library for the Spotify Web API. This SDK is very simple to use if you are already familiar with the typescript SDK provided by Spotify. It takes a lot of help from there.

Endpoints currently available

  • albums
  • artists
  • audiobooks
  • browse
  • chapters
  • episodes
  • markets
  • playlists
  • recommendations
  • search
  • shows
  • tracks
  • users

Requirements

  • python 3.9 or higher

Using this in your project

You can install this package using pip:

pip install spotify-py-sdk

or using poetry:

poetry add spotify-py-sdk

Running the example provided

First install the dependencies (make sure you have poetry installed):

poetry install

Create a .env file in the root directory with your client_id and client_secret. Refer the .env.example file for reference.

CLIENT_ID=
CLIENT_SECRET=

Now run the file to get results back from the web api:

python example.py

Create a client instance

Currently, we only have client credentials flow for authentication. If you're building a server side application, you should use Client Credentials Flow, and is the correct choice when you have both your Client ID and Client Secret available.

from spotify-py-sdk import SpotifyApi, SdkConfig
from dotenv import load_dotenv
load_dotenv()

config = SdkConfig() # optional; can create custom methods
api: SpotifyApi = SpotifyApi(os.getenv("CLIENT_ID"), os.getenv("CLIENT_SECRET"), config)

Once you have an authenticated instance of the SDK, you can make requests to the Spotify Web API by using the methods exposed on the client instance of the API.

api.search.execute("purpose", ["album"])
api.browse.get_categories()

Running the tests

To run the tests, you need to have a Spotify account.

You will need to create a new app in the Spotify Developer portal, and add a redirect URI of http://localhost:3000.

You will need to add the following environment variables:

  • CLIENT_ID
  • CLIENT_SECRET

You can run the tests using pytest. We support python-dotenv, so you can add these to a .env file in the root of the repository.

To run all tests:

pytest

To run a specific test.

pytest tests/endpoints/test_albums.py