Skip to main content

Loading Data from Spotify to DuckDB using dlt in Python

Need help deploying these pipelines, or figuring out how to run them in your data stack?

Join our Slack community or book a call with our support engineer Violetta.

Loading data from Spotify to DuckDB using the dlt library allows you to efficiently manage and analyze large volumes of music, podcast, and video data. Spotify is a digital service that offers access to millions of songs and other content from creators worldwide. DuckDB is a fast in-process analytical database that supports a feature-rich SQL dialect and integrates deeply with client APIs. By leveraging the open-source dlt library, you can streamline the process of transferring data from Spotify into DuckDB for advanced analysis and insights. For more information about Spotify, visit Spotify.

dlt Key Features

  • Easy to get started: dlt is a Python library that is easy to use and understand. It is designed to be simple to use and easy to understand. Type pip install dlt and you are ready to go. Read more
  • Supports DuckDB: dlt supports DuckDB as a destination, allowing you to efficiently load and manage your data. Read more
  • Load data from APIs: Easily load data from various APIs into DuckDB. This tutorial demonstrates how to load data from the GitHub API. Read more
  • Incremental data loading: dlt supports incremental loading to ensure that only new data is loaded, avoiding duplicates and saving resources. Read more
  • Data exploration: Use built-in integrations like Streamlit to explore and visualize your data directly from your pipeline. Read more

Getting started with your pipeline locally

OpenAPI Source Generator dlt-init-openapi

This walkthrough makes use of the dlt-init-openapi generator cli tool. You can read more about it here. The code generated by this tool uses the dlt rest_api verified source, docs for this are here.

0. Prerequisites

dlt and dlt-init-openapi requires Python 3.9 or higher. Additionally, you need to have the pip package manager installed, and we recommend using a virtual environment to manage your dependencies. You can learn more about preparing your computer for dlt in our installation reference.

1. Install dlt and dlt-init-openapi

First you need to install the dlt-init-openapi cli tool.

pip install dlt-init-openapi

The dlt-init-openapi cli is a powerful generator which you can use to turn any OpenAPI spec into a dlt source to ingest data from that api. The quality of the generator source is dependent on how well the API is designed and how accurate the OpenAPI spec you are using is. You may need to make tweaks to the generated code, you can learn more about this here.

# generate pipeline
# NOTE: add_limit adds a global limit, you can remove this later
# NOTE: you will need to select which endpoints to render, you
# can just hit Enter and all will be rendered.
dlt-init-openapi spotify --url https://raw.githubusercontent.com/dlt-hub/dlt-init-openapi/devel/tests/cases/original_specs/spotify.json --global-limit 2
cd spotify_pipeline
# install generated requirements
pip install -r requirements.txt

The last command will install the required dependencies for your pipeline. The dependencies are listed in the requirements.txt:

dlt>=0.4.12

You now have the following folder structure in your project:

spotify_pipeline/
├── .dlt/
│ ├── config.toml # configs for your pipeline
│ └── secrets.toml # secrets for your pipeline
├── rest_api/ # The rest api verified source
│ └── ...
├── spotify/
│ └── __init__.py # TODO: possibly tweak this file
├── spotify_pipeline.py # your main pipeline script
├── requirements.txt # dependencies for your pipeline
└── .gitignore # ignore files for git (not required)

1.1. Tweak spotify/__init__.py

This file contains the generated configuration of your rest_api. You can continue with the next steps and leave it as is, but you might want to come back here and make adjustments if you need your rest_api source set up in a different way. The generated file for the spotify source will look like this:

Click to view full file (1102 lines)

from typing import List

import dlt
from dlt.extract.source import DltResource
from rest_api import rest_api_source
from rest_api.typing import RESTAPIConfig


@dlt.source(name="spotify_source", max_table_nesting=2)
def spotify_source(
base_url: str = dlt.config.value,
) -> List[DltResource]:

