blog

Getting a Spotify refresh token without running a server

Nov 27, 2026 1 min read spotifyapi

For a personal site you don't need a login flow. You authorize your own account once, keep the refresh token in an env var, and let the server trade it for short-lived access tokens.

1. Create the app

Make an app in the Spotify developer dashboard and add a redirect URI. This is the step that bit me, so read the next section before you pick one.

2. Authorize once

Open this URL in a browser, with your own client id. The scopes are what let you read the current track, recent plays and top items.

https://accounts.spotify.com/authorize
  ?client_id=CLIENT_ID
  &response_type=code
  &redirect_uri=http://127.0.0.1:3000
  &scope=user-read-currently-playing user-read-recently-played user-top-read

After you approve, the browser lands on a dead page. The code in its address bar is what you want. It expires within minutes, so use it right away.

3. Trade the code

curl -X POST https://accounts.spotify.com/api/token \
  -u CLIENT_ID:CLIENT_SECRET \
  -d grant_type=authorization_code \
  -d code=THE_CODE \
  -d redirect_uri=http://127.0.0.1:3000

The response includes refresh_token. Save it. From now on the server calls the same endpoint with grant_type=refresh_token to get a fresh access token.

The redirect URI trap

I got invalid_grant: Invalid redirect URI over and over with http://localhost:3000. Spotify now wants the loopback IP, not the hostname, and the string in the token request has to match the authorize request character for character. Switching to 127.0.0.1 fixed it immediately.