# Get an albumalbum = client.albums.get("4aawyAB9vmqN3uQ7FjRGTy")print(f"{album.name} by {album.artists[0].name}")# Output: "Global Warming by Pitbull"# Access typed attributesprint(f"Released: {album.release_date}")print(f"Total tracks: {album.total_tracks}")
All responses are fully typed Pydantic models, so you get autocomplete and type checking in your IDE.
4
Clean up resources
client.close()
Always close the client when you’re done to release network resources.
The recommended way to use the client is with a context manager, which automatically handles cleanup:
from spotify_sdk import SpotifyClientwith SpotifyClient.from_client_credentials( client_id="your-client-id", client_secret="your-client-secret",) as client: album = client.albums.get("4aawyAB9vmqN3uQ7FjRGTy") print(f"{album.name} by {album.artists[0].name}")# Client is automatically closed when exiting the with block
The SDK provides specific exceptions for different error types:
from spotify_sdk import ( SpotifyClient, NotFoundError, AuthenticationError, RateLimitError,)with SpotifyClient.from_client_credentials( client_id="your-client-id", client_secret="your-client-secret",) as client: 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 credentials: {e.message}") except RateLimitError as e: print(f"Rate limited. Retry after {e.retry_after} seconds")
The SDK automatically retries requests on rate limits (429), connection errors, and server errors (5xx) using exponential backoff with jitter. Most transient errors are handled without manual intervention.