# source configuration
source_config: RESTAPIConfig = {
"client": {
"base_url": base_url,
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"total",
},
},
"resources":
[
# Get Spotify catalog information for a single album.
{
"name": "get_an_album",
"table_name": "album",
"endpoint": {
"data_selector": "available_markets",
"path": "/albums/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_albums",
"field": "id",
},

},
"paginator": {
"type":
"json_response",
"next_url_path":
"tracks.next",
},
}
},
# Get Spotify catalog information for multiple albums identified by their Spotify IDs.
{
"name": "get_multiple_albums",
"table_name": "album_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "albums",
"path": "/albums",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for several artists based on their Spotify IDs.
{
"name": "get_multiple_artists",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "artists",
"path": "/artists",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Get Spotify catalog information for a single artist identified by their unique Spotify ID.
{
"name": "get_an_artist",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/artists/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_artists",
"field": "id",
},

},
}
},
# Get Spotify catalog information about artists similar to a given artist. Similarity is based on analysis of the Spotify community's [listening history](http://news.spotify.com/se/2010/02/03/related-artists/).
{
"name": "get_an_artists_related_artists",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "artists",
"path": "/artists/{id}/related-artists",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_artists",
"field": "id",
},

},
}
},
# Get the current user's followed artists.
{
"name": "get_followed",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "artists.items",
"path": "/me/following",
"paginator": {
"type":
"json_response",
"next_url_path":
"artists.next",
},
}
},
# Get information about the user’s current playback state, including track or episode, progress, and active device.
{
"name": "get_information_about_the_users_current_playback",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "item.artists",
"path": "/me/player",
"params": {
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",
# "additional_types": "OPTIONAL_CONFIG",

},
}
},
# Get the object currently being played on the user's Spotify account.
{
"name": "get_the_users_currently_playing_track",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "item.artists",
"path": "/me/player/currently-playing",
"params": {
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",
# "additional_types": "OPTIONAL_CONFIG",

},
}
},
# Get the current user's top artists based on calculated affinity.
{
"name": "get_users_top_artists",
"table_name": "artist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/me/top/artists",
"params": {
# the parameters below can optionally be configured
# "time_range": "medium_term",

},
}
},
# Get a low-level audio analysis for a track in the Spotify catalog. The audio analysis describes the track’s structure and musical content, including rhythm, pitch, and timbre.
{
"name": "get_audio_analysis",
"table_name": "audio_analysis_object",
"endpoint": {
"data_selector": "$",
"path": "/audio-analysis/{id}",
"params": {
"id": "FILL_ME_IN", # TODO: fill in required path parameter

},
}
},
# Get audio features for multiple tracks based on their Spotify IDs.
{
"name": "get_several_audio_features",
"table_name": "audio_features_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "audio_features",
"path": "/audio-features",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Get audio feature information for a single track identified by its unique Spotify ID.
{
"name": "get_audio_features",
"table_name": "audio_features_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/audio-features/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_several_audio_features",
"field": "id",
},

},
}
},
# Get Spotify catalog information for several audiobooks identified by their Spotify IDs.<br /> **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "get_multiple_audiobooks",
"table_name": "audiobook_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "audiobooks",
"path": "/audiobooks",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for a single audiobook.<br /> **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "get_an_audiobook",
"table_name": "author_object",
"endpoint": {
"data_selector": "authors",
"path": "/audiobooks/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_audiobooks",
"field": "id",
},

},
"paginator": {
"type":
"json_response",
"next_url_path":
"chapters.next",
},
}
},
# Retrieve a list of available genres seed parameter values for [recommendations](/documentation/web-api/reference/#/operations/get-recommendations).
{
"name": "get_recommendation_genres",
"table_name": "available_genre_seed",
"endpoint": {
"data_selector": "genres",
"path": "/recommendations/available-genre-seeds",
}
},
# Get a list of categories used to tag items in Spotify (on, for example, the Spotify player’s “Browse” tab).
{
"name": "get_categories",
"table_name": "category_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "categories.items",
"path": "/browse/categories",
"params": {
# the parameters below can optionally be configured
# "country": "OPTIONAL_CONFIG",
# "locale": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"categories.total",
},
}
},
# Get a single category used to tag items in Spotify (on, for example, the Spotify player’s “Browse” tab).
{
"name": "get_a_category",
"table_name": "category_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/browse/categories/{category_id}",
"params": {
"category_id": {
"type": "resolve",
"resource": "get_categories",
"field": "id",
},
# the parameters below can optionally be configured
# "country": "OPTIONAL_CONFIG",
# "locale": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for several chapters identified by their Spotify IDs.<br /> **Note: Chapters are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "get_several_chapters",
"table_name": "chapter_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "chapters",
"path": "/chapters",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for a single chapter.<br /> **Note: Chapters are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "get_a_chapter",
"table_name": "chapter_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/chapters/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_several_chapters",
"field": "id",
},
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Check if one or more albums is already saved in the current Spotify user's 'Your Music' library.
{
"name": "check_users_saved_albums",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/albums/contains",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check if one or more audiobooks are already saved in the current Spotify user's library.
{
"name": "check_users_saved_audiobooks",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/audiobooks/contains",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check if one or more episodes is already saved in the current Spotify user's 'Your Episodes' library.
{
"name": "check_users_saved_episodes",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/episodes/contains",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check to see if the current user is following one or more artists or other Spotify users.
{
"name": "check_current_user_follows",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/following/contains",
"params": {
"type": "FILL_ME_IN", # TODO: fill in required query parameter
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check if one or more shows is already saved in the current Spotify user's library.
{
"name": "check_users_saved_shows",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/shows/contains",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check if one or more tracks is already saved in the current Spotify user's 'Your Music' library.
{
"name": "check_users_saved_tracks",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/me/tracks/contains",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Check to see if one or more Spotify users are following a specified playlist.
{
"name": "check_if_user_follows_playlist",
"table_name": "contain",
"endpoint": {
"data_selector": "$",
"path": "/playlists/{playlist_id}/followers/contains",
"params": {
"playlist_id": "FILL_ME_IN", # TODO: fill in required path parameter
"ids": "FILL_ME_IN", # TODO: fill in required query parameter

},
}
},
# Get information about a user’s available devices.
{
"name": "get_a_users_available_devices",
"table_name": "device_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "devices",
"path": "/me/player/devices",
}
},
# Get Spotify catalog information for several episodes based on their Spotify IDs.
{
"name": "get_multiple_episodes",
"table_name": "episode_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "episodes",
"path": "/episodes",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for a single episode identified by its unique Spotify ID.
{
"name": "get_an_episode",
"table_name": "episode_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/episodes/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_episodes",
"field": "id",
},
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get detailed profile information about the current user (including the current user's username).
{
"name": "get_current_users_profile",
"table_name": "image_object",
"endpoint": {
"data_selector": "images",
"path": "/me",
}
},
# Get a playlist owned by a Spotify user.
{
"name": "get_playlist",
"table_name": "image_object",
"endpoint": {
"data_selector": "images",
"path": "/playlists/{playlist_id}",
"params": {
"playlist_id": "FILL_ME_IN", # TODO: fill in required path parameter

},
"paginator": {
"type":
"json_response",
"next_url_path":
"tracks.next",
},
}
},
# Get the current image associated with a specific playlist.
{
"name": "get_playlist_cover",
"table_name": "image_object",
"endpoint": {
"data_selector": "$",
"path": "/playlists/{playlist_id}/images",
"params": {
"playlist_id": "FILL_ME_IN", # TODO: fill in required path parameter

},
}
},
# Get the list of markets where Spotify is available.
{
"name": "get_available_markets",
"table_name": "market",
"endpoint": {
"data_selector": "markets",
"path": "/markets",
}
},
# Get tracks from the current user's recently played tracks. _**Note**: Currently doesn't support podcast episodes._
{
"name": "get_recently_played",
"table_name": "play_history_object",
"endpoint": {
"data_selector": "items",
"path": "/me/player/recently-played",
"paginator": {
"type":
"json_response",
"next_url_path":
"next",
},
}
},
# Get full details of the items of a playlist owned by a Spotify user.
{
"name": "get_playlists_tracks",
"table_name": "playlist_track_object",
"endpoint": {
"data_selector": "items",
"path": "/playlists/{playlist_id}/tracks",
"params": {
"playlist_id": "FILL_ME_IN", # TODO: fill in required path parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",
# "fields": "OPTIONAL_CONFIG",
# "additional_types": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
100,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"total",
},
}
},
# Get public profile information about a Spotify user.
{
"name": "get_users_profile",
"table_name": "public_user_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/users/{user_id}",
"params": {
"user_id": "FILL_ME_IN", # TODO: fill in required path parameter

},
}
},
# Get the list of objects that make up the user's queue.
{
"name": "get_queue",
"table_name": "queue",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "queue",
"path": "/me/player/queue",
}
},
# Recommendations are generated based on the available information for a given seed entity and matched against similar artists and tracks. If there is sufficient information about the provided seeds, a list of tracks will be returned together with pool size details. For artists and tracks that are very new or obscure there might not be enough data to generate a list of tracks.
{
"name": "get_recommendations",
"table_name": "recommendation_seed_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "seeds",
"path": "/recommendations",
"params": {
# the parameters below can optionally be configured
# "limit": "20",
# "market": "OPTIONAL_CONFIG",
# "seed_artists": "OPTIONAL_CONFIG",
# "seed_genres": "OPTIONAL_CONFIG",
# "seed_tracks": "OPTIONAL_CONFIG",
# "min_acousticness": "OPTIONAL_CONFIG",
# "max_acousticness": "OPTIONAL_CONFIG",
# "target_acousticness": "OPTIONAL_CONFIG",
# "min_danceability": "OPTIONAL_CONFIG",
# "max_danceability": "OPTIONAL_CONFIG",
# "target_danceability": "OPTIONAL_CONFIG",
# "min_duration_ms": "OPTIONAL_CONFIG",
# "max_duration_ms": "OPTIONAL_CONFIG",
# "target_duration_ms": "OPTIONAL_CONFIG",
# "min_energy": "OPTIONAL_CONFIG",
# "max_energy": "OPTIONAL_CONFIG",
# "target_energy": "OPTIONAL_CONFIG",
# "min_instrumentalness": "OPTIONAL_CONFIG",
# "max_instrumentalness": "OPTIONAL_CONFIG",
# "target_instrumentalness": "OPTIONAL_CONFIG",
# "min_key": "OPTIONAL_CONFIG",
# "max_key": "OPTIONAL_CONFIG",
# "target_key": "OPTIONAL_CONFIG",
# "min_liveness": "OPTIONAL_CONFIG",
# "max_liveness": "OPTIONAL_CONFIG",
# "target_liveness": "OPTIONAL_CONFIG",
# "min_loudness": "OPTIONAL_CONFIG",
# "max_loudness": "OPTIONAL_CONFIG",
# "target_loudness": "OPTIONAL_CONFIG",
# "min_mode": "OPTIONAL_CONFIG",
# "max_mode": "OPTIONAL_CONFIG",
# "target_mode": "OPTIONAL_CONFIG",
# "min_popularity": "OPTIONAL_CONFIG",
# "max_popularity": "OPTIONAL_CONFIG",
# "target_popularity": "OPTIONAL_CONFIG",
# "min_speechiness": "OPTIONAL_CONFIG",
# "max_speechiness": "OPTIONAL_CONFIG",
# "target_speechiness": "OPTIONAL_CONFIG",
# "min_tempo": "OPTIONAL_CONFIG",
# "max_tempo": "OPTIONAL_CONFIG",
# "target_tempo": "OPTIONAL_CONFIG",
# "min_time_signature": "OPTIONAL_CONFIG",
# "max_time_signature": "OPTIONAL_CONFIG",
# "target_time_signature": "OPTIONAL_CONFIG",
# "min_valence": "OPTIONAL_CONFIG",
# "max_valence": "OPTIONAL_CONFIG",
# "target_valence": "OPTIONAL_CONFIG",

},
}
},
# Get a list of the albums saved in the current Spotify user's 'Your Music' library.
{
"name": "get_users_saved_albums",
"table_name": "saved_album_object",
"endpoint": {
"data_selector": "items",
"path": "/me/albums",
"params": {
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get a list of the audiobooks saved in the current Spotify user's 'Your Music' library.
{
"name": "get_users_saved_audiobooks",
"table_name": "saved_audiobook_object",
"endpoint": {
"data_selector": "items",
"path": "/me/audiobooks",
}
},
# Get a list of the episodes saved in the current Spotify user's library.
{
"name": "get_users_saved_episodes",
"table_name": "saved_episode_object",
"endpoint": {
"data_selector": "items",
"path": "/me/episodes",
"params": {
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get a list of shows saved in the current Spotify user's library. Optional parameters can be used to limit the number of shows returned.
{
"name": "get_users_saved_shows",
"table_name": "saved_show_object",
"endpoint": {
"data_selector": "items",
"path": "/me/shows",
}
},
# Get a list of the songs saved in the current Spotify user's 'Your Music' library.
{
"name": "get_users_saved_tracks",
"table_name": "saved_track_object",
"endpoint": {
"data_selector": "items",
"path": "/me/tracks",
"params": {
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for a single show identified by its unique Spotify ID.
{
"name": "get_a_show",
"table_name": "show",
"endpoint": {
"data_selector": "available_markets",
"path": "/shows/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_shows",
"field": "id",
},

},
"paginator": {
"type":
"json_response",
"next_url_path":
"episodes.next",
},
}
},
# Get Spotify catalog information about an artist's albums.
{
"name": "get_an_artists_albums",
"table_name": "simplified_album_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/artists/{id}/albums",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_artists",
"field": "id",
},
# the parameters below can optionally be configured
# "include_groups": "OPTIONAL_CONFIG",
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab).
{
"name": "get_new_releases",
"table_name": "simplified_album_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "albums.items",
"path": "/browse/new-releases",
"params": {
# the parameters below can optionally be configured
# "country": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"albums.total",
},
}
},
# Get Spotify catalog information about albums, artists, playlists, tracks, shows, episodes or audiobooks that match a keyword string.<br /> **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "search",
"table_name": "simplified_album_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "albums.items",
"path": "/search",
"params": {
"q": "FILL_ME_IN", # TODO: fill in required query parameter
"type": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",
# "include_external": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"albums.total",
},
}
},
# Get Spotify catalog information about an audiobook's chapters.<br /> **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.**
{
"name": "get_audiobook_chapters",
"table_name": "simplified_chapter_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/audiobooks/{id}/chapters",
"params": {
"id": "FILL_ME_IN", # TODO: fill in required path parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information about an show’s episodes. Optional parameters can be used to limit the number of episodes returned.
{
"name": "get_a_shows_episodes",
"table_name": "simplified_episode_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/shows/{id}/episodes",
"params": {
"id": "FILL_ME_IN", # TODO: fill in required path parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get a list of Spotify playlists tagged with a particular category.
{
"name": "get_a_categories_playlists",
"table_name": "simplified_playlist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "playlists.items",
"path": "/browse/categories/{category_id}/playlists",
"params": {
"category_id": {
"type": "resolve",
"resource": "get_categories",
"field": "id",
},
# the parameters below can optionally be configured
# "country": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"playlists.total",
},
}
},
# Get a list of Spotify featured playlists (shown, for example, on a Spotify player's 'Browse' tab).
{
"name": "get_featured_playlists",
"table_name": "simplified_playlist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "playlists.items",
"path": "/browse/featured-playlists",
"params": {
# the parameters below can optionally be configured
# "country": "OPTIONAL_CONFIG",
# "locale": "OPTIONAL_CONFIG",
# "timestamp": "OPTIONAL_CONFIG",

},
"paginator": {
"type":
"offset",
"limit":
50,
"offset_param":
"offset",
"limit_param":
"limit",
"total_path":
"playlists.total",
},
}
},
# Get a list of the playlists owned or followed by the current Spotify user.
{
"name": "get_a_list_of_current_users_playlists",
"table_name": "simplified_playlist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/me/playlists",
}
},
# Get a list of the playlists owned or followed by a Spotify user.
{
"name": "get_list_users_playlists",
"table_name": "simplified_playlist_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/users/{user_id}/playlists",
"params": {
"user_id": "FILL_ME_IN", # TODO: fill in required path parameter

},
}
},
# Get Spotify catalog information for several shows based on their Spotify IDs.
{
"name": "get_multiple_shows",
"table_name": "simplified_show_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "shows",
"path": "/shows",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information about an album’s tracks. Optional parameters can be used to limit the number of tracks returned.
{
"name": "get_an_albums_tracks",
"table_name": "simplified_track_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/albums/{id}/tracks",
"params": {
"id": "FILL_ME_IN", # TODO: fill in required path parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information about an artist's top tracks by country.
{
"name": "get_an_artists_top_tracks",
"table_name": "track_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "tracks",
"path": "/artists/{id}/top-tracks",
"params": {
"id": {
"type": "resolve",
"resource": "get_multiple_artists",
"field": "id",
},
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get the current user's top tracks based on calculated affinity.
{
"name": "get_users_top_tracks",
"table_name": "track_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "items",
"path": "/me/top/tracks",
"params": {
# the parameters below can optionally be configured
# "time_range": "medium_term",

},
}
},
# Get Spotify catalog information for multiple tracks based on their Spotify IDs.
{
"name": "get_several_tracks",
"table_name": "track_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "tracks",
"path": "/tracks",
"params": {
"ids": "FILL_ME_IN", # TODO: fill in required query parameter
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
# Get Spotify catalog information for a single track identified by its unique Spotify ID.
{
"name": "get_track",
"table_name": "track_object",
"primary_key": "id",
"write_disposition": "merge",
"endpoint": {
"data_selector": "$",
"path": "/tracks/{id}",
"params": {
"id": {
"type": "resolve",
"resource": "get_several_tracks",
"field": "id",
},
# the parameters below can optionally be configured
# "market": "OPTIONAL_CONFIG",

},
}
},
]
}

return rest_api_source(source_config)

2. Configuring your source and destination credentials

info

dlt-init-openapi will try to detect which authentication mechanism (if any) is used by the API in question and add a placeholder in your secrets.toml.

  • If you know your API needs authentication, but none was detected, you can learn more about adding authentication to the rest_api here.
  • OAuth detection currently is not supported, but you can supply your own authentication mechanism as outlined here.

The dlt cli will have created a .dlt directory in your project folder. This directory contains a config.toml file and a secrets.toml file that you can use to configure your pipeline. The automatically created version of these files look like this:

generated config.toml


[runtime]
log_level="INFO"

[sources.spotify]
# Base URL for the API
base_url = "https://api.spotify.com/v1"

generated secrets.toml


[sources.spotify]
# secrets for your spotify source
# example_api_key = "example value"

2.1. Adjust the generated code to your usecase

Further help setting up your source and destinations

At this time, the dlt-init-openapi cli tool will always create pipelines that load to a local duckdb instance. Switching to a different destination is trivial, all you need to do is change the destination parameter in spotify_pipeline.py to duckdb and supply the credentials as outlined in the destination doc linked below.

  • Read more about setting up the rest_api source in our docs.
  • Read more about setting up the DuckDB destination in our docs.

3. Running your pipeline for the first time

The dlt cli has also created a main pipeline script for you at spotify_pipeline.py, as well as a folder spotify that contains additional python files for your source. These files are your local copies which you can modify to fit your needs. In some cases you may find that you only need to do small changes to your pipelines or add some configurations, in other cases these files can serve as a working starting point for your code, but will need to be adjusted to do what you need them to do.

The main pipeline script will look something like this:


import dlt

from spotify import spotify_source


if __name__ == "__main__":
pipeline = dlt.pipeline(
pipeline_name="spotify_pipeline",
destination='duckdb',
dataset_name="spotify_data",
progress="log",
export_schema_path="schemas/export"
)
source = spotify_source()
info = pipeline.run(source)
print(info)

Provided you have set up your credentials, you can run your pipeline like a regular python script with the following command:

python spotify_pipeline.py

4. Inspecting your load result

You can now inspect the state of your pipeline with the dlt cli:

dlt pipeline spotify_pipeline info

You can also use streamlit to inspect the contents of your DuckDB destination for this:

# install streamlit
pip install streamlit
# run the streamlit app for your pipeline with the dlt cli:
dlt pipeline spotify_pipeline show

5. Next steps to get your pipeline running in production

One of the beauties of dlt is, that we are just a plain Python library, so you can run your pipeline in any environment that supports Python >= 3.8. We have a couple of helpers and guides in our docs to get you there:

The Deploy section will show you how to deploy your pipeline to

  • Deploy with GitHub Actions: Learn how to deploy your dlt pipeline using GitHub Actions for automated CI/CD. Follow the steps here.
  • Deploy with Airflow and Google Composer: Use Google Composer to manage your Airflow environments and deploy your dlt pipeline. Detailed instructions can be found here.
  • Deploy with Google Cloud Functions: Set up and deploy your dlt pipeline using Google Cloud Functions for a serverless approach. Follow the guide here.
  • Explore Other Deployment Options: Discover various other methods to deploy your dlt pipeline, including different cloud providers and services. Check out the comprehensive guide here.

The running in production section will teach you about:

  • How to Monitor your pipeline: Learn how to effectively monitor your dlt pipeline in production to ensure smooth operation and quick identification of any issues. Read more here.
  • Set up alerts: Set up alerts to get notified about important events and issues in your dlt pipeline. This ensures that you can take timely action to address any problems. Find out how here.
  • And set up tracing: Implement tracing to gain detailed insights into the execution of your dlt pipeline, helping you to debug and optimize its performance. Learn more here.

Available Sources and Resources

For this verified source the following sources and resources are available

Source Spotify

Spotify source provides data on tracks, albums, playlists, users, devices, and audio analysis.

Resource NameWrite DispositionDescription
image_objectappendRepresents image data associated with various Spotify entities.
simplified_track_objectappendContains simplified information about a Spotify track.
device_objectappendInformation about a user's Spotify devices.
saved_episode_objectappendDetails of episodes saved by the user.
simplified_playlist_objectappendSimplified details of a Spotify playlist.
audio_analysis_objectappendDetailed audio analysis for a single track.
chapter_objectappendRepresents chapters in audiobooks or podcasts.
saved_track_objectappendInformation about tracks saved by the user.
category_objectappendRepresents categories or genres available on Spotify.
public_user_objectappendPublic profile information of a Spotify user.
showappendInformation about a show (podcast or other).
artist_objectappendDetails about an artist on Spotify.
saved_audiobook_objectappendInformation about audiobooks saved by the user.
simplified_album_objectappendSimplified details of a Spotify album.
containappendGeneral container for various Spotify data.
queueappendDetails about the user's current playback queue.
simplified_chapter_objectappendSimplified information about chapters in audiobooks or podcasts.
play_history_objectappendUser's play history data.
albumappendInformation about a specific album.
simplified_show_objectappendSimplified details of a show (podcast or other).
author_objectappendInformation about authors of audiobooks or other content.
audio_features_objectappendAcoustic features of a track.
album_objectappendDetailed information about an album.
episode_objectappendInformation about a specific episode of a show.
recommendation_seed_objectappendSeed data used for generating recommendations.
marketappendMarket or region information for Spotify content.
playlist_track_objectappendInformation about tracks in a playlist.
saved_album_objectappendDetails of albums saved by the user.
simplified_episode_objectappendSimplified information about episodes of a show.
track_objectappendDetailed information about a Spotify track.
available_genre_seedappendAvailable genre seeds for recommendations.
audiobook_objectappendDetailed information about an audiobook.
saved_show_objectappendInformation about shows saved by the user.

Additional pipeline guides

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.