Skip to content

NotTsunami/obs-twitch-spotify-script

Repository files navigation

Twitch -> Spotify Song Requests for OBS

An OBS Python script that turns Twitch Channel Points into Spotify song requests. Viewers redeem a reward and paste a Spotify track URL; the bot adds the track to your configured Spotify playlist and playback queue, and marks the redemption fulfilled or refunds it. OBS starts and stops the bot for you — no separate terminal window needed.

Files

obs_song_request.py      # load this in OBS: Tools -> Scripts
song_request_core.py     # shared Twitch/Spotify logic
song_request_bot.py      # optional CLI helper for reward creation/listing
requirements.txt         # install into the same Python runtime OBS uses

Requirements

  • OBS Studio with Python scripting enabled.
  • 64-bit Python 3.11 or 3.12 on Windows when using 64-bit OBS.
  • Spotify Premium. Spotify's queue endpoint rejects free accounts.
  • Twitch Affiliate or Partner. Channel Points rewards require this.
  • Spotify running with an active playback device before testing requests.

OBS's scripting guide currently points to Python 3.6-3.12 support, but the Spotify/keyring package set used here requires newer Python than 3.6. Python 3.11 or 3.12 is the safest target for OBS compatibility.

References:

1. Configure OBS Python

  1. Install 64-bit Python 3.11 or 3.12.
  2. Open OBS.
  3. Go to Tools -> Scripts -> Python Settings.
  4. Point OBS at that Python install.
  5. Confirm OBS shows the Python version in the dialog.

2. Install Packages Into OBS Python

Use the exact Python executable that OBS is configured to use:

py -3.12 -m pip install -r requirements.txt

If OBS is configured for Python 3.11, use:

py -3.11 -m pip install -r requirements.txt

The required direct packages are:

  • spotipy
  • twitchAPI
  • python-dotenv
  • keyring

keyring stores Twitch/Spotify application credentials in the OS credential store. On Windows this uses Windows Credential Manager.

3. Register a Twitch Application

  1. Go to https://dev.twitch.tv/console/apps.
  2. Register an application.
  3. Set the OAuth Redirect URL to http://localhost:17563 (the default used by the twitchAPI library's user authenticator).
  4. Copy the Client ID and Client Secret. You will paste these into OBS once.
  5. Set the broadcaster login in OBS to your Twitch login slug, not display name.

Required Twitch scopes are handled by the script:

  • channel:read:redemptions
  • channel:manage:redemptions

4. Register a Spotify Application

  1. Go to https://developer.spotify.com/dashboard.
  2. Create an app with Web API enabled.
  3. Add this redirect URI:
http://127.0.0.1:8888/callback
  1. Copy the Spotify Client ID and Client Secret. You will paste these into OBS once.

5. Load the OBS Script

  1. Open OBS.
  2. Go to Tools -> Scripts.
  3. Click + and select obs_song_request.py.
  4. Fill in the non-secret settings:
    • Twitch broadcaster login
    • Twitch reward ID, if you already have an app-owned reward
    • Spotify playlist ID
    • Spotify redirect URI
    • Token cache directory
    • New reward title/cost, if you need OBS to create the reward
  5. Paste each credential value into its matching "paste to store" field:
    • Twitch client ID
    • Twitch client secret
    • Spotify client ID
    • Spotify client secret
  6. Apply the settings.

The script immediately stores those credential values through keyring and clears the OBS fields. That keeps secrets out of OBS scene/config JSON.

Token cache files are still written locally:

.cache-twitch.json
.cache-spotify.json

These OAuth token cache files should not be committed or shown on stream.

6. Create an App-Owned Twitch Reward

Twitch only lets the application that created a reward update that reward's redemptions. If you made the reward manually in the Twitch dashboard, songs may queue but the bot cannot mark requests fulfilled/refunded.

Use the OBS script controls:

  1. Keep Enable song request bot off until the reward exists.
  2. Fill in Twitch broadcaster login, Spotify playlist ID, and the credential paste-to-store fields.
  3. Set New reward title and New reward cost.
  4. Click Create app-owned Twitch reward.
  5. Watch the OBS script log. The script logs the created reward ID and writes it into the Twitch reward ID setting.

You can also click List Twitch rewards in OBS log to inspect existing reward IDs.

The CLI helper still exists if you prefer running it from a terminal:

python song_request_bot.py --create-reward --title "Song Request" --cost 500

For CLI use, provide values through your shell environment or a local .env. OBS use does not require a .env file.

You can still edit the reward's icon, color, cooldowns, and appearance in the Twitch dashboard after creation. Do not delete and recreate it manually.

To list existing rewards:

python song_request_bot.py --list-rewards

7. Running

In the OBS script settings:

  • Enable Enable song request bot.
  • Leave Run only while streaming enabled if you want the bot active only while OBS is streaming.

On the first run, browsers may open for Twitch and Spotify OAuth approval. After that, the cached OAuth tokens are reused.

Before testing, start Spotify and play any track so Spotify has an active device.

Accepted Request Formats

  • https://open.spotify.com/track/<id>
  • https://open.spotify.com/track/<id>?si=...
  • https://open.spotify.com/intl-de/track/<id>
  • spotify:track:<id>

Refund Cases

The redemption is canceled/refunded when:

  • the viewer input is not a valid Spotify track URL/URI
  • the Spotify track lookup fails
  • adding to the playlist fails

If the playlist add succeeds but queueing fails, the redemption is fulfilled because the song was still saved to the playlist.

Troubleshooting

Symptom Fix
OBS says No module named spotipy, twitchAPI, or keyring Install packages with the same Python executable configured in OBS Python Settings.
Bot will not start because credentials are missing Paste the credential values into the OBS "paste to store" fields again and apply settings.
Browser auth opens every time Check that the token cache directory is writable and stable.
Spotify queue fails with no active device Open Spotify and start playback before testing.
Redemptions queue songs but are not fulfilled/refunded Recreate the reward with python song_request_bot.py --create-reward and update the OBS reward ID.
Spotify reports insufficient scope Delete .cache-spotify.json and reauthorize.
Twitch cached token is invalid Delete .cache-twitch.json and reauthorize.

Security Notes

  • Do not put secrets in browser-source URLs, text sources, scene names, or logs.
  • OBS settings are stored in local config files, so this script does not persist credential fields in OBS settings.
  • Twitch/Spotify client credentials are stored with keyring.
  • OAuth token cache files remain sensitive. Keep .cache-* ignored by git.

About

OBS script used to connect Twitch channel redeems to a Spotify playlist

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Contributors

Languages