Last modified: Feb 01, 2026 By Alexander Williams

Python Spotify API Guide for Beginners

Do you love music and Python? The Spotify API is your bridge. It lets your code interact with the world's largest music streaming service.

You can search for songs. You can analyze playlists. You can even build your own music recommendation bot. This guide will show you how.

We will start from the very beginning. No prior API experience is needed. If you are new to APIs, our Python API Tutorial for Beginners is a great starting point.

What is the Spotify Web API?

The Spotify Web API is a RESTful service. It provides access to Spotify's vast database. This includes songs, artists, albums, and playlists.

You can read data. You can also modify your personal library. This makes it perfect for automation and personal projects.

To use it, you need to authenticate your application. Spotify uses OAuth 2.0 for security. This ensures only authorized apps can access user data.

Setting Up Your Spotify Developer Account

First, you need a Spotify account. Go to the Spotify Developer Dashboard.

Log in with your Spotify credentials. Click "Create an App". Give your app a name and description.

After creation, you will see your Client ID and Client Secret. These are your app's credentials. Keep them safe.

You must also set a Redirect URI. For testing, use http://localhost:8888/callback. This is where Spotify sends users after they log in.

Installing the Spotipy Library

Spotify provides an official API. But we will use a Python wrapper called Spotipy. It simplifies the complex authentication process.

Install it using pip. Open your terminal or command prompt.


pip install spotipy
    

That's it. You are ready to write code. Understanding Python API Calls will help you grasp the underlying concepts.

Authenticating with the Spotify API

Authentication is the first step. You must prove your app is allowed to make requests. We will use the Client Credentials flow for public data.

Create a new Python file. Import spotipy and the necessary manager.


import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

# Replace with your credentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

# Create authentication manager
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)

# Create the Spotify API client
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

print("Authentication successful! API client is ready.")
    

This flow gives you access to public data. For user-specific actions, you need user authorization. We will cover that later.

Searching for Tracks and Artists

The search method is powerful. You can find music by track name, artist, or album.

Let's search for a popular song.


# Search for the track 'Blinding Lights' by The Weeknd
results = sp.search(q='track:Blinding Lights artist:The Weeknd', type='track', limit=1)

# Extract the first track
track = results['tracks']['items'][0]

print(f"Track Name: {track['name']}")
print(f"Artist: {track['artists'][0]['name']}")
print(f"Album: {track['album']['name']}")
print(f"Spotify URI: {track['uri']}")
print(f"Preview URL: {track['preview_url']}")
    

Track Name: Blinding Lights
Artist: The Weeknd
Album: After Hours
Spotify URI: spotify:track:0VjIjW4GlUZAMYd2vXMi3b
Preview URL: https://p.scdn.co/mp3-preview/...
    

The q parameter is your query. Use type to specify if you want 'track', 'artist', or 'album'. The limit controls how many results you get back.

Getting Artist and Album Details

Once you have an artist's ID or URI, you can get detailed info. Let's get data about an artist.


# Get artist info for Taylor Swift (using her Spotify ID)
artist_id = '06HL4z0CvFAxyc27GXpf02'
artist = sp.artist(artist_id)

print(f"Artist: {artist['name']}")
print(f"Genres: {', '.join(artist['genres'][:3])}")  # Show first 3 genres
print(f"Popularity Score: {artist['popularity']}/100")
print(f"Followers: {artist['followers']['total']:,}")
    

You can do the same for albums. Use the album method with an album ID. This is similar to pulling data from other services, like in our Python API Data Pulling Guide.

Working with User Data and Playlists

To access private user data, you need user authorization. This uses a different OAuth flow. It requires the user to log in and grant permissions.

Here is a basic setup for user authentication. You will need to set the redirect URI in your app dashboard.


import spotipy
from spotipy.oauth2 import SpotifyOAuth

# Define your app's scope - what you want to access
scope = "user-library-read playlist-modify-private"

# Create OAuth object
sp_user = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    redirect_uri='http://localhost:8888/callback',
    scope=scope
))

# Get the current user's saved tracks
saved_tracks = sp_user.current_user_saved_tracks(limit=5)
print("Your recently saved tracks:")
for idx, item in enumerate(saved_tracks['items']):
    track = item['track']
    print(f"{idx+1}. {track['name']} by {track['artists'][0]['name']}")
    

The first time you run this, it will open a browser. You must log in and approve the permissions. After that, a token is saved locally.

Creating and Managing Playlists

Automating playlist management is a common use case. Let's create a new playlist and add a song to it.


# Get the current user's ID
current_user = sp_user.current_user()
user_id = current_user['id']

# Create a new private playlist
new_playlist = sp_user.user_playlist_create(
    user=user_id,
    name='My Python Generated Playlist',
    public=False,
    description='Created automatically with Spotipy!'
)

playlist_id = new_playlist['id']
print(f"Created playlist: {new_playlist['name']} (ID: {playlist_id})")

# Add a track to the playlist (using the URI from earlier)
track_uri = 'spotify:track:0Vj