Skip to main content

Spotify SDK for Python

PyPI version Python versions Actions status Documentation License

A Python SDK for the Spotify Web API.

Full documentation is available at spotify-sdk.dev.

Features

  • Type-safe: Full type hints with Pydantic models for all API responses
  • Sync and async: Dedicated SpotifyClient and AsyncSpotifyClient classes
  • Automatic retries: Exponential backoff with jitter for rate limits and transient errors
  • Context managers: Clean resource management with with and async with support

Installation

pip install spotify-sdk

Or with uv:

uv add spotify-sdk

Requirements

  • Python 3.10+

Python version support follows the official Python release cycle. We support all versions that have not reached end-of-life.

Authentication

To use the SDK, you'll need credentials from the Spotify Developer Dashboard. The SDK supports:

  • Access token authentication
  • Client credentials flow (auto-refreshes tokens)
  • Authorization code flow with refresh tokens
# Access token
client = SpotifyClient(access_token="your-access-token")

# Client credentials
client = SpotifyClient.from_client_credentials(
    client_id="your-client-id",
    client_secret="your-client-secret",
)
# Authorization code (user-scoped endpoints)
from spotify_sdk.auth import AuthorizationCode, FileTokenCache

auth = AuthorizationCode(
    client_id="your-client-id",
    client_secret="your-client-secret",
    redirect_uri="http://127.0.0.1:8080/callback",
    scope=["user-read-private"],
    token_cache=FileTokenCache(".cache/spotify-sdk/token.json"),
)

# Local helper: opens browser and captures the callback automatically
auth.authorize_local()

client = SpotifyClient(auth_provider=auth)

Quick Start

from spotify_sdk import SpotifyClient

client = SpotifyClient(access_token="your-access-token")

# Get an album
album = client.albums.get("5K79FLRUCSysQnVESLcTdb")
print(f"{album.name} by {album.artists[0].name}")
# DeBÍ TiRAR MáS FOToS by Bad Bunny

# Get album tracks
tracks = client.albums.get_tracks(album.id)
for track in tracks.items:
    print(f"{track.track_number}. {track.name}")

# Close the underlying HTTP connection when done
client.close()

Using Context Managers

from spotify_sdk import SpotifyClient

with SpotifyClient(access_token="your-access-token") as client:
    album = client.albums.get("4aawyAB9vmqN3uQ7FjRGTy")
    print(album.name)

Async Support

import asyncio
from spotify_sdk import AsyncSpotifyClient


async def main():
    async with AsyncSpotifyClient(access_token="your-access-token") as client:
        album = await client.albums.get("4Uv86qWpGTxf7fU7lG5X6F")
        print(f"{album.name} by {album.artists[0].name}")
        # The College Dropout by Kanye West


asyncio.run(main())

Services

Service Access Key Methods
Albums client.albums get, get_tracks, get_saved
Artists client.artists get, get_albums
Audiobooks client.audiobooks get, get_chapters, get_saved
Chapters client.chapters get
Episodes client.episodes get, get_saved
Library client.library save_items, remove_items, check_contains
Player client.player get_playback_state, start_playback, pause_playback, skip_to_next, add_to_queue, and more
Playlists client.playlists get, get_items, create, add_items, remove_items, and more
Search client.search search
Shows client.shows get, get_episodes, get_saved
Tracks client.tracks get, get_saved
Users client.users get_current_profile, get_top_artists, get_top_tracks, get_followed_artists

See the full documentation for detailed method signatures and examples.

Error Handling

The SDK raises specific exceptions for different error types:

from spotify_sdk import (
    SpotifyClient,
    AuthenticationError,
    BadRequestError,
    ForbiddenError,
    NotFoundError,
    RateLimitError,
    ServerError,
)

try:
    album = client.albums.get("invalid_id")
except NotFoundError as e:
    print(f"Album not found: {e.message}")
except AuthenticationError as e:
    print(f"Invalid token: {e.message}")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
except ServerError as e:
    print(f"Spotify server error: {e.message}")

Exception Hierarchy

Exception HTTP Status Description
SpotifyError - Base exception for all SDK errors
AuthenticationError 401 Invalid or expired access token
BadRequestError 400 Invalid request parameters
ForbiddenError 403 Insufficient permissions
NotFoundError 404 Resource not found
RateLimitError 429 Rate limit exceeded
ServerError 5xx Spotify server error

Configuration

Client Options

client = SpotifyClient(
    access_token="your-access-token",
    timeout=30.0,  # Request timeout in seconds (default: 30.0)
    max_retries=3,  # Maximum retry attempts (default: 3)
)

Retry Behavior

The SDK automatically retries requests on:

  • Connection errors and timeouts
  • Rate limit responses (429) - respects Retry-After header
  • Server errors (5xx)

Retries use exponential backoff with jitter:

  • Initial delay: 0.5 seconds
  • Maximum delay: 8.0 seconds
  • Multiplier: 2x per retry

Models

All API responses are returned as Pydantic models with full type hints:

album = client.albums.get("<id>")

# Access typed attributes
print(album.name)  # str
print(album.release_date)  # str
print(album.total_tracks)  # int
print(album.artists)  # list[SimplifiedArtist]
print(album.images)  # list[Image]

# Models support forward compatibility
# Unknown fields from the API are preserved

Development

Clone the repository:

git clone https://github.com/jonathan343/spotify-sdk.git
cd spotify-sdk

Install dependencies with uv:

uv sync

Run tests:

uv run pytest

Run linting:

uv run ruff check .
uv run ruff format --check --preview .

Sync/Async Architecture

The SDK uses an async-first architecture. Async code under src/spotify_sdk/_async/ is the source of truth, and the sync code under src/spotify_sdk/_sync/ is auto-generated using unasync. Do not edit _sync/ files directly.

After making changes to _async/ source or tests/_async/, regenerate the sync code:

uv run python scripts/run_unasync.py

To verify sync code is up to date (same check that runs in CI):

uv run python scripts/run_unasync.py --check

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Release files for spotify-sdk 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spotify-sdk 0.10.1
File Size Uploaded
spotify_sdk-0.10.1.tar.gz 41.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spotify-sdk 0.10.1
File Interpreter ABI Platform
spotify_sdk-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 107.9 kB

Release files / spotify_sdk-0.10.1.tar.gz

Download URL spotify_sdk-0.10.1.tar.gz
Size 41.0 kB
Tags Source
SHA-256 checksum
How to use checksums
af273deca35d3ff6ec4e52c3ff5a852e4dd7cad8063d4c5753d1965e738335da
BLAKE2b-256 checksum
How to use checksums
270b51250a91e55a676783758fd5678ae1494c454ef7b8378996c9cbf7739317
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / spotify_sdk-0.10.1-py3-none-any.whl

Download URL spotify_sdk-0.10.1-py3-none-any.whl
Size 66.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac767375afeb949aab21c92fee0acf24f99bf5954657ac85906ef5082a3a6173
BLAKE2b-256 checksum
How to use checksums
e7d31d0556f9b3347d723aafe5ad2567ad3f1115f421568299246475680adffd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page