This is a terraform provider for managing your spotify playlists.
Featured tutorial - https://learn.hashicorp.com/tutorials/terraform/spotify-playlist
Featured interview - https://www.hashicorp.com/blog/build-your-summer-spotify-playlist-with-terraform
I am not affiliated with Hashicorp or Terraform.
If you are having trouble with the provider, try updating to the latest version before submitting a bug report
resource "spotify_playlist" "playlist" {
name = "My playlist"
description = "My playlist is so awesome"
public = false
tracks = flatten([
data.spotify_track.overkill.id,
data.spotify_track.blackwater.id,
data.spotify_track.overkill.id,
data.spotify_search_track.search.tracks[*].id,
])
}
data "spotify_track" "overkill" {
url = "https://open.spotify.com/track/4XdaaDFE881SlIaz31pTAG"
}
data "spotify_track" "blackwater" {
spotify_id = "4lE6N1E0L8CssgKEUCgdbA"
}
data "spotify_search_track" "search" {
name = "Somebody Told Me"
artist = "The Killers"
album = "Hot Fuss"
}
output "test" {
value = data.spotify_search_track.search.tracks
}
Add the following to your terraform configuration
terraform {
required_providers {
spotify = {
source = "conradludgate/spotify"
version = "~> 0.2.0"
}
}
}
First, you need an instance of a spotify oauth2 server running. This acts as a middleware between terraform and spotify to allow easy access to access tokens.
For a simple way to manage your spotify oauth2 tokens is to use https://oauth2.conrad.cafe. (source code)
Register a new account, create a spotify token with the following scopes
- user-read-email
- user-read-private
- playlist-read-private
- playlist-modify-private
- playlist-modify-public
- user-library-read
- user-library-modify
Then take note of the token id in the URL and the API key that is shown on the page
Configure the terraform provider like so
provider "spotify" {
auth_server = "https://oauth2.conrad.cafe"
api_key = var.spotify_api_key
username = "your username"
token_id = "your token id"
}
variable "spotify_api_key" {
type = string
}
If you want a bit more control over your tokens, you can self host a simple instance of the oauth2 proxy designed specifically for this terraform provider
See spotify_auth_proxy to get started.
Once you have the server running, make note of the API Key it gives you.
Configure the terraform provider like so
variable "spotify_api_key" {
type = string
}
provider "spotify" {
api_key = var.spotify_api_key
}
Step-1: Open Spotify Developer page & create app :
Step-2: Enter name, description & Redirect path: http://localhost:27228/spotify_callback.
Step-5: Get the Client ID & Serect:
Step-6: Set env variable :
i)export SPOTIFY_PROXY_BASE_URI=http://localhost:27228/spotify_callback
ii)create environment file with
Step-7: Set value:
SPOTIFY_CLIENT_ID=
SPOTIFY_CLIENT_SECRET=
Step-8: Run docker command
$ docker run --rm -it -p 27228:27228 --env-file ./.env ghcr.io/conradludgate/spotify-auth-proxy
Step-9: Click the link & approve in browser:
Step-10: Crete terraform files:
This is an instance of a 'Spotify auth server' which acts as an interface between a client and the Spotify oauth API.
With go
installed, run
go get -u github.com/conradludgate/terraform-provider-spotify/spotify_auth_proxy
First, you need a Spotify client ID and secret. Visit https://developer.spotify.com/dashboard/ to create an application.
If you plan to run this proxy locally, set the redirect URI of the application to http://localhost:27228/spotify_callback
.
If you're running it remotely, substitute an appropiate base url (eg https://spotify.example.com/spotify_callback
)
You will also need to register the callback URI with Spotify for your application. Visit https://developer.spotify.com/dashboard/, click on your application, find and click the "Edit Settings" button, and paste the spotify_callback
URI above into "Redirect URIs". Scroll down and click "Save".
To start the server, make sure the environment variables SPOTIFY_CLIENT_ID
and SPOTIFY_CLIENT_SECRET
are set from the values on the Spotify dashboard.
If running remotely, also configure your base url
export SPOTIFY_PROXY_BASE_URI=https://spotify.example.com
Finally, run
spotify_auth_proxy
It should output the following:
APIKey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Auth URL: ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Take note of these values.
Now, open a browser and navigate to the Auth URL. It should redirect you to Spotify to log in. After you log in, the auth server will redirect you back to the page where it should confirm that you've authorized correctly.
The API Key is how you will retrieve the access token. The server will handle the token expiration and refreshes for you.
The API Key is random on each invocation, you can specify the SPOTIFY_PROXY_API_KEY
environment variable to keep it consistent
Alternatively, you can use the Docker to run the Spotify auth proxy.
First, create a file named .env
and populate it with your SPOTIFY_CLIENT_ID
and SPOTIFY_CLIENT_SECRET
values. Your file should look similar to the following.
SPOTIFY_CLIENT_ID=
SPOTIFY_CLIENT_SECRET=
Then, run the following command to start the auth proxy.
$ docker run --rm -it -p 27228:27228 --env-file ./.env ghcr.io/conradludgate/spotify-auth-proxy
APIKey: OK7b1j...
Token: aoIvJT...
Auth: http://localhost:27228/authorize?token=aoIvJT...