GitHub
Back to Documentation
Created by Bilal Ashraf

Spotify Provider

The Spotify provider allows you to manage Spotify resources like playlists and tracks using infrastructure as code. It also provides data sources for retrieving information based on weather, time, and user profiles to create dynamic, mood-based playlists.

Example Usage

terraform {
  required_providers {
    spotify = {
      source  = "ashrafxbilal/spotify"
      version = "0.1.0"
    }
  }
}

provider "spotify" {
  client_id     = var.spotify_client_id
  client_secret = var.spotify_client_secret
  redirect_uri  = var.spotify_redirect_uri
  refresh_token = var.spotify_refresh_token
}

Authentication

The Spotify provider requires several credentials to authenticate with the Spotify API:

Credential Description
client_id Your Spotify application client ID
client_secret Your Spotify application client secret
redirect_uri The redirect URI configured for your Spotify application
refresh_token A refresh token obtained through the OAuth flow

These can be provided in the provider configuration block or as environment variables:

# Set environment variables for authentication
export SPOTIFY_CLIENT_ID="your-client-id"
export SPOTIFY_CLIENT_SECRET="your-client-secret"
export SPOTIFY_REDIRECT_URI="your-redirect-uri"
export SPOTIFY_REFRESH_TOKEN="your-refresh-token"

Getting Started

1. Create a Spotify Developer Application

Before using this provider, you need to create a Spotify Developer application:

  1. Go to the Spotify Developer Dashboard
  2. Log in with your Spotify account
  3. Click "Create an App"
  4. Fill in the app name and description
  5. Click "Edit Settings" and add a redirect URI (e.g., http://localhost:8888/callback)
  6. Save the Client ID and Client Secret

2. Obtain a Refresh Token

To obtain a refresh token, you can use the included authentication helper:

# Clone the repository
git clone https://github.com/ashrafxbilal/terraform-provider-spotify.git
cd terraform-provider-spotify

# Set your Spotify credentials
export SPOTIFY_CLIENT_ID="your-client-id"
export SPOTIFY_CLIENT_SECRET="your-client-secret"
export SPOTIFY_REDIRECT_URI="your-redirect-uri"

# Run the authentication helper
go run spotify_auth_proxy/spotify-auth.go

Follow the instructions to authorize the application and obtain a refresh token.

3. Configure Terraform

Create a Terraform configuration file (e.g., main.tf) with the provider configuration:

terraform {
  required_providers {
    spotify = {
      source  = "ashrafxbilal/spotify"
      version = "0.1.0"
    }
  }
}

provider "spotify" {
  client_id     = var.spotify_client_id
  client_secret = var.spotify_client_secret
  redirect_uri  = var.spotify_redirect_uri
  refresh_token = var.spotify_refresh_token
}

Provider Arguments

Argument Description
client_id (Required) Your Spotify application client ID. Can also be set with the SPOTIFY_CLIENT_ID environment variable.
client_secret (Required) Your Spotify application client secret. Can also be set with the SPOTIFY_CLIENT_SECRET environment variable.
redirect_uri (Required) The redirect URI configured for your Spotify application. Can also be set with the SPOTIFY_REDIRECT_URI environment variable.
refresh_token (Required) A refresh token obtained through the OAuth flow. Can also be set with the SPOTIFY_REFRESH_TOKEN environment variable.
api_url (Optional) The Spotify API URL. Defaults to https://api.spotify.com/v1.
auth_url (Optional) The Spotify authentication URL. Defaults to https://accounts.spotify.com/api/token.