Skip to main content

Overview

The simplest authentication method for the Spotify SDK is using a direct access token. This approach is ideal for:
  • Quick prototyping and testing
  • Short-lived applications
  • Scenarios where you manually obtain tokens from the Spotify Developer Dashboard
  • Server-side scripts with pre-generated tokens
Access tokens expire after 1 hour. This method does not support automatic token refresh. For production applications requiring long-lived access, use Client Credentials or Authorization Code flows.

Basic Usage

Creating a Client with Access Token

from spotify_sdk import SpotifyClient

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

# Use the client
album = client.albums.get("5K79FLRUCSysQnVESLcTdb")
print(f"{album.name} by {album.artists[0].name}")

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}")

asyncio.run(main())

Constructor Parameters

access_token
str
required
A valid Spotify access token. You can obtain one from the Spotify Developer Dashboard or through OAuth flows.
timeout
float
default:"30.0"
Request timeout in seconds.
max_retries
int
default:"3"
Maximum number of retry attempts for failed requests. The SDK automatically retries on connection errors, timeouts, rate limits (429), and server errors (5xx).

Token Expiration Handling

Access tokens expire after 1 hour. When using direct access token authentication, you must handle expiration manually:
from spotify_sdk import SpotifyClient, AuthenticationError

def get_data_with_token_refresh():
    access_token = get_fresh_token()  # Your token refresh logic
    
    with SpotifyClient(access_token=access_token) as client:
        try:
            album = client.albums.get("album_id_123")
            return album
        except AuthenticationError as e:
            # Token expired - get a new one
            new_token = get_fresh_token()
            
            # Create a new client with the refreshed token
            with SpotifyClient(access_token=new_token) as new_client:
                album = new_client.albums.get("album_id_123")
                return album
For automatic token refresh, consider using ClientCredentials or AuthorizationCode authentication providers.

Getting an Access Token

1

Create a Spotify App

Visit the Spotify Developer Dashboard and create a new application.
2

Get Your Credentials

Note your Client ID and Client Secret from the app settings.
3

Generate a Token

Use the Spotify Web API Console or implement an OAuth flow to generate an access token. For quick testing, you can use the Spotify Web API Console.

When to Use Access Token Authentication

Use access token authentication when:
  • Building quick prototypes or proof-of-concepts
  • Running one-off scripts or data analysis tasks
  • Testing API endpoints manually
  • You have a secure way to refresh tokens externally
Avoid access token authentication when:
  • Building production applications (use Client Credentials or Authorization Code instead)
  • You need automatic token refresh
  • You need to access user-specific endpoints (use Authorization Code flow)
  • Your application runs continuously (tokens expire after 1 hour)

Build docs developers (and LLMs) love