GitHub
Back to Documentation
Created by Bilal Ashraf

Data Source: spotify_tracks

Retrieves and recommends Spotify tracks based on various criteria such as genre, mood, artist, and more. This data source is useful for creating dynamic playlists with recommended tracks.

Example Usage

# Get tracks based on genre and mood
data "spotify_tracks" "chill_electronic" {
  genre = "electronic"
  mood  = "chill"
  limit = 20
}

# Get tracks based on seed tracks
data "spotify_tracks" "similar" {
  seed_tracks = ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]
  limit = 15
}

# Get tracks based on artist
data "spotify_tracks" "artist_tracks" {
  seed_artists = ["spotify:artist:4Z8W4fKeB5YxbusRsdQVPb"]
  limit = 10
}

# Create a playlist with the recommended tracks
resource "spotify_playlist" "recommended" {
  name        = "Recommended Chill Electronic"
  description = "Tracks recommended based on electronic genre and chill mood"
  public      = true
  tracks      = data.spotify_tracks.chill_electronic.ids
}

Dynamic Recommendations Example

data "spotify_time" "now" {}
data "spotify_weather" "current" {}

data "spotify_tracks" "dynamic" {
  genre = data.spotify_time.now.genre
  mood  = data.spotify_weather.current.mood
  limit = 25
}

resource "spotify_playlist" "dynamic_recommendations" {
  name        = "${data.spotify_time.now.time_of_day} ${data.spotify_weather.current.condition} Mix"
  description = "Tracks for ${data.spotify_time.now.time_of_day} during ${data.spotify_weather.current.condition} weather"
  public      = true
  tracks      = data.spotify_tracks.dynamic.ids
}

Argument Reference

Argument Description
genre (Optional) A genre to use as a seed for recommendations.
mood (Optional) A mood to use as a seed for recommendations.
seed_tracks (Optional) A list of Spotify track URIs to use as seeds for recommendations.
seed_artists (Optional) A list of Spotify artist URIs to use as seeds for recommendations.
seed_genres (Optional) A list of genres to use as seeds for recommendations.
limit (Optional) The maximum number of tracks to return. Default: 20.
min_popularity (Optional) The minimum popularity value for recommended tracks (0-100).
max_popularity (Optional) The maximum popularity value for recommended tracks (0-100).
target_popularity (Optional) The target popularity value for recommended tracks (0-100).
min_energy (Optional) The minimum energy value for recommended tracks (0.0-1.0).
max_energy (Optional) The maximum energy value for recommended tracks (0.0-1.0).
target_energy (Optional) The target energy value for recommended tracks (0.0-1.0).

Additional audio feature parameters are available for fine-tuning recommendations, including tempo, danceability, acousticness, and more.

Attribute Reference

Attribute Description
id A unique identifier for this data source.
ids A list of Spotify track IDs for the recommended tracks.
uris A list of Spotify track URIs for the recommended tracks.
tracks A list of track objects containing detailed information about each track.