Overview
The Client Credentials flow is an OAuth 2.0 authentication method for server-to-server applications. It provides automatic token management and refresh, making it ideal for:- Backend services and APIs
- Automated data pipelines
- Applications that don’t require user-specific data
- Long-running production applications
The Client Credentials flow provides app-level access only. It cannot access user-specific endpoints like playlists, library, or playback. For user authentication, use the Authorization Code flow.
Class: ClientCredentials
TheClientCredentials class implements the OAuth 2.0 client credentials grant type with automatic token caching and refresh.
Constructor
Parameters
Spotify application client ID. If not provided, reads from
SPOTIFY_SDK_CLIENT_ID environment variable.Spotify application client secret. If not provided, reads from
SPOTIFY_SDK_CLIENT_SECRET environment variable.Optional token cache for storing access tokens between requests. Defaults to
InMemoryTokenCache(). Use FileTokenCache for persistent storage.HTTP request timeout in seconds for token requests.
Maximum number of retry attempts for failed token requests. Retries use exponential backoff with jitter.
Number of seconds before token expiry to proactively refresh. Prevents requests with nearly-expired tokens.
Optional custom HTTP client. If not provided, a new client is created and managed internally.
Methods
get_access_token()
Returns a valid access token, automatically fetching or refreshing as needed.str - A valid access token
close()
Closes the HTTP client and releases resources.Basic Usage
Using SpotifyClient.from_client_credentials()
The simplest way to use client credentials authentication:Using Environment Variables
Using Context Managers
Advanced Usage
Custom Auth Provider
For more control, create aClientCredentials instance directly:
Persistent Token Caching
UseFileTokenCache to persist tokens across application restarts:
Async Support
Token Refresh Behavior
TheClientCredentials class automatically handles token lifecycle:
Retry Logic
Token requests automatically retry with exponential backoff on:- Connection errors and timeouts
- Server errors (5xx status codes)
- Initial delay: 0.5 seconds
- Maximum delay: 8.0 seconds
- Multiplier: 2x per retry
- Jitter: Random 0.5-1.0x variation
Error Handling
When to Use Client Credentials
Use client credentials when:- Building backend services or APIs
- Accessing public catalog data (albums, artists, tracks, etc.)
- Running automated scripts or data pipelines
- You don’t need user-specific data
- You need automatic token refresh
- You need to access user-specific endpoints (playlists, library, playback, etc.)
- Building user-facing applications that require user consent
- You need user scopes or permissions
Environment Variables
The SDK recognizes these environment variables:| Variable | Description |
|---|---|
SPOTIFY_SDK_CLIENT_ID | Spotify application client ID |
SPOTIFY_SDK_CLIENT_SECRET | Spotify application client secret |
Related Classes
InMemoryTokenCache
Simple in-memory token cache (default).FileTokenCache
JSON file-based token cache for persistence.path(str): File path for storing the token. Defaults to.spotify_sdk_token.json. The file is created with0600permissions.
Related
- Access Token Authentication - Simple direct token authentication
- Authorization Code Flow - User authentication with scopes
- Error Handling - Handling authentication errors